1
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 ?

The problem is that in the constructor I'm loading the images to a pictureBox using timer that is why the images are busy with another process.

If I'm not loading the images at the constructor everything will work fine.

But I want to display the images when running the application and also to be able to convert them to other formats and making other manipulations like creating animated gif of them.

I'm stuck here.

This is the constructor code

public Form1()
        {
            InitializeComponent();

            CheckIfImagesExist();
        }

And the code of the CheckIfImagesExist method

private void CheckIfImagesExist()
        {
            GetImagesFiles();

            if (filesRadar != null)
            {
                if (filesRadar.Length > 1)
                {
                    pictureBox1.Image = new Bitmap(filesRadar[0]);
                    trackBar1.Enabled = true;
                    timer1.Enabled = true;
                }

                if (filesRadar.Length == 1)
                {
                    trackBar1.Enabled = false;
                    pictureBox1.Image = new Bitmap(filesRadar[0]);
                }
            }

            if (filesSatellite != null)
            {
                if (filesSatellite.Length > 1)
                {
                    pictureBox2.Image = new Bitmap(filesSatellite[0]);
                    trackBar1.Enabled = true;
                    timer2.Enabled = true;
                }

                if (filesSatellite.Length == 1)
                {
                    trackBar1.Enabled = false;
                    pictureBox2.Image = new Bitmap(filesSatellite[0]);
                }
            }
        }

The timer tick event

int satImagesCount = 0;
        private void timer2_Tick(object sender, EventArgs e)
        {
            satImagesCount++;

            if (satImagesCount == filesSatellite.Length)
            {
                satImagesCount = 0;
            }

            pictureBox2.Image = new Bitmap(filesSatellite[satImagesCount]);

            if (isInsideSat)
            {
                pb.Image = new Bitmap(filesSatellite[satImagesCount]);

                timer2.Interval = trackBar1.Value * 100;
            }
        }
  • 1
    See [Why does `Image.FromFile` keep a file handle open sometimes?](https://stackoverflow.com/a/1105330/2791540). In short, you have to use a MemoryStream workaround, like this: `Image.FromStream(new MemoryStream(File.ReadAllBytes(fileName)));` – John Wu Nov 02 '21 at 19:18
  • @JohnWu The problem is that when I'm running the application in the constructor I'm loading the images to a pictureBox2 with a timer so the images are in use. If I'm not loading the images to the pictureBox it will working fine. In one side I want to show the images when running the application in the other side I want to be able also to convert the images. What should I do ? – chkfmys fms Nov 02 '21 at 21:43
  • 1
    I think you need to load the images into a MemoryStream before you attempt to load them. If you do that, it seems that the file lock issue goes away. If that doesn't work... in a worst case scenario, you could make a temp copy of the image file and load that instead.. – John Wu Nov 02 '21 at 22:10

0 Answers0