I am trying to create a C# app which loads a powerpoint and makes each slide a JPG that is stored in a list of BitmapImages. The user should be able to load another powerpoint, which, on load, deletes each of the other JPGs in the folder. Currently, I am unable to delete the JPGs, as they are "being used by another process" which happens to be this app. How can I work around this?
foreach (ISlide slide in presentation.Slides)
{
System.IO.Stream imageStream = slide.ConvertToImage(Syncfusion.Drawing.ImageFormat.Jpeg);
System.Drawing.Image convertedImage = System.Drawing.Image.FromStream(imageStream);
if (!System.IO.File.Exists(picsPath + "\\Slide_" + slide.SlideNumber + ".jpg"))
convertedImage.Save(picsPath + "\\Slide_" + slide.SlideNumber + ".jpg");
else
{
try
{
System.IO.File.Delete(picsPath + "\\Slide_" + slide.SlideNumber + ".jpg");
convertedImage.Save(picsPath + "\\Slide_" + slide.SlideNumber + ".jpg");
}
catch (Exception df){Console.WriteLine(df.StackTrace);}
}
bitmap = new BitmapImage();
bitmap.BeginInit();
bitmap.UriSource = new Uri(picsPath + "\\Slide_" + slide.SlideNumber + ".jpg");
bitmap.CacheOption = BitmapCacheOption.OnLoad;
bitmap.EndInit();
VisualAidPPT.Add(bitmap);
convertedImage = null;
}