查看图像文件而不锁定它。 (复制到内存?)
我希望能够在不锁定文件的情况下打开/查看图像 (.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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
myBitmap.CacheOption = BitmapCacheOption.OnLoad
是您要查找的行。它“在加载时将整个图像缓存到内存中。所有对图像数据的请求都从内存存储中填充。”来自 MSDN类似这样的内容:
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 MSDNSomething like this:
我认为 StreamSource 是您正在寻找的房产。您将图像读入 MemoryStream,然后将 MemoryStream 设置为 BitmapImage 的 StreamSource 的值:
编辑:我已经尝试过这段代码,看起来您需要围绕设置 Source 调用 BitmapImage.BeginInit 和 BitmapImage.EndInit :
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:
EDIT: I've tried this code, and it looks like you need to call BitmapImage.BeginInit and BitmapImage.EndInit around setting the Source:
当您打开文件时,您还可以选择文件的共享来定义其在另一个程序需要该文件时的行为:(
来自 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.