The PictureBox control is one of the most used controls in Windows Forms applications. And sometimes you might want to delete the image file that is displayed on it. Doing so with an image displayed using the Image.FromFile function will result in a file locked exception. And file will stay locked until the program exits, even if you set the variable to null.
The best solution to this problem is to load the file as stream using the Image.FromStream function. The following code is an example of how to do this.
Stream stream = File.Open("C:\\imagefile.jpg", FileMode.Open, FileAccess.Read, FileShare.Delete); snapPictureBox.Image = Image.FromStream(stream); stream.Close();