PDF 工具

如何使用C#将PowerPoint转换为图像

发布 2024年三月12日
分享:

简介

转换的必要性 PowerPoint 将演示文稿转换为图片格式的情况经常发生在软件开发领域。许多开发人员发现,无论是预览生成、缩略图创建还是系统集成,以编程方式将 PowerPoint 文件转换为图片都非常有用。本文将解释如何使用 C# 将 ppt 转换为图片来完成这一操作,并包含一些示例代码,希望对您有所帮助。

如何使用 C# 将 PowerPoint 转换为图像

1.创建 PowerPoint 应用程序实例。

2.使用该实例打开演示文稿。

3.检查并创建输出文件夹。

4.迭代幻灯片并将幻灯片导出为图像。

5.关闭演示并退出应用程序

将 PowerPoint 演示文稿转换为图像格式?

在了解具体细节之前,让我们先快速了解一下将 PowerPoint 幻灯片转换成照片的意义。尽管 PowerPoint 是制作动态演示文稿的绝佳工具,但并不是总能以原始格式共享这些文件。有时,只需要演示文稿中的特定幻灯片或照片,有时,不同的系统和设置可能无法直接呈现 PowerPoint 文件。通过将 PowerPoint 演示文稿转换成图片,就可以提供一个全方位的解决方案,让您可以在各种设备和应用程序上轻松共享和查看。

使用 PowerPoint Intrope 库

用 C# 将 PowerPoint 演示文稿转换成照片有几种方法。使用 Microsoft.Office.Interop.PowerPoint 另一种流行的方法是使用 "PowerPoint 名称空间",它提供了与 PowerPoint 应用程序进行编程接口的类和方法。

创建一个新的 Visual Studio 项目

按照以下步骤创建新的 Visual Studio 项目:

打开 Visual Studio IDE。确保已安装 Visual Studio 在使用之前,请先在个人电脑上进行设置。

启动新项目:

选择文件、新建,最后选择项目。

如何使用 C# 将 PowerPoint 转换为图像:图 1 - 打开 Visual Studio 并选择文件 - 新建 - 项目。

