我正在尝试创建一个C#应用程序,该应用程序可以加载PowerPoint,并使每张幻灯片成为JPG,并存储在BitmapImages列表中。用户应该能够加载另一个PowerPoint,该PowerPoint在加载时将删除文件夹中的其他每个JPG。目前,我无法删除JPG,因为它们“正被另一个进程使用”(恰好是此应用)。我该如何解决?
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;
}
答案 0 :(得分:1)
您根本不需要编写任何图像文件。
只需直接使用slide.ConvertToImage
返回的流来加载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);
}
编辑:如果上述方法不起作用,您仍然可以解码System.Drawing.Image
并将其保存到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);
}