Quantcast
Viewing all articles
Browse latest Browse all 12583

How to delete an image file assigned to a PictureBox xontrol ?

In a Windows form application, some image files are assigned to PictureBox controls. I am trying to delete those image files. The first approach [1] with "Image.FromFile" gives an error message of "Another process is using this image file", although "Image.Dispose()" is executed. The second approach [2] using "Image.FromStream" results (X) images for PictureBoxes and Buttons, if "fs.Close()" is inclided. Without "fs.Close()", it gives the same error message of "Another process is using...". How can I display images properly and delete those images files ?

Thanks

[1]

pictureBoxSelected.Image = Image.FromFile(@actualImage);
.
.
.
pictureBoxSelected.Image.Dispose();
pictureBoxSelected.Image = Image.FromFile(@tempImage);
                                              
if (System.IO.File.Exists(actualImage) == true)
{
    System.IO.File.Delete(actualImage);
}

[2]

FileStream fs = new FileStream(fileName, FileMode.Open, FileAccess.Read);
pictureBox.Image = System.Drawing.Image.FromStream(fs);
fs.Close();


Viewing all articles
Browse latest Browse all 12583

Trending Articles