PPT 工具

如何使用 C# 將 PowerPoint 轉換為圖像

里根普恩
里根普恩
2024年3月12日
分享:

介紹

在軟體開發領域中,將PowerPoint簡報轉換為圖片格式的需求經常出現。 許多開發者發現,能夠以程式化的方式將 PowerPoint 文件轉換為照片非常有用,無論是用於生成預覽、創建縮圖,還是系統整合。 本文將說明如何使用 C# 進行 ppt 到圖像的轉換,並包括一些示例代碼來協助您完成。

如何使用 C# 將 PowerPoint 轉換為圖片

  1. 創建 PowerPoint 應用程式實例。

  2. 使用實例打開簡報。

  3. 檢查並建立輸出資料夾。

  4. 逐步浏览幻灯片并將幻灯片导出为图像。

  5. 關閉簡報並退出應用程式。

將 PowerPoint 簡報轉換為圖像格式?

在深入探討細節之前,讓我們快速看看將 PowerPoint 幻燈片轉換為照片的重要性。 即使 PowerPoint 是製作動態簡報的絕佳工具,但並不總是能以其原始格式分享這些文件。 有時,只需要從簡報中提取特定的幻燈片或照片,而其他時候,不同的系統和設置可能無法直接渲染PowerPoint文件。 將 PowerPoint 簡報轉換為圖片,提供一個易於在各種設備和應用程式上共享和查看的全方位解決方案。

使用 PowerPoint Interop 庫

有幾種方法可以在 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#)。

接下來,從可用的專案範本列表中選擇 "Console App" 或 "Console App (.NET Core)" 範本。

請完成「名稱」部分以為您的專案命名。

如何使用 C# 將 PowerPoint 轉換為圖像:圖 2 - 在創建新專案框中,選擇 C# 程式語言和 Console App。配置專案名稱和位置,然後點選「下一步」按鈕。

選擇專案的儲存位置。

點擊「Create」以開始創建新的主控臺應用程式專案。

如何使用 C# 將 PowerPoint 轉換為圖像:圖 3 - 選擇適當的 .NET Framework 並點擊“Create”按鈕。

將 PowerPoint 幻燈片轉換為 C# 中的圖像

讓我們開始看看如何使用 Microsoft.Office.Interop.PowerPoint 命名空間將 PowerPoint 幻燈片轉換為圖片。 首先確保安裝所需的組件並將其作為引用添加到您的 C# 專案中。這些組件通常可以通過直接參考 InterOp 組件或安裝 Microsoft Office 主互操作組件 (PIA) 來找到。

範例程式碼

using System.IO;
using Microsoft.Office.Interop.PowerPoint;
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);
    }
    static void ConvertPptToImages(string pptFilePath, string outputFolder)
    {
        Application pptApplication = new Application();
        Presentation pptPresentation = pptApplication.Presentations.Open(pptFilePath, MsoTriState.msoFalse, MsoTriState.msoFalse, MsoTriState.msoFalse);
        if (!Directory.Exists(outputFolder))
            Directory.CreateDirectory(outputFolder);
        int slidesCount = pptPresentation.Slides.Count;
        for (int i = 1; i <= slidesCount; i++)
        {
            string outputPath = Path.Combine(outputFolder, $"Slide{i}.png");
            pptPresentation.Slides[i].Export(outputPath, "png", 1024, 768);
        //saving the presentation slides into png images
        }
        pptPresentation.Close();
        pptApplication.Quit();
    }
}
using System.IO;
using Microsoft.Office.Interop.PowerPoint;
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);
    }
    static void ConvertPptToImages(string pptFilePath, string outputFolder)
    {
        Application pptApplication = new Application();
        Presentation pptPresentation = pptApplication.Presentations.Open(pptFilePath, MsoTriState.msoFalse, MsoTriState.msoFalse, MsoTriState.msoFalse);
        if (!Directory.Exists(outputFolder))
            Directory.CreateDirectory(outputFolder);
        int slidesCount = pptPresentation.Slides.Count;
        for (int i = 1; i <= slidesCount; i++)
        {
            string outputPath = Path.Combine(outputFolder, $"Slide{i}.png");
            pptPresentation.Slides[i].Export(outputPath, "png", 1024, 768);
        //saving the presentation slides into png images
        }
        pptPresentation.Close();
        pptApplication.Quit();
    }
}
Imports System.IO
Imports Microsoft.Office.Interop.PowerPoint
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)
	End Sub
	Private Shared Sub ConvertPptToImages(ByVal pptFilePath As String, ByVal outputFolder As String)
		Dim pptApplication As New Application()
		Dim pptPresentation As Presentation = pptApplication.Presentations.Open(pptFilePath, MsoTriState.msoFalse, MsoTriState.msoFalse, MsoTriState.msoFalse)
		If Not Directory.Exists(outputFolder) Then
			Directory.CreateDirectory(outputFolder)
		End If
		Dim slidesCount As Integer = pptPresentation.Slides.Count
		For i As Integer = 1 To slidesCount
			Dim outputPath As String = Path.Combine(outputFolder, $"Slide{i}.png")
			pptPresentation.Slides(i).Export(outputPath, "png", 1024, 768)
		'saving the presentation slides into png images
		Next i
		pptPresentation.Close()
		pptApplication.Quit()
	End Sub
