New to Telerik Document ProcessingStart a free 30-day trial

ImageSource

Updated on Jun 3, 2026

ImageSource represents a single, constant set of pixels at a certain size. Multiple Image objects can use it to draw in a PDF file.

Creating an ImageSource

The ImageSource class has several public constructor overloads. You can create it from a Stream, BitmapSource object, or by using the EncodedImageData class:

  • public ImageSource(Stream stream): Creates an ImageSource object from a stream that contains an image.

  • public ImageSource(Stream stream, FormatProviders.Pdf.Export.ImageQuality imageQuality): Creates an ImageSource object from a stream and allows you to specify the image quality through the ImageQuality enumeration. For more information about the ImageQuality and its behavior, see the ImageQuality article. This overload can throw an exception if the image format is not supported.

  • public ImageSource(BitmapSource bitmapSource): Creates a new ImageSource object from a BitmapSource object. This overload is not available in the .NET Standard version of the PdfProcessing packages.

  • public ImageSource(BitmapSource bitmapSource, FormatProviders.Pdf.Export.ImageQuality imageQuality): Creates an ImageSource instance from a BitmapSource object and allows you to specify the image quality. This overload is not available in the .NET Standard version of the PdfProcessing packages.

  • public ImageSource(EncodedImageData imageSourceInfo): Initializes a new instance of ImageSource using the EncodedImageData class.

Example 1 shows how to create an ImageSource using a FileStream.

Example 1: Create ImageSource from Stream

C#
using (FileStream source = File.Open(filename, FileMode.Open))
{
    ImageSource imageSourceFromFile = new ImageSource(source);
}

With the EncodedImageData class you can create an ImageSource with encoded image data. This way the image quality is not reduced on import.

Example 2 demonstrates how to create an ImageSource using the EncodedImageData class.

Example 2: Create ImageSource from EncodedImageData

C#
byte[] imageBytes = File.ReadAllBytes(filename);
EncodedImageData encodedImageData = new EncodedImageData(imageBytes, 8, 655, 983, ColorSpaceNames.DeviceRgb,
    new string[] { PdfFilterNames.DCTDecode });
ImageSource imageSourceFromEncodedData = new ImageSource(encodedImageData);

With the EncodedImageData class you can also create an ImageSource with encoded image data and set its transparency. The EncodedImageData class provides a second constructor overload where you can set the alpha-channel bytes of the image as a second constructor parameter to apply transparency to this image.

Example 3: Create ImageSource from EncodedImageData with transparency

C#
int imageWidth = 100;
int imageHeight = 100;
byte[] alphaChannelBytes = new byte[imageWidth * imageHeight];
EncodedImageData imageData = new EncodedImageData(imageBytes, alphaChannelBytes, 8, imageWidth, imageHeight,
    ColorSpaceNames.DeviceRgb, new string[] { PdfFilterNames.FlateDecode });
ImageSource imageSource = new ImageSource(imageData);

Properties

The ImageSource class exposes the following properties:

PropertyDescription
WidthGets the width of the image.
HeightGets the height of the image.
DecodeArrayGets or sets the decode array, which specifies a linear mapping of each component value to an appropriate component value in the color space of the image. You can use it to manipulate the tones of the image.

Methods

The ImageSource class exposes two methods that help you get the data from the ImageSource object.

These methods are not available in the .NET Standard version of the PdfProcessing packages.

MethodDescription
GetBitmapSource()Gets the BitmapSource of the image.
GetEncodedImageData()Returns the encoded image data. Use this method to directly export images from the PDF document.

The CreateDocumentWithImages example in the SDK repository demonstrates how to insert JPEG and JPEG2000 images in a PDF document without decoding the images on import. The exported images are not re-encoded and their image quality is preserved.

Extensions

RadPdfProcessing exposes an extension method that allows you to convert every BitmapSource to an ImageSource that you can use for the creation of Image elements. Example 4 shows how to use the ToImageSource() extension method over a previously created bitmap.

Example 4: Create ImageSource with extension method

C#

BitmapImage bitmap = new BitmapImage();
bitmap.BeginInit();
bitmap.UriSource = new Uri(filename, UriKind.RelativeOrAbsolute);
bitmap.EndInit();

//requires using Telerik.Windows.Documents.Fixed.Model.Extensions;
ImageSource bitmapImageSource = bitmap.ToImageSource();

The code from Example 4 does not compile in Silverlight due to differences in the BitmapImage API for this platform. You can pass the image as a stream to the SetSource() method of BitmapImage instead.

See Also