查看图像文件而不锁定它。 (复制到内存?)

发布于 2024-12-29 22:11:57 字数 275 浏览 0 评论 0原文

我希望能够在不锁定文件的情况下打开/查看图像 (.jpg)。基本上我有一个程序,可以让用户选择一张图片来覆盖一张图片。但问题是我显示的图像被覆盖。那么如何在不锁定图像的情况下加载图像呢?

这是我现在必须设置图像的代码

Image1.Source = new BitmapImage( new Uri( myFilePath ) ) ); 

myFilePath 等于一个类似于“C:\Users*\My Pictures\Sample.jpg”的字符串

I want to be able to open/view a image (.jpg) without locking the file. Basically I have a program that lets the user choose a picture that will overwrite a picture. But the problem is that I display the image that is being overwritten. So how do I load an image without locking it?

This is the code I have to set the image right now

Image1.Source = new BitmapImage( new Uri( myFilePath ) ) ); 

myFilePath is equal to a string that would something like "C:\Users*\My Pictures\Sample.jpg"

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

恏ㄋ傷疤忘ㄋ疼 2025-01-05 22:11:57

myBitmap.CacheOption = BitmapCacheOption.OnLoad 是您要查找的行。它“在加载时将整个图像缓存到内存中。所有对图像数据的请求都从内存存储中填充。”来自 MSDN

类似这样的内容:

BitmapImage bmi = new BitmapImage();
bmi.BeginInit();
bmi.UriSource = new Uri(myFilePath);
bmi.CacheOption = BitmapCacheOption.OnLoad;
bmi.EndInit();
Image1.Source = bmi;

myBitmap.CacheOption = BitmapCacheOption.OnLoad is the line you're looking for. It "caches the entire image into memory at load time. All requests for image data are filled from the memory store." From MSDN

Something like this:

BitmapImage bmi = new BitmapImage();
bmi.BeginInit();
bmi.UriSource = new Uri(myFilePath);
bmi.CacheOption = BitmapCacheOption.OnLoad;
bmi.EndInit();
Image1.Source = bmi;
故事↓在人 2025-01-05 22:11:57

我认为 StreamSource 是您正在寻找的房产。您将图像读入 MemoryStream,然后将 MemoryStream 设置为 BitmapImage 的 StreamSource 的值:

var memStream = new MemoryStream(File.ReadAllBytes(myFilePath));
Image1.Source = new BitmapImage() { StreamSource = memStream };

编辑:我已经尝试过这段代码,看起来您需要围绕设置 Source 调用 BitmapImage.BeginInit 和 BitmapImage.EndInit :

var memStream = new MemoryStream(File.ReadAllBytes(@"C:\Users\Public\Pictures\Sample Pictures\Koala.jpg"));
var img = new BitmapImage();
img.BeginInit();
img.StreamSource = memStream;
img.EndInit();
myImage.Source = img;

I think that StreamSource is the property you are looking for. You'd read the image into a MemoryStream, then set the MemoryStream as the value of the BitmapImage's StreamSource:

var memStream = new MemoryStream(File.ReadAllBytes(myFilePath));
Image1.Source = new BitmapImage() { StreamSource = memStream };

EDIT: I've tried this code, and it looks like you need to call BitmapImage.BeginInit and BitmapImage.EndInit around setting the Source:

var memStream = new MemoryStream(File.ReadAllBytes(@"C:\Users\Public\Pictures\Sample Pictures\Koala.jpg"));
var img = new BitmapImage();
img.BeginInit();
img.StreamSource = memStream;
img.EndInit();
myImage.Source = img;
聆听风音 2025-01-05 22:11:57

当您打开文件时,您还可以选择文件的共享来定义其在另一个程序需要该文件时的行为:(

来自 msdn : http://msdn.microsoft.com/en-us/library/y973b725.aspx )

File.Open 方法(字符串,文件模式、文件访问、** FileShare **)

参数
路径
类型:System.String
要打开的文件。

模式
类型:System.IO.FileMode
一个 FileMode 值,指定文件不存在时是否创建,并确定是保留还是覆盖现有文件的内容。

访问
类型:System.IO.FileAccess
一个 FileAccess 值,指定可以对文件执行的操作。

* 分享
* 类型:System.IO.FileShare *
FileShare 值指定其他线程对该文件的访问类型。

When you open a file, you can also choose the share of the file to define its beaviour when another program requires that file :

(from msdn : http://msdn.microsoft.com/en-us/library/y973b725.aspx )

File.Open Method (String, FileMode, FileAccess, ** FileShare **)

Parameters
path
Type: System.String
The file to open.

mode
Type: System.IO.FileMode
A FileMode value that specifies whether a file is created if one does not exist, and determines whether the contents of existing files are retained or overwritten.

access
Type: System.IO.FileAccess
A FileAccess value that specifies the operations that can be performed on the file.

* share
* Type: System.IO.FileShare *
A FileShare value specifying the type of access other threads have to the file.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文