iis服务器助手广告广告
返回顶部
首页 > 资讯 > 精选 >C#/VB.NET如何实现从PPT中提取图片
  • 779
分享到

C#/VB.NET如何实现从PPT中提取图片

2023-07-05 12:07:41 779人浏览 安东尼
摘要

本文小编为大家详细介绍“C#/VB.NET如何实现从PPT中提取图片”,内容详细,步骤清晰,细节处理妥当,希望这篇“C#/VB.net如何实现从PPT中提取图片”文章能帮助大家解决疑惑,下面跟着小编的思路慢慢深入,一起来学习新知识吧。程序环

本文小编为大家详细介绍“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 种方法安装:

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

将以下内容复制到 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.PresentationImports Spire.Presentation.CollectionsNamespace 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 ClassEnd 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\提取图片\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.PresentationNamespace 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 ClassEnd Namespace

读到这里,这篇“C#/VB.NET如何实现从PPT中提取图片”文章已经介绍完毕,想要掌握这篇文章的知识点还需要大家自己动手实践使用过才能领会,如果想了解更多相关内容的文章,欢迎关注编程网精选频道。

--结束END--

本文标题: C#/VB.NET如何实现从PPT中提取图片

本文链接: https://www.lsjlt.com/news/351544.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作