PPT转换图片
1、转换步骤
(1) 引入COM组件Microsoft.Office.Interop.PowerPoint;
(2) 创建Microsoft.Office.Interop.PowerPoint.Application;
(3) 打开PPT文档Application.Presentations.Open;
(4) 根据PPT宽高比(PageSetup.SlideWidth、PageSetup.SlideHeight)设置合适的宽高;
(5) 遍历幻灯片集合Presentation.Slides;
(6) 使用幻灯片(Slide)的Export方法导出图片。
2、注意事项
(1) PPT导出图片的过程是阻塞执行的,可根据情况将每一页的导出作为原子操作,实现异步执行;
(2) 打开PPT的路径不能使用“/”,应将“/”替换为“\”;
(3) 保存的PPT每一页图片路径同上处理。
3、示例代码
(1) PptTool.cs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 | using System; using Microsoft.Office.Interop.PowerPoint; using System.Drawing.Imaging; using System.IO; using Microsoft.Office.Core; namespace OfficeTools.Tools { public class PptTool { private Application _app; public void Init() { _app = new Application { DisplayAlerts = PpAlertLevel.ppAlertsNone }; } public void UnInit() { try { _app?.Quit(); } catch (Exception e) { Console.WriteLine(e); } _app = null ; GC.Collect(); } public bool Convert2Images( string filePath, string imageFolder, ImageFormat imageFormat, int width, ref int height) { //正斜杠替换为反斜杠 filePath = filePath.Replace( '/' , '\\' ); if (!File.Exists(filePath)) { return false ; } Presentation presentation = null ; try { var fileName = Path.GetFileNameWithoutExtension(filePath); imageFolder = imageFolder.Replace( '/' , '\\' ); imageFolder = imageFolder.TrimEnd( '\\' ); imageFolder += $ "\\{fileName}" ; if (!Directory.Exists(imageFolder)) { Directory.CreateDirectory(imageFolder); } //以非只读方式打开,方便操作结束后保存 presentation = _app.Presentations.Open( filePath, MsoTriState.msoTrue, //ReadOnly: true MsoTriState.msoTrue, //Untitled: true MsoTriState.msoFalse); //WithWindow: false //获取真实分辨率及其比率 //SlideWidth:幻灯片的宽度(以磅为单位) //SlideHeight:幻灯片的高度(以磅为单位) var slideWidth = presentation.PageSetup.SlideWidth; var slideHeight = presentation.PageSetup.SlideHeight; height = ( int ) (width * slideHeight / slideWidth); var suffix = imageFormat.ToString().ToLower(); var index = 0; foreach (Slide slide in presentation.Slides) { var newFileName = $ "{imageFolder}\\{++index}.{suffix}" ; slide.Export(newFileName, suffix, width, height); } } catch (Exception e) { Console.WriteLine(e); return false ; } finally { presentation?.Close(); } return true ; } } } |
(2) 调用代码
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | public void Test() { var pptTool = new PptTool(); pptTool.Init(); var filePath = @"D:\test.pptx" ; var width = 1920; var height = width; pptTool.Convert2Images(filePath, Environment.CurrentDirectory, ImageFormat.Png, width, ref height); pptTool.UnInit(); } |
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】博客园社区专享云产品让利特惠,阿里云新客6.5折上折
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 没有源码,如何修改代码逻辑?
· 一个奇形怪状的面试题:Bean中的CHM要不要加volatile?
· [.NET]调用本地 Deepseek 模型
· 一个费力不讨好的项目,让我损失了近一半的绩效!
· .NET Core 托管堆内存泄露/CPU异常的常见思路
· DeepSeek “源神”启动!「GitHub 热点速览」
· 微软正式发布.NET 10 Preview 1:开启下一代开发框架新篇章
· C# 集成 DeepSeek 模型实现 AI 私有化(本地部署与 API 调用教程)
· DeepSeek R1 简明指南:架构、训练、本地部署及硬件要求
· NetPad:一个.NET开源、跨平台的C#编辑器