question

Chocolade-4229 avatar image
0 Votes"
Chocolade-4229 asked karenpayneoregon answered

Why getting exception The process cannot access the file image0.gif' because it is being used by another process even if I dispose it first ?

 private void GenerateAnimatedGifs()
         {
             UnFreezWrapper unfreezWrapper = new UnFreezWrapper();
    
             checkBoxGetImages = false;
             checkBoxGetAllImages.Checked = false;
             GetImagesFiles();
    
             for (int i = 0; i < filesSatellite.Length; i++)
             {
                 Image img = Image.FromFile(filesSatellite[i]);
                 img.Save(filesSatellite[i] + "ConvertedToGif.gif", System.Drawing.Imaging.ImageFormat.Gif);
                 img.Dispose();
    
                 File.Delete(filesSatellite[i]);
             }
    
             GetImagesFiles();
    
             unfreezWrapper.MakeGIF(filesRadar.ToList(), @"d:\Downloaded Images\Animates Gifs\radanim.gif", 100, true);
             unfreezWrapper.MakeGIF(filesSatellite.ToList(), @"d:\Downloaded Images\Animates Gifs\satanim.gif", 100, true);
         }


In the loop I convert each image to gif save it in other name and then dispose the original image and then trying to delete the original image so only the ConvertedToGif images will left.

but I'm getting the exception is being used by another process on the delete line

 File.Delete(filesSatellite[i]);

but isn't the file disposed already ?


dotnet-csharpwindows-forms
· 5
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.


Does it work if you temporarily remove or comment the img variable and code? If File.Delete still does not work, then maybe the files are locked by GetImageFiles.

0 Votes 0 ·

The problem was that in the constructor when running the application I added the images to a pictureBox with a timer. so the images were busy.

Now it's working but I also tried when converting the images to gif I did

 pictureBox2.Image = null;

and also stopped the timer but still I get the exception that it's busy. only if in the constructor I don't load the images at all to the pictureBox it's working.

Why when setting the pictureBox2.Image to null is not working and the images are still busy ?


0 Votes 0 ·

They must be disposed, like :

 if (pictureBox2.Image != null)
 {
     pictureBox2.Image.Dispose();
     pictureBox2.Image = null;
 }


0 Votes 0 ·

I tried to stop the timer and to remove the current image/s from the pictureBox2 but still I can't delete

Still the pictureBox2 make the image/s busy even if I set the pictureBox2.Image to be null.

 private void GenerateAnimatedGifs()
         {
             UnFreezWrapper unfreezWrapper = new UnFreezWrapper();
    
             checkBoxGetImages = false;
             checkBoxGetAllImages.Checked = false;
             GetImagesFiles();
    
             timer2.Enabled = false;
             pictureBox2.Image = null;
    
             for (int i = 0; i < filesSatellite.Length; i++)
             {
                 Image img = Image.FromFile(filesSatellite[i]);
                 img.Save(filesSatellite[i] + "ConvertedToGif.gif", System.Drawing.Imaging.ImageFormat.Gif);
                 img.Dispose();
    
                 File.Delete(filesSatellite[i]);
             }
    
             GetImagesFiles();
    
             unfreezWrapper.MakeGIF(filesRadar.ToList(), @"d:\Downloaded Images\Animates Gifs\radanim.gif", 100, true);
             unfreezWrapper.MakeGIF(filesSatellite.ToList(), @"d:\Downloaded Images\Animates Gifs\satanim.gif", 100, true);
         }
0 Votes 0 ·

Try replacing 'pictureBox2.Image = null' with 'pictureBox2?.Dispose( ); pictureBox2.Image = null'.

In addition, you should probably execute 'pictureBox2?.Dispose( ); pictureBox2.Image = another_image' when you assign new images.


0 Votes 0 ·

1 Answer

karenpayneoregon avatar image
0 Votes"
karenpayneoregon answered

See if this might work for you

 ImageHelpers.LoadBitmap("Your file name goes here")


 public static class ImageHelpers
 {
     /// <summary>
     /// Load a clone of an image
     /// </summary>
     /// <param name="fileName">Image file to load</param>
     /// <returns><see cref="Bitmap"/></returns>
     public static Bitmap LoadBitmap(string fileName)
     {
         Bitmap imageClone = null;
         var imageOriginal = Image.FromFile(fileName);
    
         imageClone = new Bitmap(imageOriginal.Width, imageOriginal.Height);
    
         Graphics gr = Graphics.FromImage(imageClone);
         gr.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.None;
         gr.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.NearestNeighbor;
         gr.PixelOffsetMode = System.Drawing.Drawing2D.PixelOffsetMode.HighSpeed;
         gr.DrawImage(imageOriginal, 0, 0, imageOriginal.Width, imageOriginal.Height);
         gr.Dispose();
         imageOriginal.Dispose();
    
         return imageClone;
     }       
 }



5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.