Test in a live environment
Test in production without watermarks.
Works wherever you need it to.
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.
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.
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.
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.
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.
Select the storage location for the project.
Click "Create" to start working on a new Console application project.
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).
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
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.
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.
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().
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:
To learn more about the documentation refer here.
Let's begin by installing IronXL using the NuGet Package Manager Console before moving further:
Install-Package IronXL.Excel
After installation, IronXL may be used in our C# project.
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
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.
To learn more about the code refer here.
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.
9 .NET API products for your office documents