Presentation to image conversion

.NET Framework

This section covers converting an entire Presentation or a single slide to image in Windows Forms, WPF, ASP.NET and ASP.NET MVC platforms. The supported image formats are listed as follows.

  • BMP
  • EMF
  • JPG
  • JPEG
  • PNG

Assemblies Required

Refer to the following links for assemblies required based on platforms to convert the worksheet to image.

TIPS

When converting a slide to image, use ‘Metafile’ format for good image resolution.

The following code example demonstrates how to convert a slide to image.

//Namespaces to perform PPTX to Image conversion
using Syncfusion.Presentation;
using Syncfusion.OfficeChartToImageConverter;
using System.IO;
using Syncfusion.Drawing;
    
//Opens a PowerPoint Presentation file
IPresentation pptxDoc = Presentation.Open(fileName);

//Creates an instance of ChartToImageConverter
pptxDoc.ChartToImageConverter = new ChartToImageConverter();

//Sets the scaling mode as best
pptxDoc.ChartToImageConverter.ScalingMode = Syncfusion.OfficeChart.ScalingMode.Best;

//Converts the first slide into image
Image image = pptxDoc.Slides[0].ConvertToImage(Syncfusion.Drawing.ImageType.Metafile);

//Saves the image as file
image.Save("slide1.png");

//Disposes the image
image.Dispose();

//Closes the Presentation instance
pptxDoc.Close();
'Namespaces to perform PPTX to Image conversion
Imports Syncfusion.Presentation
Imports Syncfusion.OfficeChartToImageConverter
Imports Syncfusion.Drawing
Imports System.IO

'Opens a PowerPoint Presentation file
Dim pptxDoc As IPresentation = Presentation.Open(fileName)

'Creates an instance of ChartToImageConverter
pptxDoc.ChartToImageConverter = New ChartToImageConverter()

'Sets the scaling mode as best
pptxDoc.ChartToImageConverter.ScalingMode = Syncfusion.OfficeChart.ScalingMode.Best

'Converts the first slide into image
Dim image As Image = pptxDoc.Slides(0).ConvertToImage(Syncfusion.Drawing.ImageType.Metafile)

'Saves the image as file
image.Save("slide1.png")

'Disposes the image
image.Dispose()

'Closes the Presentation instance
Presentation_1.Close()
//Namespaces to perform PPTX to Image conversion
using Syncfusion.Presentation;
using Syncfusion.PresentationRenderer;
using System.IO;

//Open the existing PowerPoint presentation with stream.
using (IPresentation pptxDoc = Presentation.Open(fileStreamInput))
{
	//Initialize the PresentationRenderer to perform image conversion.
	pptxDoc.PresentationRenderer = new PresentationRenderer();
 
	//Convert PowerPoint slide to image as stream.
	using (Stream stream = pptxDoc.Slides[0].ConvertToImage(ExportImageFormat.Jpeg))
	{
		//Reset the stream position
		stream.Position = 0;
 
		//Create the output image file stream
		using (FileStream fileStreamOutput = File.Create("Output.jpg"))
		{
			//Copy the converted image stream into created output stream
			stream.CopyTo(fileStreamOutput);
		}
	}
}
//Namespaces to perform PPTX to Image conversion
using Syncfusion.Presentation;
using Syncfusion.PresentationRenderer;
using System.IO;

//Open the existing PowerPoint presentation with stream.
using (IPresentation pptxDoc = Presentation.Open(fileStreamInput))
{
	//Initialize the PresentationRenderer to perform image conversion.
	pptxDoc.PresentationRenderer = new PresentationRenderer();
 
	//Convert PowerPoint slide to image as stream.
	using (Stream stream = pptxDoc.Slides[0].ConvertToImage(ExportImageFormat.Jpeg))
	{
		//Reset the stream position
		stream.Position = 0;
 
		//Create the output image file stream
		using (FileStream fileStreamOutput = File.Create("Output.jpg"))
		{
			//Copy the converted image stream into created output stream
			stream.CopyTo(fileStreamOutput);
		}
	}
}

