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#

usingSpire.Presentation;usingSpire.Presentation.Collections;usingSystem.Drawing;namespaceExtractImagesFromPresentation
{
internal classProgram
{
static void Main(string[] args)
{
//初始化Presentation类的实例 Presentation ppt = newPresentation();//加载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

ImportsSpire.PresentationImportsSpire.Presentation.CollectionsNamespaceExtractImagesFromPresentationFriend ClassProgramPrivate Shared Sub Main(ByVal args As String())'初始化Presentation类的实例
            Dim ppt As Presentation = NewPresentation()'加载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))Nextppt.Dispose()End Sub
    End Class
End Namespace

效果图

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

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

完整代码

C#

usingSpire.Presentation;namespaceExtractImagesFromSlide
{
internal classProgram
{
static void Main(string[] args)
{
//初始化 Presentation 类的一个实例 Presentation ppt = newPresentation();//加载 PowerPoint 演示文稿 ppt.LoadFromFile("示例文档.pptx");//获取指定幻灯片 ISlide slide = ppt.Slides[1];int i = 0;//遍历指定幻灯片上的所有形状 foreach (IShape s inslide.Shapes)
{
//检查形状是否为SlidePicture类型 if (s isSlidePicture)
{
//提取图像 SlidePicture ps = s asSlidePicture;
ps.PictureFill.Picture.EmbedImage.Image.Save(
string.Format(@"D:\.NET\提取图片\bin\Debug\Slide\图像{0}.png", i));
i
++;
}
//检查形状是否为 PictureShape 类型 if (s isPictureShape)
{
//提取图像 PictureShape ps = s asPictureShape;
ps.EmbedImage.Image.Save(
string.Format(@"D:\.NET\提取图片\bin\Debug\Slide\图像{0}.png", i));
i
++;
}
}
}
}
}

VB.NET

ImportsSpire.PresentationNamespaceExtractImagesFromSlideFriend ClassProgramPrivate Shared Sub Main(ByVal args As String())'初始化 Presentation 类的一个实例
            Dim ppt As Presentation = NewPresentation()'加载 PowerPoint 演示文稿
            ppt.LoadFromFile("示例文档.pptx")'获取指定幻灯片
            Dim slide As ISlide = ppt.Slides(1)Dim i = 0
            '遍历指定幻灯片上的所有形状
            For Each s As IShape Inslide.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

效果图

—本文完—

标签: none

添加新评论