Images are one of the most common elements in PowerPoint presentations. There may be times when you need to extract images from specific presentation slides or from an entire presentation, for example, when you want to reuse those images in another presentation. In this article, you will learn how to extract images from PowerPoint presentations in C# and VB.NET using Spire.Presentation for .NET library.
- Extract Images from an Entire Presentation in C# and VB.NET
- Extract Images from a Specific Presentation Slide in C# and VB.NET
Install Spire.Presentation for .NET
To begin with, you need to add the DLL files included in the Spire.Presentation for.NET package as references in your .NET project. The DLL files can be either downloaded from this link or installed via NuGet.
PM> Install-Package Spire.Presentation
Extract Images from an Entire Presentation in C# and VB.NET
To extract images from an entire PowerPoint presentation, you need to use the Presentation.Images property to get the collection of all the images in the presentation, then iterate through the collection and call ImageCollection[int].Image.Save() method to save each image to an image file. The following are the detailed steps:
- Initialize an instance of the Presentation class.
- Load a PowerPoint presentation using Presentation.LoadFromFile() method.
- Get the collection of all the images in the presentation through Presentation.Images property.
- Iterate through the collection, and call ImageCollection[int].Image.Save() method to save the images in the collection to image files.
- C#
- VB.NET
using Spire.Presentation; using Spire.Presentation.Collections; using System.Drawing; namespace ExtractImagesFromPresentation { internal class Program { static void Main(string[] args) { //Initialize an instance of the Presentation class Presentation ppt = new Presentation(); //Load a PowerPoint presentation ppt.LoadFromFile(@"Template.pptx"); //Get the image collection of the presentation ImageCollection imageCollection = ppt.Images; //Iterate through the images in the collection for (int i = 0; i < imageCollection.Count; i++) { //Extract the images imageCollection[i].Image.Save(string.Format(@"Presentation\Images{0}.png", i)); } ppt.Dispose(); } } }
Extract Images from a Specific Presentation Slide in C# and VB.NET
To extract images from a specific slide, you need to iterate through all shapes on the slide, find the shapes that are of SlidePicture or PictureShape type, then use the SlidePicture.PictureFill.Picture.EmbedImage.Image.Save() or PictureShape.EmbedImage.Image.Save() method to save the images to image files. The following are the detailed steps:
- Initialize an instance of the Presentation class.
- Load a PowerPoint presentation using Presentation.LoadFromFile() method.
- Get a specific slide by its index through Presentation.Slides[int] property.
- Iterate through all shapes on the slide.
- Check if the shapes are of SlidePicture or PictureShape type. If the result is true, save the images to image files using SlidePicture.PictureFill.Picture.EmbedImage.Image.Save() or PictureShape.EmbedImage.Image.Save() method.
- C#
- VB.NET
using Spire.Presentation; namespace ExtractImagesFromSlide { internal class Program { static void Main(string[] args) { //Initialize an instance of the Presentation class Presentation ppt = new Presentation(); //Load a PowerPoint presentation ppt.LoadFromFile(@"Template4.pptx"); //Get the first slide ISlide slide = ppt.Slides[0]; int i = 0; //Iterate through all shapes on the first slide foreach (IShape s in slide.Shapes) { //Check if the shape is of SlidePicture type if (s is SlidePicture) { //Extract the image SlidePicture ps = s as SlidePicture; ps.PictureFill.Picture.EmbedImage.Image.Save(string.Format(@"Slide\Images{0}.png", i)); i++; } //Check if the shape is of PictureShape type if (s is PictureShape) { //Extract the image PictureShape ps = s as PictureShape; ps.EmbedImage.Image.Save(string.Format(@"Slide\Images{0}.png", i)); i++; } } } } }
Apply for a Temporary License
If you'd like to remove the evaluation message from the generated documents, or to get rid of the function limitations, please request a 30-day trial license for yourself.