The following code example demonstrates the conversion of an entire Presentation to images:

//Loads the PowerPoint Presentation
IPresentation pptxDoc = Presentation.Open("Sample.pptx");

//Creates instance of ChartToImageConverter
pptxDoc.ChartToImageConverter = new ChartToImageConverter();

//Sets the scaling mode as best
pptxDoc.ChartToImageConverter.ScalingMode = Syncfusion.OfficeChart.ScalingMode.Best;

//Converts entire Presentation to images
Image[] images = pptxDoc.RenderAsImages(Syncfusion.Drawing.ImageType.Metafile);

//Saves the image to file system
foreach (Image image in images)
{ 
	image.Save("ImageOutput" + Guid.NewGuid().ToString()+ ".png");
}

The following code snippet demonstrates how to convert a PowerPoint slide to image using custom image resolution,

//Loads the PowerPoint presentation
IPresentation pptxDoc = Presentation.Open("Output.pptx");

//Declare variables to hold custom width and height
int customWidth = 1500;
int customHeight = 1000;

//Converts the slide as image and returns the image stream
Stream stream = pptxDoc.Slides[0].ConvertToImage(Syncfusion.Drawing.ImageFormat.Emf);

//Creates a bitmap of specific width and height
Bitmap bitmap = new Bitmap(customWidth, customHeight, PixelFormat.Format32bppPArgb);

//Gets the graphics from image
Graphics graphics = Graphics.FromImage(bitmap);

//Sets the resolution
bitmap.SetResolution(graphics.DpiX, graphics.DpiY);

//Recreates the image in custom size
graphics.DrawImage(System.Drawing.Image.FromStream(stream), new Rectangle(0, 0, bitmap.Width, bitmap.Height));

//Saves the image as bitmap 
bitmap.Save("ImageOutput" + Guid.NewGuid().ToString() + ".jpeg");

//Closes the presentation
pptxDoc.Close();
'Loads the PowerPoint presentation
Dim pptxDoc As IPresentation = Presentation.Open("Output.pptx")

'Declare variables to hold custom width and height
Dim customWidth As Integer = 1500
Dim customHeight As Integer = 1000

'Converts the slide as image and returns the image stream
Dim stream As Stream = pptxDoc.Slides(0).ConvertToImage(Syncfusion.Drawing.ImageFormat.Emf)

'Creates a bitmap of specific width and height
Dim bitmap As New Bitmap(customWidth, customHeight, PixelFormat.Format32bppPArgb)

'Gets the graphics from image
Dim imageGraphics As Graphics = Graphics.FromImage(bitmap)

'Sets the resolution
bitmap.SetResolution(imageGraphics.DpiX, imageGraphics.DpiY)

'Recreates the image in custom size
imageGraphics.DrawImage(System.Drawing.Image.FromStream(stream), New Rectangle(0, 0, bitmap.Width, bitmap.Height))

'Saves the image as bitmap
bitmap.Save("ImageOutput" + Guid.NewGuid().ToString() + ".jpeg")

'Closes the presentation
pptxDoc.Close()

UWP

PowerPoint slides can be converted to images in UWP by using Essential Presentation library. The following assemblies are required in the UWP application to convert the slides as images.

Assembly Name Short Description
Syncfusion.Presentation.UWP This assembly contains the core features needed for creating, reading, manipulating a Presentation file.
Syncfusion.OfficeChart.UWP This assembly contains the Office Chart Object model and core features needed for chart creation.
Syncfusion.OfficeChartToImageConverter.UWP This assembly is used to convert Office Chart into Image.
Syncfusion.SfChart.UWP Supporting assembly for Syncfusion.OfficeChartToImageConverter.UWP

The following code example demonstrates how to convert a slide to image in UWP.

