How to Use C# to Convert PowerPoint to Image

Introduction

The necessity to convert PowerPoint presentations to picture formats frequently occurs in the field of software development. Many developers find it useful to be able to programmatically convert PowerPoint files to photos, whether it's for preview generation, thumbnail creation, or system integration. This article will explain how to accomplish this operation with C# ppt to image and include some sample code to help you along the way.

How to Use C# to Convert PowerPoint to Image

  1. Create PowerPoint Application Instance.
  2. Open Presentation using the Instance.
  3. Check and Create an Output Folder.
  4. Iterate Through Slides and Export Slides to Images.
  5. Close Presentation and Quit Application.

Convert PowerPoint presentation to image formats?

Let's take a quick look at the significance of converting PowerPoint slides to photos before getting into the specifics. Even while PowerPoint is a great tool for making dynamic presentations, it's not always feasible to share these files in their original format. Sometimes, just particular slides or photos taken from the presentation are required, and other times, different systems and settings may not enable direct rendering of PowerPoint files. An all-encompassing solution that is simple to share and see on a variety of devices and applications is provided by turning PowerPoint presentations into pictures.

Using PowerPoint Intrope Library

There are several methods for turning PowerPoint presentations into photos in C#. Using the Microsoft.Office.Interop.PowerPoint namespace, which offers classes and methods for programmatically interfacing with PowerPoint applications, is one popular approach. which provides extensive capability for working with PowerPoint files, is another strategy.

Create a New Visual Studio Project

Follow the below steps to create a new Visual Studio project:

Open the Visual Studio IDE. Make sure you have installed Visual Studio on your PC before using it.

Launch a New Project:

Choose File, New, and finally Project.

Project." />

