C#/VB.NET – Convert PPT(X) to Images

Compared with PowerPoint documents, images are more user friendly. Because most portable devices support browsing pictures instead of PowerPoint files. Apart from that, you would also choose to convert your PowerPoint document to images to prevent modification.

In this article, I am going to show you how to convert PowerPoint (PPT and PPTX) to PNG or SVG by using Spire.Presentation for .NET.

INSTALL SPIRE.PRESENTATION

First of all, you need to download the latest version of Spire.Presentation for .NET from this link, and manually add the DLL files in your .NET application as references. Or, you can install it directly via NuGet Package Manager.

Example 1. Convert PPT(X) to PNG

Spire.Presentation offers the ISlide.SaveAsImage() method to convert a specific slide to image data. And then, you save the data in a specified image format (such as PNG, BMP, JPEG and TIFF) using Image.Save() method.

[C#]

using Spire.Presentation;
using System;
using System.Drawing;
using System.Drawing.Imaging;

namespace ConvertPowerPointToPng
{
    class Program
    {
        static void Main(string[] args)
        {
            //Create a Presentation object
            Presentation presentation = new Presentation();

            //Load a sample PowerPoint file
            presentation.LoadFromFile(@"C:\Users\Administrator\Desktop\source.pptx");

            //Loop through the slides
            for (int i = 0; i < presentation.Slides.Count; i++)
            {
                //Save the crrent slide to image
                Image image = presentation.Slides[i].SaveAsImage();

                //Save the image in a .png format file
                String fileName = String.Format("‪result-{0}.png", i);‬‬
                image.Save("C:\\Users\\Administrator\\Desktop\\Output\\" + fileName, ImageFormat.Png);
            }
        }
    }
}

[VB.NET]

Imports Spire.Presentation
Imports System
Imports System.Drawing
Imports System.Drawing.Imaging
 
Namespace ConvertPowerPointToPng
    Class Program
        Shared  Sub Main(ByVal args() As String)
            'Create a Presentation object
            Dim presentation As Presentation =  New Presentation() 
 
            'Load a sample PowerPoint file
            presentation.LoadFromFile("C:\Users\Administrator\Desktop\source.pptx")
 
            'Loop through the slides
            Dim i As Integer
            For  i = 0 To  presentation.Slides.Count- 1  Step  i + 1
                'Save the crrent slide to image
                Dim image As Image =  presentation.Slides(i).SaveAsImage() 
 
                'Save the image in a .png format file
                Dim fileName As String =  String.Format("‪result-{0}.png",i) ‬‬
                image.Save("C:\\Users\\Administrator\\Desktop\\Output\\" + fileName, ImageFormat.Png)
            Next
        End Sub
    End Class
End Namespace

Example 2. Convert PPT(X) to SVG

To export a specific slide as SVG byte array, use the ISlide.SaveToSVG() method. Then, use the File.WriteAllBytes() method write the byte array to a .svg file.

[C#]

using System;
using Spire.Presentation;
using System.IO;

namespace ConvertPowerPointToSvg
{
    class Program
    {
        static void Main(string[] args)
        {
            //Create a Presentation object
            Presentation presentation = new Presentation();

            //Load the sample PowerPoint file
            presentation.LoadFromFile(@"C:\Users\Administrator\Desktop\source.pptx");

            //Loop through the slides
            for (int i = 0; i < presentation.Slides.Count; i++)
            {
                //Convert the current slide to SVG byte array
                byte[] svgByte = presentation.Slides[i].SaveToSVG();

                //Write the byte array to a .svg file 
                String fileName = String.Format("‪result-{0}.svg", i);‬
                File.WriteAllBytes("C:\\Users\\Administrator\\Desktop\\Output\\" + fileName, svgByte);
            }
        }
    }
}

[VB.NET]

Imports System
Imports Spire.Presentation
Imports System.IO
 
Namespace ConvertPowerPointToSvg
    Class Program
        Shared  Sub Main(ByVal args() As String)
            'Create a Presentation object
            Dim presentation As Presentation =  New Presentation() 
 
            'Load the sample PowerPoint file
            presentation.LoadFromFile("C:\Users\Administrator\Desktop\source.pptx")
 
            'Loop through the slides
            Dim i As Integer
            For  i = 0 To  presentation.Slides.Count- 1  Step  i + 1
                'Convert the current slide to SVG byte array
                Dim svgByte() As Byte =  presentation.Slides(i).SaveToSVG() 
 
                'Write the byte array to a .svg file 
                Dim fileName As String =  String.Format("‪result-{0}.svg",i) ‬
                File.WriteAllBytes("C:\\Users\\Administrator\\Desktop\\Output\\" + fileName, svgByte)
            Next
        End Sub
    End Class
End Namespace

Leave a comment