在生產環境中測試,無水印。
在任何需要的地方都能運行。
獲得 30 天的全功能產品。
在幾分鐘內上手運行。
試用產品期間完全訪問我們的支援工程團隊
在軟體開發領域中,將PowerPoint簡報轉換為圖片格式的需求經常出現。 許多開發者發現,能夠以程式化的方式將 PowerPoint 文件轉換為照片非常有用,無論是用於生成預覽、創建縮圖,還是系統整合。 本文將說明如何使用 C# 進行 ppt 到圖像的轉換,並包括一些示例代碼來協助您完成。
創建 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 IDE。在使用之前,確保您已在您的電腦上安裝Visual Studio。
啟動新專案:
選擇檔案、新建,然後選擇專案。
在“創建新項目”框中,從左側選擇您喜愛的編程語言(例如 C#)。
接下來,從可用的專案範本列表中選擇 "Console App" 或 "Console App (.NET Core)" 範本。
請完成「名稱」部分以為您的專案命名。
選擇專案的儲存位置。
點擊「Create」以開始創建新的主控臺應用程式專案。
讓我們開始看看如何使用 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
要使用 PowerPoint 應用程式,需匯入的 C# 命名空間是使用 Microsoft.Office.Interop.PowerPoint;
宣告。 程式的入口點是Main
方法。 它指定了輸出資料夾(outputFolder
),也就是創建的圖片將被保存的位置,以及 PowerPoint 文件的路徑(pptFilePath
)。 此方法負責將 PowerPoint 簡報實際轉換成照片。
Application pptApplication = new Application();
用於啟動 PowerPoint 程式的新實例。 這使與 PowerPoint 的程式交互成為可能。 使用pptApplication.Presentations
,我們通過pptFilePath.Open()
函數打開指定的PowerPoint演示文稿檔案。 此函數返回一個 Presentation 物件,代表打開的簡報。 我們確定是否存在輸出資料夾「outputFolder
」。 如果沒有,我們使用方法 Directory.CreateDirectory()
來創建它。
我們使用 for 迴圈來遍歷簡報中的每一張幻燈片。 pptPresentation
使用屬性 Slides.Count
提供幻燈片的總數。 我們使用輸出資料夾路徑和幻燈片索引來創建每張幻燈片圖片的輸出路徑(如Slides{i}.png
)。 接下來,我們使用pptPresentation
透過Export()
功能將 PowerPoint 幻燈片匯出為圖片(在此範例中,以 JPG 圖像、PNG 圖像格式)。 參數是圖片格式(「png」格式)和大小(寬度:1024,高度:768)。 最後,我們使用pptPresentation
來結束簡報。 關閉() 並使用 pptApplication
結束 PowerPoint 會話。 要適當釋放系統資源,請使用Quit()
。
一個受歡迎的 .NET 框架稱為IronXL,讓在 C# 中操作 Excel 文件變得更加容易。 憑藉其廣泛的讀取、創建和編輯 Excel 文件的功能集,這是一款適用於廣泛用途的靈活工具。 以下是 IronXL 的一些主要功能:
IronXL 能夠精確地操作 Excel 試算表中的個別單元格。 從程式上,開發者可以設定格式、樣式、公式、儲存格值及其他特性。
欲了解更多有關文檔的資訊,請參閱IronXL Documentation。
在繼續之前,讓我們先使用 NuGet 套件管理器主控台安裝 IronXL:
Install-Package IronXL.Excel
安裝完成後,IronXL可以在我們的C#項目中使用。
讓我們來檢視一個假設的情況,我們希望使用 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
首先,我們包含所需的命名空間。 IronXL 庫提供的類別和方法包含在 IronXL 命名空間中。 指定我們想要讀取的 Excel 文件的路徑(sample.xlsx)。 WorkBook 用於加載 Excel 文件。Excel 工作簿由 Load() 函數返回的 WorkBook 對象表示。 使用該活頁簿,我們可以使用 workbook.WorkSheets[0]
存取第一張工作表。 使用嵌套的 foreach 循環,我們遍歷工作表的行和列。 我們將每個單元格的值輸出到控制台。
要了解更多有關程式碼的信息,請參考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 產品的更多資訊。