Reputation: 154
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;
}
Upvotes: 0
Views: 64
Reputation: 128042
You don't need to write any image file at all.
Just directly use the Stream returned by slide.ConvertToImage
to load a BitmapImage:
foreach (var slide in presentation.Slides)
{
var bitmap = new BitmapImage();
using (var imageStream = slide.ConvertToImage(Syncfusion.Drawing.ImageFormat.Jpeg))
{
imageStream.Position = 0;
bitmap.BeginInit();
bitmap.StreamSource = imageStream;
bitmap.CacheOption = BitmapCacheOption.OnLoad;
bitmap.EndInit();
}
VisualAidPPT.Add(bitmap);
}
EDIT: In case the above does not work, you may still decode a System.Drawing.Image
and save that to a MemoryStream:
foreach (var slide in presentation.Slides)
{
var bitmap = new BitmapImage();
using (var imageStream = slide.ConvertToImage(Syncfusion.Drawing.ImageFormat.Jpeg))
using (var image = System.Drawing.Image.FromStream(imageStream))
using (var memoryStream = new MemoryStream())
{
image.Save(memoryStream, System.Drawing.Imaging.ImageFormat.Jpeg);
memoryStream.Position = 0;
bitmap.BeginInit();
bitmap.StreamSource = memoryStream;
bitmap.CacheOption = BitmapCacheOption.OnLoad;
bitmap.EndInit();
}
VisualAidPPT.Add(bitmap);
}
Upvotes: 1