End Class
$vbLabelText   $csharpLabel

要使用 PowerPoint 應用程式,需匯入的 C# 命名空間是使用 Microsoft.Office.Interop.PowerPoint; 宣告。 程式的入口點是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 提供幻燈片的總數。 我們使用輸出資料夾路徑和幻燈片索引來創建每張幻燈片圖片的輸出路徑(如Slides{i}.png)。 接下來,我們使用pptPresentation透過Export()功能將 PowerPoint 幻燈片匯出為圖片(在此範例中,以 JPG 圖像、PNG 圖像格式)。 參數是圖片格式(「png」格式)和大小(寬度:1024,高度:768)。 最後,我們使用pptPresentation來結束簡報。 關閉() 並使用 pptApplication 結束 PowerPoint 會話。 要適當釋放系統資源,請使用Quit()

輸出 - 將PowerPoint轉換為PNG圖像

如何使用 C# 將 PowerPoint 轉換為圖像:圖 6 - 將匯出的 PowerPoint 幻燈片轉換為 PNG 圖像輸出。

IronXL

一個受歡迎的 .NET 框架稱為IronXL,讓在 C# 中操作 Excel 文件變得更加容易。 憑藉其廣泛的讀取、創建和編輯 Excel 文件的功能集,這是一款適用於廣泛用途的靈活工具。 以下是 IronXL 的一些主要功能:

  • 開發人員可以快速將數據寫入新的或現有的 Excel 檔案,並使用IronXL從現有的 Excel 檔案中讀取數據。 這涵蓋訪問工作表和工作簿屬性,例如格式、公式和單元格值。
  • 使用 IronXL,開發人員可以將資料從各種來源(包括資料庫和 CSV 文件)匯入 Excel 試算表。 同樣地,Excel 文件中的資訊可以匯出為 CSV、HTML、XML 和 PDF 等其他檔案格式。
  • 開發人員可以使用IronXL動態添加、編輯和移除Excel電子表格中的工作表。 這根據應用需求提供數據組織和結構的靈活性。
  • IronXL 能夠精確地操作 Excel 試算表中的個別單元格。 從程式上,開發者可以設定格式、樣式、公式、儲存格值及其他特性。

    欲了解更多有關文檔的資訊,請參閱IronXL Documentation

安裝 IronXL

在繼續之前,讓我們先使用 NuGet 套件管理器主控台安裝 IronXL:

Install-Package IronXL.Excel

安裝完成後,IronXL可以在我們的C#項目中使用。

使用IronXL進行Excel操作

讓我們來檢視一個假設的情況,我們希望使用 IronXL 從 Excel 文件中讀取數據。

以下程式碼範例提供了如何完成此操作的快速說明:

