Available for: UI for ASP.NET MVC | UI for ASP.NET AJAX | UI for Blazor | UI for WPF | UI for WinForms | UI for Silverlight | UI for Xamarin | UI for WinUI | UI for ASP.NET Core | UI for .NET MAUI

New to Telerik Document Processing? Download free 30-day trial

ImageSource

ImageSource represents a single, constant set of pixels at a certain size. It can be used by multiple Image objects in order to be drawn in a PDF file.

Creating an ImageSource

The ImageSource class has several public constructor overloads and can be created from a Stream, BitmapSource object or using the EncodedImageData class:

  • public ImageSource(Stream stream): Creates an ImageSource object from a stream that contains 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. More information about the ImageQuality and its behavior is available in this article. This overload might 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 binaries.

  • 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 binaries.

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

Example 1 illustrates how you can create an ImageSource using a FileStream.

Example 1: Create ImageSource from Stream

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

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

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

Example 2: Create ImageSource from EncodedImageData

EncodedImageData imageData = new EncodedImageData(imageBytes, 8, 655, 983, ColorSpaceNames.DeviceRgb, new string[] { PdfFilterNames.DCTDecode }); 
ImageSource imageSource = new ImageSource(imageData); 
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 in order to apply transparency to this image.

Example 3: Create ImageSource from EncodedImageData with transparency

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

Properties

The properties exposed by the ImageSource class are as follows:

  • Width: Gets the width of the image.
  • Height: Gets the height of the image.
  • DecodeArray: Gets or sets the decode array, which specifies a linear mapping of each component value to a number that would be appropriate as a component value in the color space of the image. It could be used to manipulate the tones of the image, depending on its color space.

Methods

The ImageSource class exposes two methods, which could help you get the data from the ImageSource object.

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

  • BitmapSource GetBitmapSource(): Gets the BitmapSource of the image.
  • EncodedImageData GetEncodedImageData(): Returns the encoded image data. This method can be used if you need to directly export the images from the PDF document.

This example in our SDK repository demonstrates how to insert JPEG and JPEG2000 images in a PDF document without requiring that you decode the images on import. This way the exported images will not be re-encoded and their image quality will be preserved.

Extensions

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

Example 4: Create ImageSource with extension method

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

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

See Also

In this article