Eagle-Eye Eagle-Eye - 3 years ago 295
C# Question

Open Image from file, then release lock?

I'm using the following line of code to open an

from a file:

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

I expect it to lock the file, load the image to memory, set
to the copy in memory, and release the lock. In reality, the lock won't go away until I
of the
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
to the image stored in
, then let the user edit or delete
or whatever.

Answer Source

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);
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download