On Thu, 14 Jun 2007 04:09:52 -0700, FrankB <bo*******@iabg.dewrote:
after setting another image to the picture box it is not possible to
delete the last shown file via File.Delete ( sFilename). Path of image
file is correct. Error message box says: file is being used by other
process.
Is there a way to "free" the file?
As long as the Image is still around somewhere (eg attached to the
PictureBox), you're not going to be able to do anything with the file. I
assume that this is because .NET has memory-mapped the file, so that the
backing store is the original file rather than the swap file. At least,
that's the only reason I can think of for it to keep the file locked after
creating the Image instance.
Anyway, the solution is to make a copy of the image before attaching to
the PictureBox:
using (Image image = Image.FromFile(...))
{
pictureBoxMain.Image = new Image(image);
}
// deleting original image file should work at this point in the code
Pete