Search code examples
c#winformspicturebox

lock image file in PictureBox


I load an image into a picturebox:

myPictureBox.Image = Image.FromFile(strImageFile);

and works perfectly, but the image file is locked and I can't manage until my application is closed..

I need, from another window of the program, save a new image to reload when this child window is closed..


Solution

  • Image.FromFile will keep the File open which prevents access to the image file till the Image is disposed. If you want to release the lock, you need to keep the Image file in memory.

    myPictureBox.Image = Image.FromStream(new MemoryStream(File.ReadAllBytes(strImageFile)));