From the "Create a new project" box, select your favorite programming language (C#, for example) from the left side.

Next, select the "Console App" or "Console App (.NET Core)" template from the list of available project templates.

Please complete the "Name" section to give your project a name.

How to Use C# to Convert PowerPoint to Image: Figure 2 - From the Create New Project box, select the C# programming language and Console App. Configure the Project name and location, then click on the "Next" button.

Select the storage location for the project.

Click "Create" to start working on a new Console application project.

How to Use C# to Convert PowerPoint to Image: Figure 3 - Select the appropriate .NET Framework and click on the "Create" button.

Convert PowerPoint slides to images in C#

Let's begin by looking at how to use the Microsoft.Office.Interop.PowerPoint namespace to convert PowerPoint slides to pictures. Make sure the required assemblies are installed and added to your C# project as references first. These assemblies are usually found by referring to the InterOp assemblies directly or by installing the Microsoft Office Primary Interop Assemblies (PIA).

Code Example

using System.IO;
using Microsoft.Office.Interop.PowerPoint;
class Program
{
    static void Main(string[] args)
    {
        string pptFilePath = "demo.pptx"; // Path to your PowerPoint file
        string outputFolder = "output_images"; // Output folder path where images will be saved
        ConvertPptToImages(pptFilePath, outputFolder);
    }
    static void ConvertPptToImages(string pptFilePath, string outputFolder)
    {
        Application pptApplication = new Application();
        Presentation pptPresentation = pptApplication.Presentations.Open(pptFilePath, MsoTriState.msoFalse, MsoTriState.msoFalse, MsoTriState.msoFalse);
        if (!Directory.Exists(outputFolder))
            Directory.CreateDirectory(outputFolder);
        int slidesCount = pptPresentation.Slides.Count;
        for (int i = 1; i <= slidesCount; i++)
        {
            string outputPath = Path.Combine(outputFolder, $"Slide{i}.png");
            pptPresentation.Slides[i].Export(outputPath, "png", 1024, 768);
        //saving the presentation slides into png images
        }
        pptPresentation.Close();
        pptApplication.Quit();
    }
}
using System.IO;
using Microsoft.Office.Interop.PowerPoint;
class Program
{
    static void Main(string[] args)
    {
        string pptFilePath = "demo.pptx"; // Path to your PowerPoint file
        string outputFolder = "output_images"; // Output folder path where images will be saved
        ConvertPptToImages(pptFilePath, outputFolder);
    }
    static void ConvertPptToImages(string pptFilePath, string outputFolder)
    {
        Application pptApplication = new Application();
        Presentation pptPresentation = pptApplication.Presentations.Open(pptFilePath, MsoTriState.msoFalse, MsoTriState.msoFalse, MsoTriState.msoFalse);
        if (!Directory.Exists(outputFolder))
            Directory.CreateDirectory(outputFolder);
        int slidesCount = pptPresentation.Slides.Count;
        for (int i = 1; i <= slidesCount; i++)
        {
            string outputPath = Path.Combine(outputFolder, $"Slide{i}.png");
            pptPresentation.Slides[i].Export(outputPath, "png", 1024, 768);
        //saving the presentation slides into png images
        }
        pptPresentation.Close();
        pptApplication.Quit();
    }
}
Imports System.IO
Imports Microsoft.Office.Interop.PowerPoint
Friend Class Program
	Shared Sub Main(ByVal args() As String)
		Dim pptFilePath As String = "demo.pptx" ' Path to your PowerPoint file
		Dim outputFolder As String = "output_images" ' Output folder path where images will be saved
		ConvertPptToImages(pptFilePath, outputFolder)
	End Sub
	Private Shared Sub ConvertPptToImages(ByVal pptFilePath As String, ByVal outputFolder As String)
		Dim pptApplication As New Application()
		Dim pptPresentation As Presentation = pptApplication.Presentations.Open(pptFilePath, MsoTriState.msoFalse, MsoTriState.msoFalse, MsoTriState.msoFalse)
		If Not Directory.Exists(outputFolder) Then
			Directory.CreateDirectory(outputFolder)
		End If
		Dim slidesCount As Integer = pptPresentation.Slides.Count
		For i As Integer = 1 To slidesCount
			Dim outputPath As String = Path.Combine(outputFolder, $"Slide{i}.png")
			pptPresentation.Slides(i).Export(outputPath, "png", 1024, 768)
		'saving the presentation slides into png images
		Next i
		pptPresentation.Close()
		pptApplication.Quit()
	End Sub
End Class
VB   C#

The C# namespace needed to work with PowerPoint apps is imported by using Microsoft.Office.Interop.PowerPoint; declaration. The program's entrance point is the Main method. It designates the output folder (outputFolder), which is where the created photographs will be kept, and the path to the PowerPoint file (pptFilePath). The actual transformation of PowerPoint presentations into photos is handled by this approach. Below is the PowerPoint ppt which is used for the above example code.

PowerPoint presentation File

How to Use C# to Convert PowerPoint to Image: Figure 4 - PowerPoint ppt used for the code example.

Application pptApplication = new Application(); is used to start a new instance of the PowerPoint program. This enables programmatic interaction with PowerPoint.Using pptApplication.Presentations, we open the PowerPoint presentation file indicated by the pptFilePath.Open() function. A Presentation object, which represents the opened presentation, is returned by this function. We determine if the output folder "outputFolder" is present. If not, we use the method Directory.CreateDirectory() to create it.

Console Output

How to Use C# to Convert PowerPoint to Image: Figure 5 - Console Output

We use a for loop to iterate through each slide in the presentation. The pptPresentation provides the total number of slides using the property Slides.Count. We use the output folder path and slide index to create the output path for each slide's picture (as Slides{i}.png). Next, we use pptPresentation to export the PowerPoint slide as a picture (in this example, in JPG image, PNG image format). Use the Export() function. The parameters are the picture format ("png" format) and size (width: 1024, height: 768). Lastly, we use pptPresentation to end the presentation.Close() and use pptApplication to end the PowerPoint session. To appropriately relinquish system resources, use Quit().

Output - Convert a PowerPoint to PNG images

How to Use C# to Convert PowerPoint to Image: Figure 6 - Exported PowePoint slides to PNG Images output

IronXL

A well-liked.NET framework called IronXL makes it easier to manipulate Excel files in C#. With its extensive feature set for reading, creating, and editing Excel files, it's a flexible tool suitable for a wide range of uses. I'll go over some of IronXL's main features below:

  • Developers may quickly write data to new or existing Excel files and read data from existing Excel files using IronXL. This covers gaining access to worksheet and workbook attributes such as formatting, formulae, and cell values.
  • With IronXL, developers can import data into Excel spreadsheets from a variety of sources, including databases and CSV files. Likewise, information from Excel files may be exported to CSV, HTML, XML, and PDF, among other file formats.
  • Developers may dynamically add, edit, and remove worksheets from Excel spreadsheets using IronXL. This gives data organization and structure flexibility based on application needs.
  • IronXL makes it possible to precisely manipulate individual cells in Excel spreadsheets. Programmatically, developers may set formatting, styles, formulae, cell values, and other characteristics.

To learn more about the documentation refer here.

Install IronXL

Let's begin by installing IronXL using the NuGet Package Manager Console before moving further:

Install-Package IronXL.Excel
Install-Package IronXL.Excel
'INSTANT VB TODO TASK: The following line uses invalid syntax:
'Install-Package IronXL.Excel
VB   C#

After installation, IronXL may be used in our C# project.

Using IronXL for Excel Operations

Let us examine a hypothetical situation where we wish to use IronXL to read data from an Excel file.

A quick illustration of how to accomplish this in given in the following code sample:

using IronXL;
using System;
class Program
{
    static void Main(string[] args)
    {
        // Path to the Excel file
        string excelFilePath = "sample.xlsx";
        // Load the Excel file
        WorkBook workbook = WorkBook.Load(excelFilePath);
        // Access the first worksheet
        WorkSheet worksheet = workbook.WorkSheets[0];
        // Iterate through rows and columns to read data
        foreach (var row in worksheet.Rows)
        {
            foreach (var cell in row)
            {
                Console.Write(cell.Value + "\t");
            }
            Console.WriteLine();
        }
    }
}
using IronXL;
using System;
class Program
{
    static void Main(string[] args)
    {
        // Path to the Excel file
        string excelFilePath = "sample.xlsx";
        // Load the Excel file
        WorkBook workbook = WorkBook.Load(excelFilePath);
        // Access the first worksheet
        WorkSheet worksheet = workbook.WorkSheets[0];
        // Iterate through rows and columns to read data
        foreach (var row in worksheet.Rows)
        {
            foreach (var cell in row)
            {
                Console.Write(cell.Value + "\t");
            }
            Console.WriteLine();
        }
    }
}
Imports Microsoft.VisualBasic
Imports IronXL
Imports System
Friend Class Program
	Shared Sub Main(ByVal args() As String)
		' Path to the Excel file
		Dim excelFilePath As String = "sample.xlsx"
		' Load the Excel file
		Dim workbook As WorkBook = WorkBook.Load(excelFilePath)
		' Access the first worksheet
		Dim worksheet As WorkSheet = workbook.WorkSheets(0)
		' Iterate through rows and columns to read data
		For Each row In worksheet.Rows
			For Each cell In row
				Console.Write(cell.Value & vbTab)
			Next cell
			Console.WriteLine()
		Next row
	End Sub
End Class
VB   C#

First, we include the required namespaces. The classes and methods offered by the IronXL library are contained in the IronXL namespace. The path to the Excel file we wish to read (sample.xlsx) is specified. WorkBook is used to load the Excel file. The Excel workbook is represented by a WorkBook object that is returned by the Load() function. Using the workbook, we can access the first worksheet in the workbook.WorkSheets[0]. Using nested foreach loops, we traverse through the worksheet's rows and columns. We output the value of each cell to the console.

How to Use C# to Convert PowerPoint to Image: Figure 7 - Console Output.

To learn more about the code refer here.

Conclusion

In many software applications, it is necessary to convert PowerPoint presentations to photos using C#. Using the Microsoft.Office.Interop.PowerPoint namespace or not, the procedure may be completed rather quickly. This article's code examples will help you include PowerPoint to picture conversion capability into your C# apps with ease, creating a plethora of opportunities for information distribution and modification.

Without requiring Excel to be installed on the target machine or depending on the Interop library, IronXL provides a quick and effective way to execute Excel operations in C#. Developers using IronXL to deal with Excel data in their C# applications will find it useful as it streamlines operations like reading, writing, and changing Excel files with its user-friendly API and extensive feature set. IronXL offers a dependable solution that increases efficiency and adaptability in Excel-related development projects, regardless of whether you're creating reports, handling data, or automating spreadsheet chores.

A free-trial. They are more fully functioning, and offer more features, and support. Visit the IronXL website for comprehensive and current information about licensing. Visit this website to learn more about Iron Software products.