Images
The .NET Framework version of the RadPdfProcessing library provides built-in functionality for converting images and scaling their quality. The .NET Standard version does not provide such functionality and requires manual configuration. The FixedExtensibilityManager class is exposed to address this need. The code samples later in this article demonstrate how to configure it.
Exporting Images
To reduce file size, PDF supports only a limited set of compression filters such as JPEG and JPEG2000 compression of color and grayscale images. To allow the library to export images other than JPEG and JPEG2000, you must process these images before export. The .NET Standard specification does not define APIs for converting or processing images or scaling their quality. To export images other than JPEG and JPEG2000, or to use ImageQuality other than High, PdfProcessing exposes two extensibility points through the static FixedExtensibilityManager class: ImagePropertiesResolver and JpegImageConverter.
On cross-platform targets, set
ImagePropertiesResolverorJpegImageConverterbefore export. If both properties arenull, export throws anInvalidOperationException.
Requirements
To export images other than JPEG and JPEG2000, or to use ImageQuality other than High, add references to the following .NET Standard packages:
| NuGet package | Description |
|---|---|
| Telerik.Documents.ImageUtils | Provides default image resolver and JPEG converter implementations. |
| SkiaSharp.NativeAssets.* (version 3.119.1) | May differ according to the used platform. For Linux use SkiaSharp.NativeAssets.Linux.NoDependencies. |
| SkiaSharp.Views.Blazor and wasm-tools | For Blazor WebAssembly. |
ImagePropertiesResolver
The ImagePropertiesResolver property accepts a resolver that parses image data into color and alpha information. On cross-platform targets, the resolver is required for formats such as PNG when you need transparency preserved in the exported PDF. The resolver reads the image bytes and does not apply the ImageQuality value from the export settings.
Default Implementation for ImagePropertiesResolver
PdfProcessing provides a default implementation called ImagePropertiesResolver. The built-in logic depends on the SkiaSharp library to parse the image data. To use the default functionality, set an instance of the ImagePropertiesResolver class to the FixedExtensibilityManager.ImagePropertiesResolver property.
View the implementation Requirements.
Example 1: Set the default ImagePropertiesResolver implementation
Telerik.Documents.ImageUtils.ImagePropertiesResolver defaultImagePropertiesResolver = new Telerik.Documents.ImageUtils.ImagePropertiesResolver();
Telerik.Documents.Extensibility.FixedExtensibilityManager.ImagePropertiesResolver = defaultImagePropertiesResolver;
Custom Implementation for ImagePropertiesResolver
If you have specific requirements and the default ImagePropertiesResolver does not fit them, you can implement custom logic. To achieve this:
- Inherit the
Telerik.Documents.Core.Imaging.ImagePropertiesResolverBaseclass. - Implement its members.
- Assign an instance of the custom implementation to the
FixedExtensibilityManager.ImagePropertiesResolverproperty.
JpegImageConverter
The JpegImageConverter property uses an implementation of the JpegImageConverterBase abstract class to convert an image to JPEG. Pass this implementation to the JpegImageConverter property of the FixedExtensibilityManager.
If you have both the
ImagePropertiesResolverandJpegImageConverterproperties set,ImagePropertiesResolvertakes priority and is used to parse the image.
Default Implementation for JpegImageConverter
The Telerik.Documents.ImageUtils package provides a default implementation of the JpegImageConverter class that you can use when exporting a document. The default implementation depends on the SkiaSharp library to convert images to JPEG format.
View the implementation Requirements.
Example 2: Set the default JpegImageConverter implementation
Telerik.Documents.Extensibility.JpegImageConverterBase defaultJpegImageConverter = new Telerik.Documents.ImageUtils.JpegImageConverter();
Telerik.Documents.Extensibility.FixedExtensibilityManager.JpegImageConverter = defaultJpegImageConverter;
Custom Implementation for JpegImageConverter
The following example demonstrates how to implement a custom image converter. It depends on the SixLabors.ImageSharp library. You can follow this approach with other image processing libraries according to your specific requirements.
Required NuGet Packages
- SixLabors.ImageSharp - version 3.1.12
- SixLabors.ImageSharp.Drawing - version 2.1.7
The following using/imports statements are required in the project:
- using SixLabors.ImageSharp;
- using SixLabors.ImageSharp.Formats.Jpeg;
- using SixLabors.ImageSharp.Formats.Png;
- using SixLabors.ImageSharp.PixelFormats;
- using SixLabors.ImageSharp.Processing;
Example 3: Create a custom JpegImageConverterBase implementation
// SixLabors.ImageSharp 3.1.12
// SixLabors.ImageSharp.Drawing 2.1.7
//using SixLabors.ImageSharp;
//using SixLabors.ImageSharp.Formats.Jpeg;
//using SixLabors.ImageSharp.Formats.Png;
//using SixLabors.ImageSharp.PixelFormats;
//using SixLabors.ImageSharp.Processing;
internal class CustomJpegImageConverter : Telerik.Documents.Extensibility.JpegImageConverterBase
{
public override bool TryConvertToJpegImageData(byte[] imageData, ImageQuality imageQuality, out byte[] jpegImageData)
{
jpegImageData = null;
try
{
using (var imageStream = new MemoryStream(imageData))
{
SixLabors.ImageSharp.Image image = SixLabors.ImageSharp.Image.Load(imageStream);
var imageFormat = image.Metadata.DecodedImageFormat;
// Handle transparency for PNG
if (imageFormat is PngFormat && image.PixelType.BitsPerPixel == 32)
{
var background = new Image<Rgba32>(image.Width, image.Height, Color.White);
background.Mutate(ctx => ctx.DrawImage(image, 1f));
image.Dispose(); // Dispose original
image = background; // Assign new image
}
var jpegEncoder = new JpegEncoder
{
Quality = (int)imageQuality
};
using (var ms = new MemoryStream())
{
image.Save(ms, jpegEncoder);
jpegImageData = ms.ToArray();
}
image.Dispose(); // Dispose final image
}
return true;
}
catch (SixLabors.ImageSharp.UnknownImageFormatException)
{
return false;
}
catch (SixLabors.ImageSharp.ImageProcessingException)
{
return false;
}
catch (Exception)
{
return false;
}
}
}
Example 4: Assign a custom image converter
JpegImageConverterBase customJpegImageConverter = new CustomJpegImageConverter();
Telerik.Documents.Extensibility.FixedExtensibilityManager.JpegImageConverter = customJpegImageConverter;
A complete SDK example of a custom
JpegImageConverterBaseimplementation is available on the GitHub repository.