Search code examples
c#zippictureboxdelete-fileunzip

Delete an image programmatically after loaded in pictureBox completely in C#


I have an image file named image1.jpg in picture folder. This image file is protected by zip and the password is 1234. I need to load this image to my C# winform application; So I have to unzip this image, load it and then delete the image1.jpg again. My problem is that my C# application doesn't show the image when I delete it even after loading the image. If I remove "File.Delete(...." line as you see, It shows the image and there is no problem.

//Unzip a zip file protected and overwrite if needed
using (ZipFile zip = ZipFile.Read(Directory.GetCurrentDirectory().ToString() + "\\Pictures" + "\\image1.zip"))
{
    zip.Password = "1234";
    zip.ExtractAll(Directory.GetCurrentDirectory().ToString() + "\\Pictures\\", Ionic.Zip.ExtractExistingFileAction.DoNotOverwrite);
}



pictureBox1.ImageLocation = Directory.GetCurrentDirectory().ToString() + "\\Pictures" + "\\image1.jpg";


File.Delete(Directory.GetCurrentDirectory().ToString() + "\\Pictures" + "\\image1.jpg");

Solution

  • var imagePath = Directory.GetCurrentDirectory().ToString() + "\\Pictures" + "\\image1.jpg;
    pictureBox1.LoadCompleted += (s, e) => File.Delete(imagePath);
    pictureBox1.ImageLocation = imagePath;