We use cookies to give you the best experience on our website. If you continue to browse, then you agree to our privacy policy and cookie policy. Image for the cookie policy date
Unfortunately, activation email could not send to your email. Please try again.
Syncfusion Feedback

How to convert PDF to image in WPF PDFViewer?

Platform: WPF |
Control: PdfViewer
Tags: image, pdf, pdfviewer, c#

WPF PDF Viewer control supports viewing, reviewing, and printing PDF files in WPF applications. The PDF viewer also provides a way to convert PDF files into images in a format such as .jpg, .png and .tiff in C# and VB.NET using `ExportAsImage` API. You can use the images in your application to create thumbnails or cover page for a document etc.,

Convert a page of the PDF file into PNG image

You can refer to the following steps for performing the same:

Step 1: Include the following namespace in the MainWindow.xaml.cs file.

C#

using System.Windows.Media.Imaging;
using Syncfusion.Windows.PdfViewer;
using Syncfusion.Pdf.Parsing;
using System.Windows;
using System.IO;

VB.NET

Imports System.Windows.Media.Imaging
Imports Syncfusion.Windows.PdfViewer
Imports Syncfusion.Pdf.Parsing
Imports System.Windows
Imports System.IO

Step 2: Using the following code snippet, the PDF page can be exported as PNG image.

C#

PdfViewerControl pdfViewer = new PdfViewerControl();
//Load the input PDF file
PdfLoadedDocument loadedDocument = new PdfLoadedDocument("../../Data/Barcode.pdf");
pdfViewer.Load(loadedDocument);
//Export the particular PDF page as image at the page index of 0.
BitmapSource image = pdfViewer.ExportAsImage(0);
//Setup the output path
string output = @"..\..\Output\Image";
if (image != null)
{
   //Initialize the new PngBitmapEncoder
   BitmapEncoder encoder = new PngBitmapEncoder();
   //Create the bitmap frame using the bitmap source and add it to the encoder.
   encoder.Frames.Add(BitmapFrame.Create(image));
   //Create the file stream for the output in the desired image format.
   FileStream stream = new FileStream(output + ".png", FileMode.Create);
   //Save the stream, so that the image will be generated in the output location.
   encoder.Save(stream);
}
//Dispose the document.
loadedDocument.Dispose();
loadedDocument = null;

VB.NET

Dim pdfViewer As PdfViewerControl = New PdfViewerControl()
'Load the input PDF file
Dim loadedDocument As PdfLoadedDocument = New PdfLoadedDocument("../../Data/Barcode.pdf")
pdfViewer.Load(loadedDocument)
'Export the particular PDF page as image at the page index of 0.
Dim image As BitmapSource = pdfViewer.ExportAsImage(0)
'Setup the output path
Dim output As String = "..\..\Output\Image"
If image IsNot Nothing Then
'Initialize the New PngBitmapEncoder
Dim encoder As BitmapEncoder = New PngBitmapEncoder()
'Create the bitmap frame using the bitmap source And add it to the encoder.
encoder.Frames.Add(BitmapFrame.Create(image))
'Create the file stream for the output in the desired image format.
Dim stream As FileStream = New FileStream(output & ".png", FileMode.Create)
'Save the stream, so that the image will be generated in the output location.
encoder.Save(stream)
End If
'Dispose the document.
loadedDocument.Dispose()
loadedDocument = Nothing

Convert a specific range of pages of the PDF file into PNG images

You can refer to the following steps for performing the same:

Step 1: Include the following namespace in the MainWindow.xaml.cs file.

C#

using System.Windows.Media.Imaging;
using Syncfusion.Windows.PdfViewer;
using Syncfusion.Pdf.Parsing;
using System.Windows;
using System.IO;

VB.NET

Imports System.Windows.Media.Imaging
Imports Syncfusion.Windows.PdfViewer
Imports Syncfusion.Pdf.Parsing
Imports System.Windows
Imports System.IO

