How to Extract Images from PowerPoint Presentations in C#/VB.NET

 PowerPoint is an application software for making slides (presentations), and each slide can contain various information such as text, graphics, graphics, tables, sounds, and images. Sometimes we find that there are some exquisite pictures in the PPT, or we want to save the pictures in the PPT for other reasons. But if the PowerPoint document contains a lot of pictures, saving them one by one will take too much time and effort. So, is there any way to extract pictures from PowerPoint efficiently and conveniently? In this post, you will learn how to programmatically extract pictures from PowerPoint documents. The following are the steps and methods I sorted out, and the C#/VB.NET code is attached for your reference.

  • Extract Images from an Entire Presentation 
  • Extract Images from a Specific Presentation Slide 

Programming Environment 

In this test, Free Spire. Presentation for .NET is introduced into the program. The Spire. Presentation.dll file can be referenced by:

Method 1: Download Free Spire.Presentation for .NET locally, unzip it, and install it. After the installation is complete, find Spire. Presentation.dll in the BIN folder under the installation path. Then open the "Solution Explorer" in Visual Studio, right-click "References", "Add Reference", and add a reference to the dll file in the BIN folder of the local path to the program.

Method 2: Install via NuGet. It can be installed by the following 2 methods:

(1) You can open the "Solution Explorer" in Visual Studio, right-click "References", "Manage NuGet Packages", then search for "Free Spire. Presentation ", and click "Install". Wait for the program installation to complete.

(2) Copy the following content to the PM console installation.

Install-Package FreeSpire. Presentation -Version 7.8

Extract Images from an Entire Presentation in C# and VB.NET

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.

Full Code

C#

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(@"Jupiter.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(@"D:\.NET\Extract images\Presentation\Images{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())

            'Initialize an instance of the Presentation class

            Dim ppt As Presentation = New Presentation()

            'Load a PowerPoint presentation

            ppt.LoadFromFile("Jupiter.pptx")

            'Get the image collection of the presentation

            Dim imageCollection As ImageCollection = ppt.Images

            'Iterate through the images in the collection

            For i As Integer = 0 To imageCollection.Count - 1

                'Extract the images

                imageCollection(i).Image.Save(String.Format("D:\.NET\Extract images\Presentation\Images{0}.png", i))

            Next

            ppt.Dispose()

        End Sub

    End Class

End Namespace

Effective Shot


Extract Images from a Specific Presentation Slide

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.

Full Code

C#

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("Jupiter.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(@"D:\.NET\Extract images\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(@"D:\.NET\Extract images\Slide\Images{0}.png", i));

                    i++;

                }

            }

        }

    }

}

VB.NET

Imports Spire.Presentation

 

Namespace ExtractImagesFromSlide

    Friend Class Program

        Private Shared Sub Main(ByVal args As String())

            'Initialize an instance of the Presentation class

            Dim ppt As Presentation = New Presentation()

            'Load a PowerPoint presentation

            ppt.LoadFromFile("Jupiter.pptx")

            'Get the first slide

            Dim slide As ISlide = ppt.Slides(0)

            Dim i = 0

            'Iterate through all shapes on the first slide

            For Each s As IShape In slide.Shapes

                'Check if the shape is of SlidePicture type

                If TypeOf s Is SlidePicture Then

                    'Extract the image

                    Dim ps As SlidePicture = TryCast(s, SlidePicture)

                    ps.PictureFill.Picture.EmbedImage.Image.Save(String.Format("D:\.NET\Extract images\Slide\Images{0}.png", i))

                    i += 1

                End If

                'Check if the shape is of PictureShape type

                If TypeOf s Is PictureShape Then

                    'Extract the image

                    Dim ps As PictureShape = TryCast(s, PictureShape)

                    ps.EmbedImage.Image.Save(String.Format("D:\.NET\Extract images\Slide\Images{0}.png", i))

                    i += 1

                End If

            Next

        End Sub

    End Class

End Namespace

Effective Shot

Conclusion:

     In this post, you have learned how to extract images from PowerPoint Presentations in C#/VB.NET. Not only that, we also have other functions, such as, C#/VB.NET: Group or Ungroup Shapes in PowerPoint, C#/VB.NET: Add Images to PowerPoint Documents and so on. Apart from that, if you'd like to learn more, you can visit this link to explore more about for Spire. Presentation for .NET.

 


Comments

Popular posts from this blog

How to Change Font Color in Word via Java

How to Convert OpenDocument Presentation (.odp) to PDF via Java Application