PPT 工具 如何使用 C# 將 PowerPoint 轉換成圖片 Jordi Bardia 更新:8月 3, 2025 下載 IronPPT NuGet 下載 開始免費試用 法學碩士副本 法學碩士副本 將頁面複製為 Markdown 格式,用於 LLMs 在 ChatGPT 中打開 請向 ChatGPT 諮詢此頁面 在雙子座打開 請向 Gemini 詢問此頁面 在雙子座打開 請向 Gemini 詢問此頁面 打開困惑 向 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# 將 PowerPoint 轉換為映像:圖 1 - 開啟 Visual Studio 並選擇檔案 - 新建 - 專案。 在"建立新專案"方塊中,從左側選擇您喜歡的程式語言(例如 C#)。 接下來,從可用項目範本清單中選擇"控制台應用程式"或"控制台應用程式 (.NET Core)"範本。 請填寫"名稱"一欄,為您的專案命名。 如何使用 C# 將 PowerPoint 轉換為圖像:圖 2 - 從"建立新專案"方塊中,選擇 C# 程式語言和控制台應用程式。配置項目名稱和位置,然後按一下"下一步"按鈕。 選擇項目的儲存位置。 點擊"建立"開始建立新的控制台應用程式專案。 如何使用 C# 將 PowerPoint 轉換為映像:圖 3 - 選擇適當的 .NET Framework,然後按一下"建立"按鈕。 使用 C# 將 PowerPoint 投影片轉換為影像 讓我們先來看看如何使用 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 } } Imports System.IO ' Import System.IO namespace for file handling Imports Microsoft.Office.Interop.PowerPoint ' Import Interop PowerPoint namespace Friend Class Program Shared Sub Main(ByVal args() As String) Dim pptFilePath As String = "demo.pptx" ' Path to your PowerPoint file Dim outputFolder As String = "output_images" ' Output folder path where images will be saved ConvertPptToImages(pptFilePath, outputFolder) ' Convert PowerPoint slides to images End Sub Private Shared Sub ConvertPptToImages(ByVal pptFilePath As String, ByVal outputFolder As String) Dim pptApplication As New Application() ' Create a new PowerPoint application instance Dim pptPresentation As Presentation = pptApplication.Presentations.Open(pptFilePath, MsoTriState.msoFalse, MsoTriState.msoFalse, MsoTriState.msoFalse) ' Open the PowerPoint presentation If Not Directory.Exists(outputFolder) Then ' Check if the output folder exists Directory.CreateDirectory(outputFolder) ' Create the output folder if it doesn't exist End If Dim slidesCount As Integer = pptPresentation.Slides.Count ' Get the number of slides in the presentation For i As Integer = 1 To slidesCount ' Iterate through all slides Dim outputPath As String = 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 Next i pptPresentation.Close() ' Close the PowerPoint presentation pptApplication.Quit() ' Quit the PowerPoint application End Sub End Class $vbLabelText $csharpLabel 使用Microsoft.Office.Interop.PowerPoint;聲明匯入與 PowerPoint 應用程式互動所需的 C# 命名空間。 該程式的入口點是Main方法。 它指定輸出資料夾( outputFolder ),即儲存建立的照片的位置,以及 PowerPoint 檔案的路徑( pptFilePath )。 此方法負責將 PowerPoint 簡報實際轉換為照片。 PowerPoint簡報文件 如何使用 C# 將 PowerPoint 轉換為圖像:圖 4 - 用於程式碼範例的 PowerPoint ppt。 Application pptApplication = new Application();用於啟動 PowerPoint 程式的新實例。 這使得可以透過程式與 PowerPoint 進行互動。 使用pptApplication.Presentations ,我們開啟由pptFilePath.Open()函數指定的 PowerPoint 簡報檔案。 此函數傳回一個 Presentation 對象,該對象表示已開啟的簡報。 我們確定輸出資料夾" outputFolder "是否存在。 如果不存在,我們使用Directory.CreateDirectory()方法來建立它。 控制台輸出 如何使用 C# 將 PowerPoint 轉換為圖片:圖 5 - 控制台輸出。 我們使用 for 迴圈來遍歷簡報中的每一張投影片。 pptPresentation使用屬性Slides.Count提供總幻燈片。 我們使用輸出資料夾路徑和幻燈片索引來建立每個幻燈片圖片的輸出路徑(例如Slide{i}.png )。 接下來,我們使用pptPresentation Export()函數將 PowerPoint 投影片匯出為圖片(在本例中為 PNG 圖片格式)。 參數為圖片格式("png"格式)和尺寸(寬度:1024,高度:768)。 最後,我們使用pptPresentation.Close()結束演示文稿,使用pptApplication.Quit()結束 PowerPoint 會話。 若要適當地釋放系統資源,請使用Quit() 。 輸出 - 將 PowerPoint 檔案轉換為 PNG 映像 如何使用 C# 將 PowerPoint 轉換為圖片:圖 6 - 將 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 實現。 高保真影像匯出**:可以使用Save()或Export()方法將每個Slide儲存為自訂解析度的 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; Imports IronPPT $vbLabelText $csharpLabel 若要解鎖全部功能,請設定您的許可證金鑰或使用免費的 30 天試用金鑰: IronPPT.License.LicenseKey = "YOUR_LICENSE_KEY"; 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(); Imports IronPPT Imports System.IO ' Optional: apply the license key ' IronPPT.License.LicenseKey = "YOUR_LICENSE_KEY"; Private presentation = PresentationDocument.Load("input.pptx") If Not Directory.Exists("images") Then Directory.CreateDirectory("images") End If For i As Integer = 0 To presentation.Slides.Count - 1 Dim slide = presentation.Slides(i) Dim filePath As String = Path.Combine("images", $"slide{i+1}.png") slide.SaveAsImage(filePath, width:= 1024, height:= 768) Next i 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 在佛罗里达州迈阿密长大,曾在佛罗里达大学学习计算机科学和统计学。 相關文章 更新9月 18, 2025 如何以 C# 程式化方式建立並自動化 PowerPoint 簡報 透過本教程學習使用 C# 自動建立 PowerPoint。掌握互操作函式庫以簡化簡報設計和客製化。 閱讀更多 如何以 C# 程式化方式建立並自動化 PowerPoint 簡報
更新9月 18, 2025 如何以 C# 程式化方式建立並自動化 PowerPoint 簡報 透過本教程學習使用 C# 自動建立 PowerPoint。掌握互操作函式庫以簡化簡報設計和客製化。 閱讀更多