Step 2: Using the following code snippet, the specific range of pages of a PDF file can be exported as PNG images.

C#

PdfViewerControl pdfViewer = new PdfViewerControl();
//Load the input PDF file
PdfLoadedDocument loadedDocument = new PdfLoadedDocument("../../Data/Barcode.pdf");
pdfViewer.Load(loadedDocument);
//Export all the pages as images at the specific page range.
BitmapSource[] image = pdfViewer.ExportAsImage(0, loadedDocument.Pages.Count - 1);
//Setup the output path
string output = @"..\..\Output\Image";
if (image != null)
{
   for (int i = 0; i < image.Length; i++)
   {
      //Initialize the new PngBitmapEncoder
      BitmapEncoder encoder = new PngBitmapEncoder();
      //Create the bitmap frame using the bitmap source and add it to the encoder.
      encoder.Frames.Add(BitmapFrame.Create(image[i]));
      //Create the file stream for the output in the desired image format.
      FileStream stream = new FileStream(output + i.ToString() + ".png", FileMode.Create);
      //Save the stream, so that the image will be generated in the output location.
      encoder.Save(stream);
   }
}
//Dispose the document.
loadedDocument.Dispose();
loadedDocument = null;

VB.NET

Dim pdfViewer As PdfViewerControl = New PdfViewerControl()
'Load the input PDF file
Dim loadedDocument As PdfLoadedDocument = New PdfLoadedDocument("../../Data/Barcode.pdf")
pdfViewer.Load(loadedDocument)
'Export all the pages as images at the specific page range.
Dim image As BitmapSource() = pdfViewer.ExportAsImage(0, loadedDocument.Pages.Count - 1)
'Setup the output path
Dim output As String = "..\..\Output\Image"
If image IsNot Nothing Then
For i As Integer = 0 To image.Length - 1
'Initialize the New PngBitmapEncoder
Dim encoder As BitmapEncoder = New PngBitmapEncoder()
'Create the bitmap frame using the bitmap source and add it to the encoder.
encoder.Frames.Add(BitmapFrame.Create(image(i)))
'Create the file stream for the output in the desired image format.
Dim stream As FileStream = New FileStream(output & i.ToString() & ".png", FileMode.Create)
'Save the stream, so that the image will be generated in the output location.
encoder.Save(stream)
Next
End If
'Dispose the document.
loadedDocument.Dispose()
loadedDocument = Nothing

View sample in GitHub.

2X faster development

The ultimate WPF UI toolkit to boost your development speed.
ADD COMMENT
You must log in to leave a comment
Comments
Richard
Feb 14, 2021

For me, pdfViewer.ExportAsImage doesn't export BitmapSource, it exports Bitmap. Is that new/

Reply
deactivated_1013222@syncfusion.com [Syncfusion]
Feb 15, 2021

Ah, wasn't using the correct nuget package.

deactivated_1013222@syncfusion.com [Syncfusion]
Feb 15, 2021

When I try and run the code I get the exception: "System.InvalidOperationException: 'The calling thread must be STA, because many UI components require this.'". Is there a recommended way to handle this?

Reply
Remi
Sep 20, 2021

You can do more simple code

PdfLoadedDocument doc = new PdfLoadedDocument(file); PdfViewerControl pdfViewer = new PdfViewerControl(); pdfViewer.Load(doc); var image = pdfViewer.ExportAsImage(0, 300, 300); // Export page 0 in 300 DPI image.Save(System.IO.Path.GetFileNameWithoutExtension(file) + ".png", System.Drawing.Imaging.ImageFormat.Png);

Reply

Please sign in to access our KB

This page will automatically be redirected to the sign-in page in 10 seconds.

Up arrow icon

Warning Icon You are using an outdated version of Internet Explorer that may not display all features of this and other websites. Upgrade to Internet Explorer 8 or newer for a better experience.Close Icon

Live Chat Icon For mobile