翼度科技»论坛 编程开发 .net 查看内容

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

6

主题

6

帖子

18

积分

新手上路

Rank: 1

积分
18
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#
  1. using Spire.Presentation;
  2. using Spire.Presentation.Collections;
  3. using System.Drawing;
  4. namespace ExtractImagesFromPresentation
  5. {
  6.     internal class Program
  7.     {
  8.         static void Main(string[] args)
  9.         {
  10.             //初始化Presentation类的实例
  11.             Presentation ppt = new Presentation();
  12.             //加载PowerPoint演示文稿
  13.             ppt.LoadFromFile("示例文档.pptx");
  14.             //获取演示文稿的图像集
  15.             ImageCollection imageCollection = ppt.Images;
  16.             //遍历集合中的图像
  17.             for (int i = 0; i < imageCollection.Count; i++)
  18.             {
  19.                 //提取图像
  20.                 imageCollection[i].Image.Save(string.Format("Presentation\\图片{0}.png", i));
  21.             }
  22.             ppt.Dispose();
  23.         }
  24.     }
  25. }
复制代码
VB.NET
  1. Imports Spire.Presentation
  2. Imports Spire.Presentation.Collections
  3. Namespace ExtractImagesFromPresentation
  4.     Friend Class Program
  5.         Private Shared Sub Main(ByVal args As String())
  6.             '初始化Presentation类的实例
  7.             Dim ppt As Presentation = New Presentation()
  8.             '加载PowerPoint演示文稿
  9.             ppt.LoadFromFile("示例文档.pptx")
  10.             '获取演示文稿的图像集
  11.             Dim imageCollection As ImageCollection = ppt.Images
  12.             '遍历集合中的图像
  13.             For i As Integer = 0 To imageCollection.Count - 1
  14.                 '提取图像
  15.                 imageCollection(i).Image.Save(String.Format("Presentation\图片{0}.png", i))
  16.             Next
  17.             ppt.Dispose()
  18.         End Sub
  19.     End Class
  20. End Namespace
复制代码
效果图


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


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

C#
  1. using Spire.Presentation;
  2. namespace ExtractImagesFromSlide
  3. {
  4.     internal class Program
  5.     {
  6.         static void Main(string[] args)
  7.         {
  8.             //初始化 Presentation 类的一个实例
  9.             Presentation ppt = new Presentation();
  10.             //加载 PowerPoint 演示文稿
  11.             ppt.LoadFromFile("示例文档.pptx");
  12.             //获取指定幻灯片
  13.             ISlide slide = ppt.Slides[1];
  14.             int i = 0;
  15.             //遍历指定幻灯片上的所有形状
  16.             foreach (IShape s in slide.Shapes)
  17.             {
  18.                 //检查形状是否为SlidePicture类型
  19.                 if (s is SlidePicture)
  20.                 {
  21.                     //提取图像
  22.                     SlidePicture ps = s as SlidePicture;
  23.                     ps.PictureFill.Picture.EmbedImage.Image.Save(string.Format(@"D:\.NET\提取图片\bin\Debug\Slide\图像{0}.png", i));
  24.                     i++;
  25.                 }
  26.                 //检查形状是否为 PictureShape 类型
  27.                 if (s is PictureShape)
  28.                 {
  29.                     //提取图像
  30.                     PictureShape ps = s as PictureShape;
  31.                     ps.EmbedImage.Image.Save(string.Format(@"D:\.NET\提取图片\bin\Debug\Slide\图像{0}.png", i));
  32.                     i++;
  33.                 }
  34.             }
  35.         }
  36.     }
  37. }
复制代码
VB.NET
  1. Imports Spire.Presentation
  2. Namespace ExtractImagesFromSlide
  3.     Friend Class Program
  4.         Private Shared Sub Main(ByVal args As String())
  5.             '初始化 Presentation 类的一个实例
  6.             Dim ppt As Presentation = New Presentation()
  7.             '加载 PowerPoint 演示文稿
  8.             ppt.LoadFromFile("示例文档.pptx")
  9.             '获取指定幻灯片
  10.             Dim slide As ISlide = ppt.Slides(1)
  11.             Dim i = 0
  12.             '遍历指定幻灯片上的所有形状
  13.             For Each s As IShape In slide.Shapes
  14.                 '检查形状是否为SlidePicture类型
  15.                 If TypeOf s Is SlidePicture Then
  16.                     '提取图像
  17.                     Dim ps As SlidePicture = TryCast(s, SlidePicture)
  18.                     ps.PictureFill.Picture.EmbedImage.Image.Save(String.Format("D:\.NET\提取图片\bin\Debug\Slide\图像{0}.png", i))
  19.                     i += 1
  20.                 End If
  21.                 '检查形状是否为 PictureShape 类型
  22.                 If TypeOf s Is PictureShape Then
  23.                     '提取图像
  24.                     Dim ps As PictureShape = TryCast(s, PictureShape)
  25.                     ps.EmbedImage.Image.Save(String.Format("D:\.NET\提取图片\bin\Debug\Slide\图像{0}.png", i))
  26.                     i += 1
  27.                 End If
  28.             Next
  29.         End Sub
  30.     End Class
  31. End Namespace
复制代码
效果图


—本文完—

来源:https://www.cnblogs.com/Carina-baby/archive/2023/03/13/17211465.html
免责声明:由于采集信息均来自互联网,如果侵犯了您的权益,请联系我们【E-Mail:cb@itdo.tech】 我们会及时删除侵权内容,谢谢合作!

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?立即注册

x

举报 回复 使用道具