0

I have programmed for creating a animated GIF file from some jog files using NGif. The animated file (Just the automated slide show) is being created without any problem. The source images of the GIF (from which jpg files I created the animation) are in "Temp2" Folder. I wanted to delete that folder including files inside but the program is unable to handle that and giving the message: "...file is still being used by another process".

The function that I created is as below:

public void Create_Animated_GIF()
        {
            String Output_File_Path = Environment.CurrentDirectory + "\\Animation.gif";

            AnimatedGifEncoder GEncoder = new AnimatedGifEncoder();

            GEncoder.Start(Output_File_Path);
            GEncoder.SetDelay(100);
            GEncoder.SetRepeat(0);

            for (int i = 0; i < 2; i++)
            {
                GEncoder.AddFrame(System.Drawing.Image.FromFile(Image_URLs[i]));
            }

            /*
            try
            {
                System.IO.Directory.Delete(Environment.CurrentDirectory + "\\Temp2", true);
            }
            catch
            {
            }
            */

            GEncoder.Finish();

            System.IO.Directory.Delete(Environment.CurrentDirectory + "\\Temp2", true);
        }

Here, I tried try-catch; it did not work!

Another function wjere I used this Temp2 folder is as follows:


public void Crop_Images()
        {
            if (!System.IO.Directory.Exists(Environment.CurrentDirectory + "\\Temp2\\"))
                System.IO.Directory.CreateDirectory(Environment.CurrentDirectory + "\\Temp2\\");

            for (int i = 0; i < 2; i++)
            {
                System.Drawing.Image Source_Image = System.Drawing.Image.FromFile(Environment.CurrentDirectory + "\\Temp\\" + i.ToString() + ".jpg");

                System.Drawing.Bitmap Source_Image_Btmp = new System.Drawing.Bitmap(Source_Image);

                System.Drawing.Rectangle Destin_Rect = new System.Drawing.Rectangle(1327, 1330, 632, 491);

                System.Drawing.Image Destin_Image = Source_Image_Btmp.Clone(Destin_Rect, Source_Image_Btmp.PixelFormat);

                Image_URLs[i] = Environment.CurrentDirectory + "\\Temp2\\" + i.ToString() + ".jpg";

                Destin_Image.Save(Image_URLs[i]);

                Source_Image.Dispose();
                Source_Image_Btmp.Dispose();
                Destin_Image.Dispose();
            }

            /*
            try
            {
                System.IO.Directory.Delete(Environment.CurrentDirectory + "\\Temp", true);
            }
            catch
            {
            }
            */

            System.IO.Directory.Delete(Environment.CurrentDirectory + "\\Temp", true);
        }

Here, Temp folder is deleted successfully.

I tried to call the delete command after calling the relevant function from main; but still it didn't work!

Can anybody please help me how I can detach the folder from the process that I have created?

Bhargav Rao
  • 50,140
  • 28
  • 121
  • 140
Razib Ali
  • 300
  • 1
  • 4
  • 11
  • Is GEncode disposable? You may need to put it in a using block or dispose of it before you attempt to delete the jpeg files, It may still have them locked. – Cyberdrew Jun 09 '14 at 19:33
  • No, there is no such command. I found only Finish which I have used here in the code. I tried searching Close, Exit, Dispose and went through the full list; but no such suitable method I got! – Razib Ali Jun 09 '14 at 19:40
  • How about declaring System.Drawing.Image.FromFile in a variable then disposing? – Cyberdrew Jun 09 '14 at 19:50
  • Sorry Cyberbrew; I do not get you! How should I use that in the first function? Can you please let me know? – Razib Ali Jun 10 '14 at 07:18
  • 1
    for (int i = 0; i < 2; i++) { using (var image = System.Drawing.Image.FromFile(Image_URLs[i])) { GEncoder.AddFrame(image); } } – Cyberdrew Jun 10 '14 at 13:57
  • Yes! nothing to dispose here! Just using the code exactly which you have given here automatically disposes after the loop! You are great! Thank you so very much! – Razib Ali Jun 10 '14 at 18:25

1 Answers1

1

Posting answer:

for (int i = 0; i < 2; i++)
            {
                using (var image = System.Drawing.Image.FromFile(Image_URLs[i]))
                {
                    GEncoder.AddFrame(image);

                }
            }
Cyberdrew
  • 1,832
  • 1
  • 19
  • 39