using IronXL;
using System;
class Program
{
    static void Main(string [] args)
    {
        // Path to the Excel file
        string excelFilePath = "sample.xlsx";
        // Load the Excel file
        WorkBook workbook = WorkBook.Load(excelFilePath);
        // Access the first worksheet
        WorkSheet worksheet = workbook.WorkSheets[0];
        // Iterate through rows and columns to read data
        foreach (var row in worksheet.Rows)
        {
            foreach (var cell in row)
            {
                Console.Write(cell.Value + "\t");
            }
            Console.WriteLine();
        }
    }
}
using IronXL;
using System;
class Program
{
    static void Main(string [] args)
    {
        // Path to the Excel file
        string excelFilePath = "sample.xlsx";
        // Load the Excel file
        WorkBook workbook = WorkBook.Load(excelFilePath);
        // Access the first worksheet
        WorkSheet worksheet = workbook.WorkSheets[0];
        // Iterate through rows and columns to read data
        foreach (var row in worksheet.Rows)
        {
            foreach (var cell in row)
            {
                Console.Write(cell.Value + "\t");
            }
            Console.WriteLine();
        }
    }
}
Imports Microsoft.VisualBasic
Imports IronXL
Imports System
Friend Class Program
	Shared Sub Main(ByVal args() As String)
		' Path to the Excel file
		Dim excelFilePath As String = "sample.xlsx"
		' Load the Excel file
		Dim workbook As WorkBook = WorkBook.Load(excelFilePath)
		' Access the first worksheet
		Dim worksheet As WorkSheet = workbook.WorkSheets(0)
		' Iterate through rows and columns to read data
		For Each row In worksheet.Rows
			For Each cell In row
				Console.Write(cell.Value & vbTab)
			Next cell
			Console.WriteLine()
		Next row
	End Sub
End Class
$vbLabelText   $csharpLabel

首先,我們包含所需的命名空間。 IronXL 庫提供的類別和方法包含在 IronXL 命名空間中。 指定我們想要讀取的 Excel 文件的路徑(sample.xlsx)。 WorkBook 用於加載 Excel 文件。Excel 工作簿由 Load() 函數返回的 WorkBook 對象表示。 使用該活頁簿,我們可以使用 workbook.WorkSheets[0] 存取第一張工作表。 使用嵌套的 foreach 循環,我們遍歷工作表的行和列。 我們將每個單元格的值輸出到控制台。

如何使用 C# 將 PowerPoint 轉換為圖像:圖 7 - 控制台輸出。

要了解更多有關程式碼的信息,請參考IronXL Read Excel Examples

結論

在許多軟體應用中,需要使用C#將PowerPoint簡報轉換為照片。 使用或不使用 Microsoft.Office.Interop.PowerPoint 命名空間,該過程可能很快就能完成。 本文章的程式碼範例將幫助您輕鬆將 PowerPoint 轉換為圖片的功能納入 C# 應用程式中,創造大量資訊分發和修改的機會。

不需要在目標計算機上安裝 Excel 或依賴 Interop 庫,IronXL 提供了一種快速且有效的方法來在 C# 中執行 Excel 操作。 使用 IronXL 在 C# 應用程式中處理 Excel 資料的開發人員會發現它很有用,因為它通過其用戶友好的 API 和豐富的功能集簡化了讀取、寫入和更改 Excel 文件的操作。 IronXL 提供可靠的解決方案,可提高與 Excel 相關開發項目中的效率和適應性,無論您是在創建報告、處理數據,還是自動化電子表格作業。

可用的 IronXL 免費試用 包含豐富的功能和支援。 請訪問IronXL 授權資訊以獲取全面和最新的授權資訊。 請造訪Iron Software 網站以了解有關 Iron Software 產品的更多資訊。

里根普恩
軟體工程師
Regan 畢業於雷丁大學,擁有電子工程學士學位。在加入 Iron Software 之前,他的工作角色讓他專注於單一任務;而他在 Iron Software 工作中最喜歡的是他所能承擔的工作範圍,無論是增加銷售價值、技術支持、產品開發或市場營銷。他喜歡了解開發人員如何使用 Iron Software 庫,並利用這些知識不斷改進文檔和開發產品。
< 上一頁
如何使用 C# 創建 PowerPoint 演示文稿

準備開始了嗎? 版本: 2025.3 剛剛發布

查看許可證 >