//Load the presentation file using open picker
FileOpenPicker openPicker = new FileOpenPicker();
openPicker.FileTypeFilter.Add(".pptx");
StorageFile inputFile = await openPicker.PickSingleFileAsync();
pptxDoc = await Presentation.OpenAsync(inputFile);

//Initialize the ‘ChartToImageConverter’ instance to convert the charts in the slides
pptxDoc.ChartToImageConverter = new ChartToImageConverter();

//Pick the folder to save the converted images.
FolderPicker folderPicker = new FolderPicker();
folderPicker.ViewMode = PickerViewMode.Thumbnail;
folderPicker.FileTypeFilter.Add("*");
StorageFolder storageFolder = await folderPicker.PickSingleFolderAsync();
StorageFile imageFile = await storageFolder.CreateFileAsync("Slide1.jpg", CreationCollisionOption.ReplaceExisting);

//Convert the slide to image.
await slide.SaveAsImageAsync(imageFile);

//Closes the presentation instance
pptxDoc.Close();

The following code snippet demonstrates how to convert a PowerPoint slide to image using custom image resolution.

//Load the presentation file using open picker
FileOpenPicker openPicker = new FileOpenPicker();
openPicker.FileTypeFilter.Add(".pptx");
StorageFile inputFile = await openPicker.PickSingleFileAsync();
pptxDoc = await Presentation.OpenAsync(inputFile);

//Initialize the ‘ChartToImageConverter’ instance to convert the charts in the slides.
pptxDoc.ChartToImageConverter = new ChartToImageConverter();

//Pick the folder to save the converted images.
FolderPicker folderPicker = new FolderPicker();
folderPicker.ViewMode = PickerViewMode.Thumbnail;
folderPicker.FileTypeFilter.Add("*");
StorageFolder storageFolder = await folderPicker.PickSingleFolderAsync();
StorageFile imageFile = await storageFolder.CreateFileAsync("Slide1.jpg", CreationCollisionOption.ReplaceExisting);

//Get the stream of the created image file.
StorageFile imageStream = await imageFile.OpenStreamForWriteAsync()

//Creates a new instance for the rendering options to customize the image resolution.
RenderingOptions renderingOptions = new RenderingOptions();

//Sets the horizontal scaling value for the converted image. The default value is 1.
renderingOptions.ScaleX = 10F;

//Sets the vertical scaling value for the converted image. The default value is 1.
renderingOptions.ScaleY = 10F;

//Convert the slide to image with specified resolution.
await slide.SaveAsImageAsync(imageStream, renderingOptions);

//Closes the presentation instance
pptxDoc.Close();

The following code snippet demonstrates how to convert a PowerPoint slide to image by passing ‘CancellationToken’.

//Load the presentation file using open picker
FileOpenPicker openPicker = new FileOpenPicker();
openPicker.FileTypeFilter.Add(".pptx");
StorageFile inputFile = await openPicker.PickSingleFileAsync();
pptxDoc = await Presentation.OpenAsync(inputFile);

//Initialize the ChartToImageConverter instance to convert the charts in the slides.
pptxDoc.ChartToImageConverter = new ChartToImageConverter();


//Pick the folder to save the converted images.
FolderPicker folderPicker = new FolderPicker();                    
folderPicker.ViewMode = PickerViewMode.Thumbnail;
folderPicker.FileTypeFilter.Add("*");
StorageFolder storageFolder = await folderPicker.PickSingleFolderAsync();

//Create a cancellation token to cancel the image rendering instantly.
CancellationTokenSource cancellationToken = new CancellationTokenSource();

//Convert the slide to image.
int slideNumber = 1;
foreach (ISlide slide in pptxDoc.Slides)
{
   StorageFile imageFile = await storageFolder.CreateFileAsync("Slide" + slideNumber++ + ".jpg", CreationCollisionOption.ReplaceExisting);
   await slide.SaveAsImageAsync(imageFile, cancellationToken.Token);
}

//Close the Presentation instance
pptxDoc.Close();

