releasing PictureBox's Image file handle
-
In the code below I load an image into a PictureBox using the Image.FromFile method. When I am done with that image I would like to release it so that I can delete it from the hard drive. However, even after I set the PictureBox's Image property to another image I still can not delete it. Obviously my process still has a handle to the file but I can't find a way to release that handle. I tried looking through the methods and properties of pictureBox1.Image but I did not see anything that looked useful. Any ideas? ------------------ pictureBox1.Image= Image.FromFile(MyFile); pictureBox1.Image = Image.FromFile(AnotherFile); System.IO.File.Delete(MyFile); //This fails becuase MyFile is "in use by another process"
-
In the code below I load an image into a PictureBox using the Image.FromFile method. When I am done with that image I would like to release it so that I can delete it from the hard drive. However, even after I set the PictureBox's Image property to another image I still can not delete it. Obviously my process still has a handle to the file but I can't find a way to release that handle. I tried looking through the methods and properties of pictureBox1.Image but I did not see anything that looked useful. Any ideas? ------------------ pictureBox1.Image= Image.FromFile(MyFile); pictureBox1.Image = Image.FromFile(AnotherFile); System.IO.File.Delete(MyFile); //This fails becuase MyFile is "in use by another process"
-
Got it. Thanks. ------------------ pictureBox1.Image= Image.FromFile(MyFile); pictureBox1.Image.Dispose(); pictureBox1.Image = Image.FromFile(AnotherFile); System.IO.File.Delete(MyFile);
i was just about to say,
Dispose
the Image. When you Dispose an object, it releases any unmanaged resources it has open, including file handles. RageInTheMachine9532 "...a pungent, ghastly, stinky piece of cheese!" -- The Roaming Gnome