PPT导出为图片

使用Aspose组件导出

Aspose有Aspose.Slides.dll,可以无需安装office,进行读写PPT文件。

Aspose可能通过Aspose.Slides.NET安装

简单的导出图片demo,如下:

复制代码
 1     internal class PptToImagesConverter
 2     {
 3         private const string ImageExtension = ".png";
 4         public bool ConvertToImages(string pptFile, string exportImagesFolder)
 5         {
 6             using (Presentation pres = new Presentation(pptFile))
 7             {
 8                 int desiredX = 1200;
 9                 int desiredY = 800;
10 
11                 float scaleX = (float)(1.0 / pres.SlideSize.Size.Width) * desiredX;
12                 float scaleY = (float)(1.0 / pres.SlideSize.Size.Height) * desiredY;
13 
14                 foreach (ISlide sld in pres.Slides)
15                 {
16                     Bitmap bmp = sld.GetThumbnail(scaleX, scaleY);
17 
18                     string slidePath = Path.Combine(exportImagesFolder,$"Slide_{sld.SlideNumber}.{ImageExtension}");
19                     bmp.Save(slidePath, System.Drawing.Imaging.ImageFormat.Png);
20                 }
21             }
22 
23             return true;
24         }
25     }
复制代码

注:以上途径是没有购买过的dll,生成的图片会有水印。使用正版购买的,应该不会有问题。

使用Interop.PowerPoint导出

也是在Nuget中搜索并安装:

另,通过Presentations.Open("c:\test.pptx::PASSWORD::")可以解密PPT,从而导出图片。 

复制代码
 1     internal class PptToImagesConverter1
 2     {
 3         private const string PASSWORD_MARK = "::PASSWORD::";
 4         private const string ImageExtension = ".png";
 5         public bool ConvertToImages(string pptFile, string exportImagesFolder)
 6         {
 7             var tempPpt = Path.Combine(Path.GetTempPath(), Guid.NewGuid() + Path.GetExtension(pptFile));
 8             File.Copy(pptFile, tempPpt);
 9             Microsoft.Office.Interop.PowerPoint.Application app = new Microsoft.Office.Interop.PowerPoint.Application();
10             Microsoft.Office.Interop.PowerPoint.Presentation presentation = app.Presentations.Open(tempPpt + PASSWORD_MARK, MsoTriState.msoTrue, MsoTriState.msoFalse, MsoTriState.msoFalse);
11             int desiredX = 1200;
12             int desiredY = 800;
13             var slides = ((Microsoft.Office.Interop.PowerPoint.Presentation)presentation).Slides.Cast<Microsoft.Office.Interop.PowerPoint.Slide>().ToList();
14             Parallel.ForEach(slides, slide =>
15             {
16                 string slidePath = Path.Combine(exportImagesFolder, "Slide-" + slide.SlideIndex + ImageExtension);
17                 slide.Export(slidePath, ImageExtension, desiredX, desiredY);
18             });
19             return true;
20         }
21     }
复制代码

 

posted @   唐宋元明清2188  阅读(527)  评论(1编辑  收藏  举报
编辑推荐:
· [.NET]调用本地 Deepseek 模型
· 一个费力不讨好的项目,让我损失了近一半的绩效!
· .NET Core 托管堆内存泄露/CPU异常的常见思路
· PostgreSQL 和 SQL Server 在统计信息维护中的关键差异
· C++代码改造为UTF-8编码问题的总结
阅读排行:
· CSnakes vs Python.NET:高效嵌入与灵活互通的跨语言方案对比
· DeepSeek “源神”启动!「GitHub 热点速览」
· 我与微信审核的“相爱相杀”看个人小程序副业
· Plotly.NET 一个为 .NET 打造的强大开源交互式图表库
· 上周热点回顾(2.17-2.23)
历史上的今天:
2018-02-25 C# 同步转异步 AutoResetEvent
点击右上角即可分享
微信分享提示
剑桥
16:15发布
剑桥
16:15发布
7°
西南风
3级
空气质量
相对湿度
66%
今天
2°/10°
周三
2°/8°
周四
中雨
2°/7°