如何使用 C# 將 PowerPoint 轉換為圖像
在軟體開發領域中,將PowerPoint簡報轉換成圖片格式的需求經常出現。 許多開發者發現能夠以程式方式將PowerPoint檔案轉換為照片非常有用,不論是為了預覽生成、縮圖建立,還是系統整合。 本文將說明如何使用C#將PPT轉換為圖片,並附上一些範例程式碼以幫助您。
如何使用C#將PowerPoint轉換為圖片
- 建立PowerPoint應用程式實例。
- 使用實例打開簡報。
- 檢查並建立輸出資料夾。
- 遍歷幻燈片並將幻燈片導出為圖片。
- 關閉簡報並退出應用程式。
將PowerPoint簡報轉換為圖片格式?
在進入詳細內容前,讓我們快速看一下將PowerPoint幻燈片轉換為照片的重要性。 儘管PowerPoint是製作動態簡報的好工具,但共享這些檔案並不總是能以原始格式進行。 有時候,只需要簡報中的特定幻燈片或照片,其他時候,不同的系統和設置可能無法直接呈現PowerPoint檔案。 將PowerPoint簡報轉換成圖片提供了一個全方位的解決方案,容易在各類裝置和應用中分享和查看。
Using PowerPoint Interop Library
有幾種方法可以在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在您的PC上。
啟動一個新專案:
選擇檔案、新建,最終選擇專案。

在"建立新專案"框中,從左側選擇您喜愛的程式語言(例如C#)。
接下來,從可用的專案模板列表中選擇"Console App"或"Console App (.NET Core)"模板。
請完成"名稱"部分為專案命名。

選擇專案的儲存位置。
單擊"建立"以開始工作在一個新的控制台應用程式專案上。

Convert PowerPoint Slides to Images in C
讓我們開始看一下如何使用Microsoft.Office.Interop.PowerPoint命名空間將PowerPoint幻燈片轉換為圖片。 首先,確保所需的程式集已安裝並作為引用新增到您的C#專案中。這些程式集通常是通過直接引用InterOp程式集或安裝Microsoft Office Primary Interop Assemblies(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
透過使用Microsoft.Office.Interop.PowerPoint;聲明進口用於處理PowerPoint應用程式的C#命名空間。 程式的進入點是Main方法。 它指定輸出文件夾(pptFilePath)的路徑。 此方法負責將PowerPoint簡報實際轉換為照片。
PowerPoint簡報檔案

Application pptApplication = new Application();用於啟動PowerPoint程式的新實例。 這允許程式化地與PowerPoint進行互動。 使用pptFilePath.Open()函式指定的PowerPoint簡報檔案。 該函式返回一個Presentation物件,代表打開的簡報。 我們判斷輸出資料夾"outputFolder"是否存在。 如果不存在,我們使用方法Directory.CreateDirectory()建立它。
控制台輸出

我們使用for迴圈遍歷簡報中的每張幻燈片。 Slides.Count提供幻燈片總數。 我們使用輸出資料夾路徑和幻燈片索引為每張幻燈片的圖片建立輸出路徑(如Slide{i}.png)。 接下來,我們使用Export()函式。 參數為圖片格式("png"格式)和大小(寬度:1024,高度:768)。 最後,我們使用pptApplication.Quit()結束PowerPoint會話。 適當釋放系統資源,請使用Quit()。
輸出 - 將PowerPoint轉換為PNG圖片

IronPPT
IronPPT是Iron Software提供的專用.NET程式庫,用於使用C#或VB.NET處理PowerPoint(PPT/PPTX)檔案——無需Microsoft Office或Office Interop組件。
主要特點
- 無需Office的PowerPoint處理:在任何.NET平台上載入、編輯或建立
.ppt)檔案——無需安裝PowerPoint,適用於Windows、macOS、Linux、Docker或Azure。 - 幻燈片型別和佈局控制,包括大小、方向、背景和母板佈局。
- 豐富的內容支持:新增和樣式化文字(字體、大小、顏色、對齊)、繪製形狀、插入圖片和配置圖表或表格,所有這些都使用流暢的API。
- 高保真圖像導出:每個
presentation.Save("Slide1.png", width:1200, height:800))。 - 支持多個.NET版本:支持.NET Framework 4.6.2+、.NET Core 3.1、.NET 5–9 和.NET 6/7/8,適用於Azure或容器環境。
- 伺服器安全性和執行緒友好:理想用於背景服務、web API或CI/CD工作負載。
安裝IronPPT
使用以下任一方式將NuGet包新增到您的專案中:
Install-Package IronPPT
或透過Visual Studio的NuGet套件管理器GUI(搜索"IronPPT")。 安裝後,透過新增:
using IronPPT;
using IronPPT;
Imports IronPPT
為了解鎖完整功能,請設置您的授權金鑰或使用免費的30天試用金鑰:
IronPPT.License.LicenseKey = "YOUR_LICENSE_KEY";
IronPPT.License.LicenseKey = "YOUR_LICENSE_KEY";
IronPPT.License.LicenseKey = "YOUR_LICENSE_KEY"
使用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()
這種方法完全避免了COM。 IronPPT在內部處理分頁、矢量縮放和圖片渲染,因此您的圖片與PowerPoint的外觀和感覺一致。
對於更高級的使用,您可以控制幻燈片順序、重複使用模板、新增表格或圖表,或插入自定義SVG/矢量圖片(請參見詳細的API參考,了解完整的類和方法分解)。
結論
在許多現代.NET應用中,將PowerPoint簡報轉換為圖片是必需的——用於文件預覽、自動化報告或後期處理。 您可以使用Microsoft Office Interop組件來完成此任務,但它帶來了許多限制——如Office安裝、穩定性問題、授權問題和平台限制。
相反,IronPPT提供了一個功能齊全、高性能且跨平台的API,用於將.pptx檔案轉換為圖片——從單個幻燈片到整個幻燈片組。無論您是在桌面客戶端、web API還是無頭伺服器環境運行,IronPPT無需Office即可提供相同的保真度和控制。用IronPPT替換上面的Interop-based程式碼,開始更快、更可靠地生成PowerPoint預覽,並擁有完整的.NET控制權。
存取IronPPTAPI參考或查看詳細的程式碼範例(包括它們的llms.txt索引)以探索更多功能。 提供免費試用——立即試用並將PowerPoint-圖片轉換新增到您的.NET工具包中!


