C#/VB.NET實(shí)現(xiàn)從PPT中提取圖片的示例代碼
PowerPoint是用于制作幻燈片(演示文稿)的應(yīng)用軟件,每張幻燈片中都可以包含文字、圖形、圖形、表格、聲音和影像等多種信息。有時候我們發(fā)現(xiàn)在PPT里面有一些精美的圖片,或者其他原因想要把PPT里面的圖片保存下來。但如果PowerPoint文檔中包含大量圖片,一張張保存未免太費(fèi)時間及精力。那有什么辦法可以高效便捷地提取出PPT中的圖片呢?在這篇文章中,您將學(xué)習(xí)如何以編程方式從PowerPoint文檔中提取圖片。下面是我整理的步驟及方法,并附上C#/VB.NET代碼供大家參考。
- 從整個演示文稿中提取圖像
- 從特定演示幻燈片中提取圖像
程序環(huán)境
本次測試時,在程序中引入 Free Spire.Presentation.dll 文件。
方法1:
將?Free Spire.Presentation for .NET?? 下載到本地,解壓,找到 BIN 文件夾下的 Spire.Presentation.dll。然后在 Visual Studio 中打開“解決方案資源管理器”,鼠標(biāo)右鍵點(diǎn)擊“引用”,“添加引用”,將本地路徑 BIN 文件夾下的 dll 文件添加引用至程序。
方法2::
通過 ?NuGet??安裝。可通過以下 2 種方法安裝:
1. 可以在 Visual Studio 中打開“解決方案資源管理器”,鼠標(biāo)右鍵點(diǎn)擊“引用”,“管理 NuGet 包”,然后搜索“Free Spire.Presentation”,點(diǎn)擊“安裝”。等待程序安裝完成。
2. 將以下內(nèi)容復(fù)制到 PM 控制臺安裝。
Install-Package FreeSpire.Presentation -Version 7.8
從整個演示文稿中提取圖像
- 初始化 Presentation 類的一個實(shí)例。
- 使用 Presentation.LoadFromFile() 方法加載 PowerPoint 演示文稿。
- 通過 Presentation.Images 屬性獲取演示文稿中所有圖片的集合。
- 遍歷集合,調(diào)用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類的實(shí)例 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類的實(shí)例 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 類的一個實(shí)例。
- 使用 Presentation.LoadFromFile() 方法加載 PowerPoint 演示文稿。
- 通過 Presentation.Slides[int] 屬性按索引獲取特定幻燈片。
- 遍歷幻燈片上的所有形狀。
- 檢查形狀是否為 SlidePicture 或 PictureShape 類型。 如果結(jié)果為真,則使用 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 類的一個實(shí)例 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 類的一個實(shí)例 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
效果圖
到此這篇關(guān)于C#/VB.NET實(shí)現(xiàn)從PPT中提取圖片的示例代碼的文章就介紹到這了,更多相關(guān)C# PPT提取圖片內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Unity?UGUI的PhysicsRaycaster物理射線檢測組件介紹使用
這篇文章主要介紹了Unity?UGUI的PhysicsRaycaster物理射線檢測組件的介紹及使用,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-07-07SuperSocket入門--Telnet服務(wù)器和客戶端請求處理
本文的控制臺項(xiàng)目是根據(jù)SuperSocket官方Telnet示例代碼進(jìn)行調(diào)試的,官方示例代碼:Telnet示例。下面跟著小編一起來看下吧2017-01-01