0
点赞
收藏
分享

微信扫一扫

如何通过C#/VB.NET从PowerPoint文档中提取图片

PowerPoint是用于制作幻灯片(演示文稿)的应用软件,每张幻灯片中都可以包含文字、图形、图形、表格、声音和影像等多种信息。有时候我们发现在PPT里面有一些精美的图片,或者其他原因想要把PPT里面的图片保存下来。但如果PowerPoint文档中包含大量图片,一张张保存未免太费时间及精力。那有什么办法可以高效便捷地提取出PPT中的图片呢?在这篇文章中,您将学习如何以编程方式从PowerPoint文档中提取图片。下面是我整理的步骤及方法,并附上C#/VB.NET代码供大家参考。

  • 从整个演示文稿中提取图像
  • 从特定演示幻灯片中提取图像

程序环境:

本次测试时,在程序中引入Free Spire. Presentation.dll 文件。

方法1:

​​Free Spire.Presentation for .NET ​​下载到本地,解压,找到 BIN 文件夹下的 Spire. Presentation.dll。然后在 Visual Studio 中打开“解决方案资源管理器”,鼠标右键点击“引用”,“添加引用”,将本地路径 BIN 文件夹下的 dll 文件添加引用至程序。

方法2::

通过 ​​​NuGet​​​​安装。可通过以下 2 种方法安装:

  1. 可以在 Visual Studio 中打开“解决方案资源管理器”,鼠标右键点击“引用”,“管理 NuGet 包”,然后搜索“Free Spire. Presentation”,点击“安装”。等待程序安装完成。

  2. 将以下内容复制到 PM 控制台安装。

Install-Package FreeSpire. Presentation -Version 7.8

从整个演示文稿中提取图像

  • 初始化 Presentation 类的一个实例。
  • 使用 Presentation.LoadFromFile() 方法加载 PowerPoint 演示文稿。
  • 通过 Presentation.Images 属性获取演示文稿中所有图片的集合。
  • 遍历集合,调用ImageCollection[int].Image.Save()方法将集合中的图片保存到图片文件中。

完整代码

C#

using Spire.Presentation;
using Spire.Presentation.Collections;
using System.Drawing;

namespace ExtractImagesFromPresentation
{
internal class Program
{
static void Main(string[] args)
{
//初始化Presentation类的实例
Presentation ppt = new Presentation();

//加载PowerPoint演示文稿
ppt.LoadFromFile("示例文档.pptx");

//获取演示文稿的图像集
ImageCollection imageCollection = ppt.Images;

//遍历集合中的图像
for (int i = 0; i < imageCollection.Count; i++)
{
//提取图像
imageCollection[i].Image.Save(string.Format("Presentation\\图片{0}.png", i));
}

ppt.Dispose();
}
}
}

​​VB.NET​​

Imports Spire.Presentation
Imports Spire.Presentation.Collections

Namespace ExtractImagesFromPresentation
Friend Class Program
Private Shared Sub Main(ByVal args As String())
'初始化Presentation类的实例
Dim ppt As Presentation = New Presentation()

'加载PowerPoint演示文稿
ppt.LoadFromFile("示例文档.pptx")

'获取演示文稿的图像集
Dim imageCollection As ImageCollection = ppt.Images

'遍历集合中的图像
For i As Integer = 0 To imageCollection.Count - 1
'提取图像
imageCollection(i).Image.Save(String.Format("Presentation\图片{0}.png", i))
Next

ppt.Dispose()
End Sub
End Class
End Namespace

效果图

如何通过C#/VB.NET从PowerPoint文档中提取图片_提取图像

从特定演示幻灯片中提取图像

  • 初始化 Presentation 类的一个实例。
  • 使用 Presentation.LoadFromFile() 方法加载 PowerPoint 演示文稿。
  • 通过 Presentation.Slides[int] 属性按索引获取特定幻灯片。
  • 遍历幻灯片上的所有形状。
  • 检查形状是否为 SlidePicture 或 PictureShape 类型。 如果结果为真,则使用 SlidePicture.PictureFill.Picture.EmbedImage.Image.Save()或 PictureShape.EmbedImage.Image.Save() 方法将图像保存到图像文件。

完整代码

C#

using Spire.Presentation;

namespace ExtractImagesFromSlide
{
internal class Program
{
static void Main(string[] args)
{
//初始化 Presentation 类的一个实例
Presentation ppt = new Presentation();
//加载 PowerPoint 演示文稿
ppt.LoadFromFile("示例文档.pptx");

//获取指定幻灯片
ISlide slide = ppt.Slides[1];

int i = 0;
//遍历指定幻灯片上的所有形状
foreach (IShape s in slide.Shapes)
{
//检查形状是否为SlidePicture类型
if (s is SlidePicture)
{
//提取图像
SlidePicture ps = s as SlidePicture;
ps.PictureFill.Picture.EmbedImage.Image.Save(string.Format(@"D:\.NET\提取图片\bin\Debug\Slide\图像{0}.png", i));
i++;
}
//检查形状是否为 PictureShape 类型
if (s is PictureShape)
{
//提取图像
PictureShape ps = s as PictureShape;
ps.EmbedImage.Image.Save(string.Format(@"D:\.NET\提取图片\bin\Debug\Slide\图像{0}.png", i));
i++;
}
}
}
}
}

​​​​VB.NET​​​

Imports Spire.Presentation

Namespace ExtractImagesFromSlide
Friend Class Program
Private Shared Sub Main(ByVal args As String())
'初始化 Presentation 类的一个实例
Dim ppt As Presentation = New Presentation()
'加载 PowerPoint 演示文稿
ppt.LoadFromFile("示例文档.pptx")

'获取指定幻灯片
Dim slide As ISlide = ppt.Slides(1)

Dim i = 0
'遍历指定幻灯片上的所有形状
For Each s As IShape In slide.Shapes
'检查形状是否为SlidePicture类型
If TypeOf s Is SlidePicture Then
'提取图像
Dim ps As SlidePicture = TryCast(s, SlidePicture)
ps.PictureFill.Picture.EmbedImage.Image.Save(String.Format("D:\.NET\提取图片\bin\Debug\Slide\图像{0}.png", i))
i += 1
End If
'检查形状是否为 PictureShape 类型
If TypeOf s Is PictureShape Then
'提取图像
Dim ps As PictureShape = TryCast(s, PictureShape)
ps.EmbedImage.Image.Save(String.Format("D:\.NET\提取图片\bin\Debug\Slide\图像{0}.png", i))
i += 1
End If
Next
End Sub
End Class
End Namespace

效果图

如何通过C#/VB.NET从PowerPoint文档中提取图片_C#_02

—本文完—

举报

相关推荐

0 条评论