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

时间:2021-11-20 13:29:35

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?

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

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"

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

3 个解决方案

#1


8  

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

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

Something like this:

像这样的东西:

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

#2


1  

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:

我认为StreamSource是您正在寻找的属性。您将图像读入MemoryStream,然后将MemoryStream设置为BitmapImage的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:

编辑:我已经尝试过这段代码,看起来你需要调用BitmapImage.BeginInit和BitmapImage.EndInit来设置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;

#3


0  

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

当您打开文件时,您还可以选择文件的共享来定义其他程序需要该文件时的beaviour:

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

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

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

File.Open方法(String,FileMode,FileAccess,** FileShare **)

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

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

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.

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

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

access类型:System.IO.FileAccess FileAccess值,指定可对文件执行的操作。

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

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

#1


8  

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

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

Something like this:

像这样的东西:

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

#2


1  

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:

我认为StreamSource是您正在寻找的属性。您将图像读入MemoryStream,然后将MemoryStream设置为BitmapImage的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:

编辑:我已经尝试过这段代码,看起来你需要调用BitmapImage.BeginInit和BitmapImage.EndInit来设置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;

#3


0  

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

当您打开文件时,您还可以选择文件的共享来定义其他程序需要该文件时的beaviour:

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

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

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

File.Open方法(String,FileMode,FileAccess,** FileShare **)

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

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

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.

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

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

access类型:System.IO.FileAccess FileAccess值,指定可对文件执行的操作。

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

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