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?