ImageSource
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 anImageSourceobject from a stream that contains an image. -
public ImageSource(Stream stream, FormatProviders.Pdf.Export.ImageQuality imageQuality): Creates anImageSourceobject from a stream and allows you to specify the image quality through the ImageQuality enumeration. For more information about theImageQualityand 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 newImageSourceobject from aBitmapSourceobject. This overload is not available in the .NET Standard version of the PdfProcessing packages. -
public ImageSource(BitmapSource bitmapSource, FormatProviders.Pdf.Export.ImageQuality imageQuality): Creates anImageSourceinstance from aBitmapSourceobject 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 ofImageSourceusing theEncodedImageDataclass.
Example 1 shows how to create an ImageSource using a FileStream.
Example 1: Create ImageSource from Stream
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
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
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:
| Property | Description |
|---|---|
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 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.
| Method | Description |
|---|---|
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
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
BitmapImageAPI for this platform. You can pass the image as a stream to theSetSource()method ofBitmapImageinstead.