Drawing bitmap images
Version 6.1.0
In this article
Overview
PDFsharp provides the XImage class to handle images. Images can be loaded from files or streams. When using the GDI+ build, XImage images can also be created from GDI+ Image objects.
Loading images from files
This code snippet shows how to load an image from a file using a relative path.
var fullName = "Logo.png";
var image = XImage.FromFile(fullName);
Loading images from streams
This code snippet shows how an image can be loaded from a stream.
var fullName = "Logo.png";
var imageStream = File.Open(fullName, FileMode.Open, FileAccess.Read);
var image = XImage.FromStream(imageStream);
Closing the stream is not shown here for the sake of simplicity.
It is possible to add images to your app or assembly as resources. Since these resources are accessed using streams, the pattern shown above also applies.
Drawing images on PDF pages
This code snippet shows how to draw an image at position (10, 10) with a size of (100, 100). gfx is an object of class XGraphics obtained for a PagePage object.
var fullName = "Logo.png";
var image = XImage.FromFile(fullName);
gfx.DrawImage(image, 10, 10, 100, 100);
For further information about drawing with PDFsharp: Drawing