PictureBox and Dispose

c#

Solution

You can read a image into the picture box without locking it as shown below

Image img;
string file = @"d:\a.jpg";
using (Bitmap bmp = new Bitmap(file))
{
   img = new Bitmap(bmp);
   current_pic.Image = img;
}
if (File.Exists(file))
{
    File.Delete(file);
    current_pic.Image.Save(file, ImageFormat.Jpeg);
}
else
    current_pic.Image.Save(file, ImageFormat.Jpeg);

I have updated the code to even support the save operation.

While the previous code supported the delete even after linking the images. The stream was closed and this while saving resulted in a GDI+ error.

The newly updated code meets all your requirements as follows

- Allowing a delete of the file while the images is linked

- Save of the Image using the Image property in the Picturebox control

Problem

I must delete an image file if it already exists (overwriting it) while a `PictureBox` is showing the same. However If I try to delete the file it's blocked by `PictureBox`. So I write the following code: ``` if (File.Exists(file)) { Image _tmp = (Image)current_pic.Image.Clone(); current_pic.Image.Dispose(); current_pic.Dispose(); File.Delete(path); current_pic.Image = _tmp; current_pic.Image.Save(file, ImageFormat.Jpeg); } else current_pic.Image.Save(file, ImageFormat.Jpeg); ``` and the Image on filesystem is deleted thanks to `pic.Dispose()` but the Image is not more showed inside the `PictureBox`. Maybe does `Dispose()` method invalidate `PictureBox`?

Original source