# ImageSource

`ImageSource` represents a single, constant set of pixels at a certain size. Multiple [Image](https://www.telerik.com/document-processing-libraries/documentation/libraries/radpdfprocessing/model/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](https://learn.microsoft.com/en-us/dotnet/api/system.io.stream), [BitmapSource](https://learn.microsoft.com/en-us/dotnet/api/system.windows.media.imaging.bitmapsource) object, or by using the [`EncodedImageData`](https://docs.telerik.com/devtools/document-processing/api/Telerik.Windows.Documents.Fixed.Model.Resources.EncodedImageData.html) 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](https://docs.telerik.com/devtools/document-processing/api/Telerik.Windows.Documents.Fixed.FormatProviders.Pdf.Export.ImageQuality.html). For more information about the `ImageQuality` and its behavior, see the [ImageQuality](https://www.telerik.com/document-processing-libraries/documentation/libraries/radpdfprocessing/concepts/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`](https://docs.telerik.com/devtools/document-processing/api/Telerik.Windows.Documents.Fixed.Model.Resources.EncodedImageData.html) 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:

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

> tip The [CreateDocumentWithImages example in the SDK repository](https://github.com/telerik/document-processing-sdk/tree/master/PdfProcessing/CreateDocumentWithImages) 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](https://www.telerik.com/document-processing-libraries/documentation/libraries/radpdfprocessing/model/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.Documents.Fixed.Model.Extensions;
ImageSource bitmapImageSource = bitmap.ToImageSource();

```

## See Also

* [Image](https://www.telerik.com/document-processing-libraries/documentation/libraries/radpdfprocessing/model/image)
* [Adding SVG FormSource into a Document](https://www.telerik.com/document-processing-libraries/documentation/libraries/radpdfprocessing/model/formsource/svg)
* [ImageSource API Reference](https://docs.telerik.com/devtools/document-processing/api/Telerik.Windows.Documents.Fixed.Model.Resources.ImageSource.html)
