Install-Package FreeSpire.Presentation -Version 7.8从整个演示文稿中提取图像
4.遍历集合,调用ImageCollection[int].Image.Save()方法将集合中的图片保存到图片文件中。
using Spire.Presentation; using Spire.Presentation.Collections; using System.Drawing; // 堆代码 duidaima.com 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
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效果图