本文小编为大家详细介绍“C#/VB.NET如何实现从PPT中提取图片”,内容详细,步骤清晰,细节处理妥当,希望这篇“C#/VB.NET如何实现从PPT中提取图片”文章能帮助大家解决疑惑,下面跟着小编的思路慢慢深入,一起来学习新知识吧。
程序环境
本次测试时,在程序中引入 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从特定演示幻灯片中提取图像
初始化 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提取图片inDebugSlide图像{0}.png", i));
                    i++;
                }
                //检查形状是否为 PictureShape 类型
                if (s is PictureShape)
                {
                    //提取图像
                    PictureShape ps = s as PictureShape;
                    ps.EmbedImage.Image.Save(string.Format(@"D:.NET提取图片inDebugSlide图像{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提取图片inDebugSlide图像{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提取图片inDebugSlide图像{0}.png", i))
                    i += 1
                End If
            Next
        End Sub
    End Class
End Namespace