Open Image from file, then release lock?

C#WinformsImageLocking

C# Problem Overview


I'm using the following line of code to open an Image from a file:

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

I expect it to lock the file, load the image to memory, set pictureBox1.Image to the copy in memory, and release the lock. In reality, the lock won't go away until I Dispose() of the Image in memory. I can not release the lock on the file on the harddrive that I am no longer using until I get rid of the file in memory that I am using.
Microsoft's site mentions it in a C#-labeled article, but their solution is written in visual basic, which is useless to me.

In summary: I want to set pictureBox1.Image to the image stored in "test.png", then let the user edit or delete "test.png" or whatever.

C# Solutions


Solution 1 - C#

The approach with stream is not correct.

See here https://stackoverflow.com/a/8701748/355264

Correct code from above link:

Image img;
using (var bmpTemp = new Bitmap("image_file_path"))
{
    img = new Bitmap(bmpTemp);
}

Solution 2 - C#

Or better yet, use a using statement (the code below is otherwise copied from sylon's [deleted] post). This way if the Image.FromStream throws an exception, you can still be assured that the stream is immediately closed.

using (FileStream stream = new FileStream("test.png", FileMode.Open, FileAccess.Read))
{
    pictureBox1.Image = Image.FromStream(stream);
}

Solution 3 - C#

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

FileStream stream = new FileStream("test.png", FileMode.Open, FileAccess.Read);
pictureBox1.Image = Image.FromStream(stream);
stream.Close();

Solution 4 - C#

The easiest ever way I found is to freeze the object that contains the Source (path to the file). All controls that can contain an image, seem to have a .Source which, if not null, it 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 solution:

    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;
    }

Solution 5 - C#

talking open, read and release

StreamReader streamReader = new StreamReader("picture.png");
Bitmap tmpBitmap = (Bitmap)Bitmap.FromStream(streamReader.BaseStream);
streamReader.Close();
pictureBox1.Image = tmpBitmap;`

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionEagle-EyeView Question on Stackoverflow
Solution 1 - C#net_progView Answer on Stackoverflow
Solution 2 - C#JoshLView Answer on Stackoverflow
Solution 3 - C#lahsrahView Answer on Stackoverflow
Solution 4 - C#Rafael VenturaView Answer on Stackoverflow
Solution 5 - C#psycobotView Answer on Stackoverflow