Open image from file, then release lock?

I use the following line of code to open Image from a file:

 pictureBox1.Image = Image.FromFile("test.png"); 

I expect it to lock the file, load the image into memory, set pictureBox1.Image to a copy in memory, and release the lock. In fact, the lock will not disappear until I Dispose() in Image in memory. I can’t release the file lock on the hard drive, which I no longer use, until I get rid of the file in memory that I use.
The Microsoft site mentions it in an article in C #, but their solution is written on a visual basis, which is useless to me.

In short: I want to set pictureBox1.Image to the image saved in "test.png" , then allow the user to edit or delete "test.png" or something else.

+54
c # locking image winforms
Jul 04 2018-11-11T00:
source share
5 answers

The flow approach is incorrect .

See here https://stackoverflow.com/a/312960/

Correct the code from the link above:

 Image img; using (var bmpTemp = new Bitmap("image_file_path")) { img = new Bitmap(bmpTemp); } 
+61
Jan 02 2018-12-12T00:
source share

Or even better, use the using statement (the code below is otherwise copied from the simon's column [deleted]). That way, if Image.FromStream throws an exception, you can be sure that the stream is closed immediately.

 using (FileStream stream = new FileStream("test.png", FileMode.Open, FileAccess.Read)) { pictureBox1.Image = Image.FromStream(stream); } 
+45
Jul 04 2018-11-22T00:
source share

You can also use the stream to read the image, and then close the stream.

 FileStream stream = new FileStream("test.png", FileMode.Open, FileAccess.Read); pictureBox1.Image = Image.FromStream(stream); stream.Close(); 
+9
Jul 04 2018-11-21T00:
source share

The easiest way I've found is to freeze the object containing the source (file path). All controls that may contain the image seem to have a .Source, which, if not null, will lock the file it points to.

Now the trick is to change the Image control to a read-only state, which then unlocks the file.

My decision:

  private Image CreatePreviewImage() { Image ReportImage = new Image(); Uri path = new Uri(@"C:\Folder\Image1.png"); if (File.Exists(path.OriginalString)) { ReportImage.Name = "Report1"; ReportImage.Source = LoadImageFromFile(path); } return ReportImage; } public ImageSource LoadImageFromFile(Uri path) { BitmapImage bitmap = new BitmapImage(); bitmap.BeginInit(); bitmap.UriSource = path; bitmap.CacheOption = BitmapCacheOption.OnLoad; bitmap.CreateOptions = BitmapCreateOptions.IgnoreImageCache; bitmap.DecodePixelWidth = 900; bitmap.EndInit(); bitmap.Freeze(); //This is the magic line that releases/unlocks the file. return bitmap; } 
+2
Jun 07 '16 at 8:03
source share

speak openly, read and release

     StreamReader streamReader = new StreamReader ("picture.png");
     Bitmap tmpBitmap = (Bitmap) Bitmap.FromStream (streamReader.BaseStream);
     streamReader.Close ();
     pictureBox1.Image = tmpBitmap; `
    
0
May 19 '16 at 21:43
source share



All Articles