Convert Image (JPG, BMP, GIF, PNG) to PDF in WPF

This section will introduce an easy solution to convert image to PDF in WPF. In my method, image to PDF conversion is just a piece of cake since various image formats such as jpg, png and bmp, even images of gif, tif and ico can be converted to PDF through two key steps for Spire.PDF users.

Spire.PDF for WPF,a professional WPF PDF component, enables your WPF applications to read, write and manipulate PDF document without Adobe Acrobat or any third party component library. As for image to PDF conversion, apart from clearly converting images of different formats to PDF, Spire.PDF for WPF also allows you to directly load your images to PDF files from stream. Please feel free to Download Spire.PDF and give it a try following our programming guide below. The following is a picture which we will convert to PDF. At the bottom of this  article, a target PDF will be presented.

Image to PDF

Now it's time for the key procedure of image to PDF conversion. To realize the key procedure, we need below two steps.

Step 1: Load an image file from system

An image file is required in this step. The image format can be any format among jpg, png, bmp, gif, tif and ico. Here a jpg image is loaded.

[C#]
//Load a jpg image from system
PdfImage image = PdfImage.FromFile(@"..\sky.jpg");
[VB.NET]
'Load a jpg image from system
Dim image As PdfImage = PdfImage.FromFile("..\sky.jpg") 

Step 2: Set the image location and size to fit PDF page

Spire.PDF for WPF contains a namespace “Spire.Pdf.Graphics” in which image size and location are set through four parameters: Width/Height for image size and fitWidth/fitHeight for image location.

[C#]
  //Set image display location and size in PDF
float widthFitRate = image.PhysicalDimension.Width / page.Canvas.ClientSize.Width;
float heightFitRate = image.PhysicalDimension.Height / page.Canvas.ClientSize.Height;
float fitRate = Math.Max(widthFitRate, heightFitRate);
float fitWidth = image.PhysicalDimension.Width / fitRate;
float fitHeight = image.PhysicalDimension.Height / fitRate;
page.Canvas.DrawImage(image, 30, 30, fitWidth, fitHeight);
[VB.NET]
'Set image display location and size in PDF
Dim widthFitRate As Single = image.PhysicalDimension.Width / page.Canvas.ClientSize.Width
Dim heightFitRate As Single = image.PhysicalDimension.Height / page.Canvas.ClientSize.Height
Dim fitRate As Single = Math.Max(widthFitRate, heightFitRate)
Dim fitWidth As Single = image.PhysicalDimension.Width / fitRate
Dim fitHeight As Single = image.PhysicalDimension.Height / fitRate
page.Canvas.DrawImage(image, 30, 30, fitWidth, fitHeight)

After this coding, run your application, you can find a target PDF as below.

Image to PDF

Apart from Spire.PDF for WPF, Spire.PDF also packs Spire.PDF for .NET and Spire.PDF for Silverlight, which enables you to directly edit PDF in your .NET/Silverlight applications through equally simple method. Click to know more about the other two PDF .NET component and PDF Silverlight component.