PPT 工具 如何使用 C# 將 PowerPoint 轉換為圖像 Jordi Bardia 更新:2026年1月18日 下載 IronPPT NuGet 下載 開始免費試用 LLM副本 LLM副本 將頁面複製為 Markdown 格式,用於 LLMs 在 ChatGPT 中打開 請向 ChatGPT 諮詢此頁面 在雙子座打開 請向 Gemini 詢問此頁面 在 Grok 中打開 向 Grok 詢問此頁面 打開困惑 向 Perplexity 詢問有關此頁面的信息 分享 在 Facebook 上分享 分享到 X(Twitter) 在 LinkedIn 上分享 複製連結 電子郵件文章 在軟體開發領域,經常需要將PowerPoint簡報轉換為圖片格式。 許多開發人員發現,能夠以程式設計方式將 PowerPoint 檔案轉換為照片非常有用,無論是用於產生預覽、建立縮圖還是系統整合。 本文將解釋如何使用 C# 將 ppt 轉換為圖像,並提供一些範例程式碼來幫助您完成操作。 隆重介紹IronPPT : Iron Software出品的.NET PowerPoint 庫IronPPT 可無縫載入並儲存 PPTX 文件,無需 Microsoft Office。它非常適合在任何.NET應用程式中自動執行幻燈片、文字、形狀和圖像的操作。立即開始使用 IronPPT! 如何使用 C# 將 PowerPoint 轉換為圖像 建立 PowerPoint 應用程式實例。 使用實例開啟簡報。 檢查並建立輸出資料夾。 遍歷投影片並將投影片匯出為影像。 關閉簡報並退出應用程式。 將 PowerPoint 簡報轉換為影像格式? 在深入探討具體細節之前,讓我們先快速了解將 PowerPoint 投影片轉換為照片的意義。 儘管 PowerPoint 是製作動態簡報的絕佳工具,但以原始格式共用這些文件並不總是可行的。 有時,只需要從簡報中截取特定的幻燈片或照片;而有時,不同的系統和設定可能無法直接渲染 PowerPoint 文件。 將 PowerPoint 簡報轉換為圖片,即可提供全面的解決方案,方便在各種裝置和應用程式上共用和檢視。 使用 PowerPoint 互通庫 在 C# 中,有幾種方法可以將 PowerPoint 簡報轉換為照片。 使用[Microsoft.Office.Interop.PowerPoint](https://learn.microsoft.com/en-us/previous-versions/office/office-12/ff761925(v=office.12)命名空間(它提供了以程式設計方式與 PowerPoint 應用程式互動的類別和方法)是一種流行的方法。 這為處理 PowerPoint 文件提供了強大的功能。 建立一個新的 Visual Studio 項目 請依照下列步驟建立一個新的 Visual Studio 專案: 開啟 Visual Studio IDE。請確保在使用前已在您的電腦上安裝了Visual Studio 。 啟動新專案: 選擇"檔案"、"新建",最後選擇"專案"。 在"建立新專案"方塊中,從左側選擇您喜歡的程式語言(例如 C#)。 接下來,從可用項目範本清單中選擇"控制台應用程式"或"控制台應用程式(.NET Core)"範本。 請在"名稱"欄中填寫項目名稱。 選擇項目的儲存位置。 點擊"建立"開始建立新的控制台應用程式專案。 Convert PowerPoint Slides to Images in C 讓我們先來看看如何使用 Microsoft.Office.Interop.PowerPoint 命名空間將 PowerPoint 投影片轉換為圖片。 請先確保所需的程序集已安裝並新增至您的 C# 專案中作為引用。這些組件通常可以透過直接引用 InterOp 組件或安裝 Microsoft Office 主互通組件 (PIA) 來找到。 程式碼範例 using System.IO; // Import System.IO namespace for file handling using Microsoft.Office.Interop.PowerPoint; // Import Interop PowerPoint namespace class Program { static void Main(string[] args) { string pptFilePath = "demo.pptx"; // Path to your PowerPoint file string outputFolder = "output_images"; // Output folder path where images will be saved ConvertPptToImages(pptFilePath, outputFolder); // Convert PowerPoint slides to images } static void ConvertPptToImages(string pptFilePath, string outputFolder) { Application pptApplication = new Application(); // Create a new PowerPoint application instance Presentation pptPresentation = pptApplication.Presentations.Open(pptFilePath, MsoTriState.msoFalse, MsoTriState.msoFalse, MsoTriState.msoFalse); // Open the PowerPoint presentation if (!Directory.Exists(outputFolder)) // Check if the output folder exists Directory.CreateDirectory(outputFolder); // Create the output folder if it doesn't exist int slidesCount = pptPresentation.Slides.Count; // Get the number of slides in the presentation for (int i = 1; i <= slidesCount; i++) // Iterate through all slides { string outputPath = Path.Combine(outputFolder, $"Slide{i}.png"); // Set the output path for the current slide pptPresentation.Slides[i].Export(outputPath, "png", 1024, 768); // Export slide to PNG format } pptPresentation.Close(); // Close the PowerPoint presentation pptApplication.Quit(); // Quit the PowerPoint application } } using System.IO; // Import System.IO namespace for file handling using Microsoft.Office.Interop.PowerPoint; // Import Interop PowerPoint namespace class Program { static void Main(string[] args) { string pptFilePath = "demo.pptx"; // Path to your PowerPoint file string outputFolder = "output_images"; // Output folder path where images will be saved ConvertPptToImages(pptFilePath, outputFolder); // Convert PowerPoint slides to images } static void ConvertPptToImages(string pptFilePath, string outputFolder) { Application pptApplication = new Application(); // Create a new PowerPoint application instance Presentation pptPresentation = pptApplication.Presentations.Open(pptFilePath, MsoTriState.msoFalse, MsoTriState.msoFalse, MsoTriState.msoFalse); // Open the PowerPoint presentation if (!Directory.Exists(outputFolder)) // Check if the output folder exists Directory.CreateDirectory(outputFolder); // Create the output folder if it doesn't exist int slidesCount = pptPresentation.Slides.Count; // Get the number of slides in the presentation for (int i = 1; i <= slidesCount; i++) // Iterate through all slides { string outputPath = Path.Combine(outputFolder, $"Slide{i}.png"); // Set the output path for the current slide pptPresentation.Slides[i].Export(outputPath, "png", 1024, 768); // Export slide to PNG format } pptPresentation.Close(); // Close the PowerPoint presentation pptApplication.Quit(); // Quit the PowerPoint application } } $vbLabelText $csharpLabel 使用 Microsoft.Office.Interop.PowerPoint; 聲明匯入與 PowerPoint 應用程式互動所需的 C# 命名空間。 程式的入口點是 Main 方法。 它指定輸出資料夾(outputFolder),即保存建立的照片的位置,以及 PowerPoint 文件的路徑(pptFilePath)。 此方法負責將 PowerPoint 簡報實際轉換為照片。 PowerPoint簡報文件 用於啟動 PowerPoint 程式的新實例的命令是 Application pptApplication = new Application();。 這使得可以透過程式與 PowerPoint 進行互動。 使用 pptApplication.Presentations,我們開啟由 pptFilePath.Open() 函數指定的 PowerPoint 簡報檔案。 此函數傳回一個 Presentation 對象,該對象表示已開啟的簡報。 我們確定輸出資料夾"outputFolder"是否存在。 如果沒有,我們使用方法 Directory.CreateDirectory() 來創建它。 控制台輸出 我們使用 for 迴圈來遍歷簡報中的每一張投影片。 pptPresentation 提供使用屬性 Slides.Count 的投影片總數。 我們使用輸出資料夾路徑和幻燈片索引來建立每個幻燈片圖片的輸出路徑(如 Slide{i}.png)。 接下來,我們使用 pptPresentation 將 PowerPoint 投影片匯出為圖片(在本例中為 PNG 圖片格式),使用 Export() 函數。 參數為圖片格式("png"格式)和尺寸(寬度:1024,高度:768)。 最後,我們使用 pptPresentation.Close() 結束演示文稿,使用 pptApplication.Quit() 結束 PowerPoint 會話。 若要適當地釋放系統資源,請使用 Quit()。 輸出 - 將 PowerPoint 檔案轉換為 PNG 映像 IronPPT IronPPT 是Iron Software開發的專用.NET庫,用於使用 C# 或 VB .NET處理 PowerPoint (PPT/PPTX) 文件,而無需Microsoft Office 或 Office Interop 元件。 主要特點 *無需 Office 即可處理 PowerPoint:*在任何.NET平台(Windows、macOS、Linux、Docker 或 Azure)上載入、編輯或建立 .pptx(和 .ppt)文件,而無需安裝 PowerPoint。 投影片類型和版面控制,包括大小、方向、背景和母版佈局。 豐富的內容支援:新增和設定文字樣式(字體、大小、顏色、對齊方式)、繪製形狀、插入圖像以及配置圖表或表格——所有這些都可透過流暢的 API 實現。 高保真圖像導出:**每個 Slide 都可以使用 Save() 或 Export() 方法以自訂解析度儲存為 PNG 或 JPEG(例如 presentation.Save("Slide1.png", width:1200, height:800))。 支援多個.NET版本:Azure 或容器環境中的.NET Framework 4.6.2+、. .NET Core 3.1、 .NET 5–9 和.NET 6/7/8。 *伺服器安全且執行緒友好:非常適合後台服務、Web API 或 CI/CD 工作負載。 安裝 IronPPT 使用以下任一方法將NuGet套件新增至您的專案: Install-Package IronPPT 或透過 Visual Studio 的NuGet套件管理器 GUI(搜尋"IronPPT")。 安裝完成後,透過新增以下內容匯入: using IronPPT; using IronPPT; $vbLabelText $csharpLabel 若要解鎖全部功能,請設定您的許可證金鑰或使用免費的 30 天試用金鑰: IronPPT.License.LicenseKey = "YOUR_LICENSE_KEY"; IronPPT.License.LicenseKey = "YOUR_LICENSE_KEY"; $vbLabelText $csharpLabel 使用 IronPPT 將 PowerPoint 投影片轉換為影像 使用 IronPPT,將投影片轉換為影像既簡單又方便。以下是一個地道的 C# 範例,展示了具體操作方法: using IronPPT; using System.IO; // Optional: apply the license key // IronPPT.License.LicenseKey = "YOUR_LICENSE_KEY"; var presentation = PresentationDocument.Load("input.pptx"); if (!Directory.Exists("images")) Directory.CreateDirectory("images"); for (int i = 0; i < presentation.Slides.Count; i++) { var slide = presentation.Slides[i]; string filePath = Path.Combine("images", $"slide{i+1}.png"); slide.SaveAsImage(filePath, width: 1024, height: 768); } presentation.Close(); using IronPPT; using System.IO; // Optional: apply the license key // IronPPT.License.LicenseKey = "YOUR_LICENSE_KEY"; var presentation = PresentationDocument.Load("input.pptx"); if (!Directory.Exists("images")) Directory.CreateDirectory("images"); for (int i = 0; i < presentation.Slides.Count; i++) { var slide = presentation.Slides[i]; string filePath = Path.Combine("images", $"slide{i+1}.png"); slide.SaveAsImage(filePath, width: 1024, height: 768); } presentation.Close(); $vbLabelText $csharpLabel 這種方法完全避免了COM問題。 IronPPT 在內部處理分頁、向量縮放和圖像渲染,因此您的圖像與 PowerPoint 的外觀和感覺相符。 對於更進階的用法,您可以控制投影片順序、重複使用範本、新增表格或圖表,或插入自訂 SVG/向量圖像(有關完整的類別和方法細分,請參閱詳細的 API 參考)。 結論 在許多現代.NET應用程式中,將 PowerPoint 簡報轉換為圖像是必不可少的——用於文件預覽、自動報告或下游處理。 您可以使用 Microsoft Office Interop 元件來完成此任務,但這會帶來許多限制——Office 安裝、穩定性問題、授權問題和平台限制。 IronPPT 提供了一個功能齊全、高效且跨平台的 API,用於將 .pptx 檔案轉換為圖像——從單張幻燈片到整個簡報均可轉換。無論您是在桌面用戶端、Web API 還是無頭伺服器環境中操作,IronPPT 都能提供相同的保真度和控制力,而無需 Office。將上述基於 Interop 的程式碼替換為 IronPPT,即可開始更快、更可靠地產生 PowerPoint 預覽,並完全掌控.NET 的控制。 造訪 IronPPT API 參考或查看詳細的程式碼範例(包括其llms.txt索引)以探索更多功能。 提供免費試用版-立即試用,將 PowerPoint 轉圖像功能新增至您的.NET工具包! Jordi Bardia 立即與工程團隊聊天 軟體工程師 Jordi 在 Python、C# 和 C++ 上最得心應手,當他不在 Iron Software 展現技術時,便在做遊戲編程。在分担產品测测试,產品開發和研究的责任時,Jordi 為持续的產品改進增值。他说这种多样化的经验使他受到挑战并保持参与, 而这也是他与 Iron Software 中工作一大乐趣。Jordi 在佛罗里达州迈阿密长大,曾在佛罗里达大学学习计算机科学和统计学。 相關文章 更新2026年1月18日 如何使用 C# 程式化創建和自動化 PowerPoint 演示文稿 通過此教程學習如何使用 C# 自動創建 PowerPoint。掌握 interop 庫以簡化演示設計和自定義。 閱讀更多 如何使用 C# 程式化創建和自動化 PowerPoint 演示文稿
更新2026年1月18日 如何使用 C# 程式化創建和自動化 PowerPoint 演示文稿 通過此教程學習如何使用 C# 自動創建 PowerPoint。掌握 interop 庫以簡化演示設計和自定義。 閱讀更多