在 "创建新项目 "框中,选择你喜欢的编程语言 (例如 C#) 从左侧

然后,选择 "控制台应用程序 "或 "控制台应用程序 (.NET Core)"模板。

请填写 "名称 "部分,为项目命名。

如何使用 C# 将 PowerPoint 转换为图像:图 2 - 在创建新项目框中,选择 C# 编程语言和控制台应用程序。配置项目名称和位置,然后点击"Next"按钮。

选择项目的存储位置。

点击 "创建",开始创建新的控制台应用程序项目。

如何使用 C# 将 PowerPoint 转换为图像:图 3 - 选择适当的 .NET Framework 并单击 "Create" 按钮。

将 PowerPoint 幻灯片转换为 C&num 中的图像;

首先,让我们看看如何使用 Microsoft.Office.Interop.PowerPoint 命名空间将 PowerPoint 幻灯片转换为图片。首先确保安装了所需的程序集,并将其作为引用添加到 C# 项目中。这些程序集通常可以通过直接引用 InterOp 程序集或安装 Microsoft Office Primary Interop Assemblies 来找到。 (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
VB   C#

使用 Microsoft.Office.Interop.PowerPoint; 声明导入了使用 PowerPoint 应用程序所需的 C# 命名空间。程序的入口是 Main 方法。它指定了输出文件夹 (输出文件夹)保存已创建照片的位置,以及 PowerPoint 文件的路径 (pptFilePath).将 PowerPoint 演示文稿转换为照片的实际工作就是通过这种方法完成的。下面是用于上述示例代码的 PowerPoint ppt。

PowerPoint 演示文件

如何使用 C# 将 PowerPoint 转换为图像:图 4 - 代码示例中使用的 PowerPoint ppt。

Application pptApplication = 新应用程序(); 用于启动 PowerPoint 程序的新实例。通过 pptApplication.Presentations,我们可以打开pptFilePath.Open 所指示的 PowerPoint 演示文稿文件。() 函数。该函数将返回一个 Presentation 对象,该对象代表已打开的演示文稿。我们要确定输出文件夹 "outputFolder"是否存在。如果没有,我们将使用方法 Directory.CreateDirectory() 来创建它。

控制台输出

如何使用 C# 将 PowerPoint 转换为图像:图 5 - 控制台输出

我们使用 for 循环遍历演示文稿中的每张幻灯片。pptPresentation 使用 Slides.Count 属性提供幻灯片总数。我们使用输出文件夹路径和幻灯片索引为每张幻灯片的图片创建输出路径 (作为幻灯片{i}.png).接下来,我们使用 pptPresentation 将 PowerPoint 幻灯片导出为图片 (在本例中,采用 JPG 图像、PNG 图像格式).使用 导出() 功能。参数为图片格式 ("png "格式) 和尺寸 (宽度:1024,高度:768).最后,我们使用 pptPresentation 结束演示。() 并使用 pptApplication 结束 PowerPoint 会话。要适当放弃系统资源,请使用 Quit().

输出 - 将 PowerPoint 转换为 PNG 图像

如何使用 C# 将 PowerPoint 转换为图像:图 6 - 输出到 PNG 图像的 PowePoint 幻灯片

IronXL

一个名为 铁XL 使用 C# 来处理 Excel 文件变得更容易。IronXL 具有读取、创建和编辑 Excel 文件的广泛功能集,是一款适用于各种用途的灵活工具。下面我将介绍 IronXL 的一些主要功能:

  • 开发人员可以使用 IronXL 快速向新的或现有的 Excel 文件写入数据,并从现有的 Excel 文件读取数据。 IronXL.这包括访问工作表和工作簿属性,如格式化、公式和单元格值。
  • 有了 IronXL,开发人员可以将各种来源的数据导入 Excel 电子表格,包括数据库和 CSV 文件。同样,Excel 文件中的信息也可以导出为 CSV、HTML、XML 和 PDF 等文件格式。
  • 开发人员可以使用 IronXL 从 Excel 电子表格中动态添加、编辑和删除工作表。这样,数据组织和结构就可以根据应用需求灵活调整。
  • IronXL 可以精确操作 Excel 电子表格中的单个单元格。开发人员可以通过编程设置格式、样式、公式、单元格值和其他特征。

要了解更多有关文档的信息,请参阅 这里.

安装 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
VB   C#

首先,我们包含所需的命名空间。IronXL 库提供的类和方法都包含在 IronXL 命名空间中。我们要读取的 Excel 文件的路径 (sample.xlsx) 指定。 工作簿用于加载 Excel 文件。Excel 工作簿由一个 WorkBook 对象表示,该对象由 Load() 功能。使用工作簿,我们可以访问 workbook.WorkSheets 中的第一个工作表 [0].使用嵌套的 foreach 循环,我们遍历工作表的行和列。我们将每个单元格的值输出到控制台。

如何使用 C# 将 PowerPoint 转换为图像:图 7 - 控制台输出。

要了解更多代码信息,请参阅 这里.

结论

在许多软件应用程序中,都需要使用 C# 将 PowerPoint 演示文稿转换为照片。无论是否使用 Microsoft.Office.Interop.PowerPoint 命名空间,都可以快速完成这一过程。本文的代码示例将帮助您在 C# 应用程序中轻松实现 PowerPoint 到图片的转换功能,为信息发布和修改创造大量机会。

无需在目标计算机上安装 Excel,也无需依赖 Interop 库、 铁XL 提供了一种在 C# 中执行 Excel 操作的快速而有效的方法。使用 IronXL 在其 C# 应用程序中处理 Excel 数据的开发人员会发现它非常有用,因为它通过用户友好的 API 和广泛的功能集简化了读取、写入和更改 Excel 文件等操作。IronXL 提供了一个可靠的解决方案,可提高 Excel 相关开发项目的效率和适应性,无论您是要创建报表、处理数据还是自动执行电子表格杂务。

A 免费试用.它们的功能更全面,提供的功能和支持也更多。访问 IronXL 网站 了解有关许可证颁发的最新综合信息。请访问 网站 了解有关 Iron Software 产品的更多信息。

< 前一页
如何将PDF旋转180度(初学者教程)
下一步 >
如何使用 C# 创建 PowerPoint 演示文稿

准备开始了吗? 版本: 2024.9 刚刚发布

免费NuGet下载 总下载量: 10,731,156 查看许可证 >