NOTE

  1. PowerPoint Presentation to image conversion is supported in Blazor server-side application alone and is not supported in Blazor client-side application.
  2. Instance of ChartToImageConverter class is mandatory to convert the charts present in the Presentation to image. Otherwise, the charts in the presentation are not exported to the converted image
  3. ChartToImageConverter is supported from .NET Framework 4.0 onward
  4. The assembly “Syncfusion.SfChart.WPF” is non compliance with FIPS(Federal Information Processing Standard) algorithm policy.
  5. EMF images in the PowerPoint slides will not be converted in UWP due to platform limitation.
  6. Radial gradient, rectangular gradient and path gradient brushes are not supported in UWP due to platform limitation. These brushes are rendered as linear gradient brush in our UWP slide to image conversion.

Font substitution for unavailable fonts

When a font used in a PowerPoint presentation is unavailable in the environment where it is converted to image, then the library substitutes the ‘Microsoft Sans Serif’ as a default font for text rendering. This leads to a difference in text layouts of PowerPoint presentation and the converted image. To avoid this, the Essential Presentation library allows you to set an alternate font for the missing font used in the PowerPoint presentation.

NOTE

Font substitution for Unavailable fonts is not supported in UWP platform.

The following code sample demonstrates how to set a substitute font for a missing font while converting a PowerPoint presentation to image.

//Load the PowerPoint presentation and convert to image
using (IPresentation pptxDoc = Presentation.Open("Sample.pptx"))
{
	//Initialize 'ChartToImageConverter' to convert charts in the slides, and this is optional
	pptxDoc.ChartToImageConverter = new ChartToImageConverter();

	// Initializes the 'SubstituteFont' event to set the replacement font
	pptxDoc.FontSettings.SubstituteFont += FontSettings_SubstituteFont;

	//Converts the first slide into image
	Image image = pptxDoc.Slides[0].ConvertToImage(Syncfusion.Drawing.ImageType.Metafile);

	//Saves the image as file
	image.Save("slide1.png");

	//Disposes the image
	image.Dispose();
}

/// <summary>
/// Sets the alternate font when a specified font is unavailable in the production environment
/// </summary>
/// <param name="sender">FontSettings type of the Presentation in which the specified font is used but unavailable in production environment. </param>
/// <param name="args">Retrieves the unavailable font name and receives the substitute font name for conversion. </param>
private static void FontSettings_SubstituteFont(object sender, SubstituteFontEventArgs args)
{
	if (args.OriginalFontName == "Arial Unicode MS")
		args.AlternateFontName = "Arial";
	else
		args.AlternateFontName = "Times New Roman";
}
'Load the PowerPoint presentation and convert to image
Dim pptxDoc As IPresentation = Presentation.Open("Sample.pptx")

'Initialize 'ChartToImageConverter' to convert charts in the slides, and this is optional
pptxDoc.ChartToImageConverter = New ChartToImageConverter()

'Initializes the 'SubstituteFont' event to set the replacement font
AddHandler pptxDoc.FontSettings.SubstituteFont, AddressOf SubstituteFont

'Convert the PowerPoint presentation to image.
Dim image As Image = pptxDoc.Slides(0).ConvertToImage(Syncfusion.Drawing.ImageType.Metafile)

'Save the image.
image.Save("slide1.png")

'Dispose the Presentation instance
pptxDoc.Dispose()

'Dispose the image
image.Dispose()

''' <summary>
''' Sets the alternate font when a specified font is unavailable in the production environment
''' </summary>
''' <param name="sender">FontSettings type of the Presentation in which the specified font is used but unavailable in production environment. </param>
''' <param name="args">Retrieves the unavailable font name and receives the substitute font name for conversion. </param>
Private Sub SubstituteFont(ByVal sender As Object, ByVal args As SubstituteFontEventArgs)
	' Sets the alternate font when a specified font is not installed in the production environment
	If args.OriginalFontName = "Arial Unicode MS" Then
		args.AlternateFontName = "Arial"
	Else
		args.AlternateFontName = "Times New Roman"
	End If
End Sub