diff --git a/knowledge-base/create-custom-image-properties-resolver-net-standard.md b/knowledge-base/create-custom-image-properties-resolver-net-standard.md index 29d7e303..9afdcd26 100644 --- a/knowledge-base/create-custom-image-properties-resolver-net-standard.md +++ b/knowledge-base/create-custom-image-properties-resolver-net-standard.md @@ -23,6 +23,11 @@ res_type: kb RadPdfProcessing Yoan Karamanov + + 2.0.0* + SixLabors.ImageSharp + - + @@ -34,158 +39,161 @@ res_type: kb ## Solution -The following code snippets demonstrates how to create a custom implementation of the ImagePropertiesResolver abstract class using the [SixLabors.ImageSharp](https://github.com/SixLabors/ImageSharp) library and set it to the ImagePropertiesResolver property of the FixedExtensibilityManager. +The following code snippets demonstrate how to create a custom implementation of the ImagePropertiesResolver abstract class using the [SixLabors.ImageSharp](https://github.com/SixLabors/ImageSharp) library and set it to the ImagePropertiesResolver property of the FixedExtensibilityManager. #### __[C#] Create a custom implementation inheriting the ImagePropertiesResolverBase abstract class__ -{{region kb-create-custom-image-properties-resolver1}} +```csharp - public class ImagePropertiesResolver : ImagePropertiesResolverBase +public class ImagePropertiesResolver : ImagePropertiesResolverBase +{ + public override Telerik.Documents.Primitives.Size GetImageSize(byte[] imageData) { - public override Telerik.Documents.Primitives.Size GetImageSize(byte[] imageData) + var size = Telerik.Documents.Primitives.Size.Empty; + try { - var size = Telerik.Documents.Primitives.Size.Empty; - try + using (SixLabors.ImageSharp.Image image = SixLabors.ImageSharp.Image.Load(imageData)) { - using (ImageSharp image = ImageSharp.Load(imageData)) - { - size = new Telerik.Documents.Primitives.Size(image.Width, image.Height); - } + size = new Telerik.Documents.Primitives.Size(image.Width, image.Height); } - catch (UnknownImageFormatException ex) - { - // Image format not recognized. - throw new NotSupportedException("Unsupported image format.", ex); - } - - return size; } - - public override bool TryGetRawImageData(byte[] imageData, out byte[] rawRgbData, out byte[] rawAlpha, out Telerik.Documents.Primitives.Size size) + catch (SixLabors.ImageSharp.UnknownImageFormatException ex) { - try - { - IImageFormat imageFormat; - using (ImageSharp image = ImageSharp.Load(imageData, out imageFormat)) - { - size = new Telerik.Documents.Primitives.Size(image.Width, image.Height); - - IImageDecoder decoder = null; - Dictionary decoderSwitch = new Dictionary - { - { typeof(PngFormat), () => decoder = new PngDecoder() }, - { typeof(BmpFormat), () => decoder = new BmpDecoder() }, - { typeof(GifFormat), () => decoder = new GifDecoder() }, - { typeof(JpegFormat), () => decoder = new JpegDecoder() }, - { typeof(PbmFormat), () => decoder = new PbmDecoder() }, - { typeof(TgaFormat), () => decoder = new TgaDecoder() }, - { typeof(TiffFormat), () => decoder = new TiffDecoder() }, - { typeof(WebpFormat), () => decoder = new WebpDecoder() }, - }; - - if (decoderSwitch.ContainsKey(imageFormat.GetType())) - { - decoderSwitch[imageFormat.GetType()](); - } - else - { - rawRgbData = null; - rawAlpha = null; - - return false; - } + // Image format not recognized. + throw new NotSupportedException("Unsupported image format.", ex); + } - Configuration configuration = new Configuration(); - ImageSharp decodedImage = decoder.Decode(configuration, new MemoryStream(imageData)); + return size; + } - ImageFrame frame = decodedImage.Frames[0]; + public override bool TryGetRawImageData(byte[] imageData, out byte[] rawRgbData, out byte[] rawAlpha, out Telerik.Documents.Primitives.Size size) + { + try + { + IImageFormat imageFormat; + using (SixLabors.ImageSharp.Image image = SixLabors.ImageSharp.Image.Load(imageData, out imageFormat)) + { + size = new Telerik.Documents.Primitives.Size(image.Width, image.Height); - ImageFrame frameRgb24 = frame as ImageFrame; - if (frameRgb24 != null) + IImageDecoder decoder = null; + Dictionary decoderSwitch = new Dictionary { - GetRawDataFromRgbSource(frameRgb24, out rawRgbData); - rawAlpha = null; + { typeof(PngFormat), () => decoder = new PngDecoder() }, + { typeof(BmpFormat), () => decoder = new BmpDecoder() }, + { typeof(GifFormat), () => decoder = new GifDecoder() }, + { typeof(JpegFormat), () => decoder = new JpegDecoder() }, + { typeof(PbmFormat), () => decoder = new PbmDecoder() }, + { typeof(TgaFormat), () => decoder = new TgaDecoder() }, + { typeof(TiffFormat), () => decoder = new TiffDecoder() }, + { typeof(WebpFormat), () => decoder = new WebpDecoder() }, + }; + + if (decoderSwitch.ContainsKey(imageFormat.GetType())) + { + decoderSwitch[imageFormat.GetType()](); + } + else + { + rawRgbData = null; + rawAlpha = null; - return true; - } + return false; + } - ImageFrame frameRgba32 = frame as ImageFrame; - if (frameRgba32 != null) - { - GetRawDataFromRgbaSource(frameRgba32, out rawRgbData, out rawAlpha); + SixLabors.ImageSharp.Configuration configuration = new SixLabors.ImageSharp.Configuration(); + SixLabors.ImageSharp.Image decodedImage = decoder.Decode(configuration, new MemoryStream(imageData)); - return true; - } + SixLabors.ImageSharp.ImageFrame frame = decodedImage.Frames[0]; - throw new NotSupportedException("Not supported image pixel format."); - } - } - catch (Exception ex) - { - if (ex is UnknownImageFormatException || ex is ImageProcessingException) + SixLabors.ImageSharp.ImageFrame frameRgb24 = frame as SixLabors.ImageSharp.ImageFrame; + if (frameRgb24 != null) { - rawRgbData = null; + GetRawDataFromRgbSource(frameRgb24, out rawRgbData); rawAlpha = null; - size = new Telerik.Documents.Primitives.Size(); - return false; + return true; } - else + + SixLabors.ImageSharp.ImageFrame frameRgba32 = frame as SixLabors.ImageSharp.ImageFrame; + if (frameRgba32 != null) { - throw ex; + GetRawDataFromRgbaSource(frameRgba32, out rawRgbData, out rawAlpha); + + return true; } + + throw new NotSupportedException("Not supported image pixel format."); } } - - private static void GetRawDataFromRgbSource(ImageFrame source, out byte[] data) + catch (Exception ex) { - byte[] pixels = new byte[source.PixelBuffer.Width * source.PixelBuffer.Height * 3]; - Span span = new Span(pixels); - source.CopyPixelDataTo(span); - data = span.ToArray(); + if (ex is SixLabors.ImageSharp.UnknownImageFormatException || ex is SixLabors.ImageSharp.ImageProcessingException) + { + rawRgbData = null; + rawAlpha = null; + size = new Telerik.Documents.Primitives.Size(); + + return false; + } + else + { + throw ex; + } } + } - private static void GetRawDataFromRgbaSource(ImageFrame source, out byte[] data, out byte[] alpha) - { - byte[] pixels = new byte[source.PixelBuffer.Width * source.PixelBuffer.Height * 4]; - Span span = new Span(pixels); - source.CopyPixelDataTo(span); + private static void GetRawDataFromRgbSource(SixLabors.ImageSharp.ImageFrame source, out byte[] data) + { + byte[] pixels = new byte[source.PixelBuffer.Width * source.PixelBuffer.Height * 3]; + Span span = new Span(pixels); + source.CopyPixelDataTo(span); + data = span.ToArray(); + } - data = new byte[source.PixelBuffer.Width * source.PixelBuffer.Height * 3]; - alpha = new byte[source.PixelBuffer.Width * source.PixelBuffer.Height]; - bool shouldExportAlpha = false; + private static void GetRawDataFromRgbaSource(SixLabors.ImageSharp.ImageFrame source, out byte[] data, out byte[] alpha) + { + byte[] pixels = new byte[source.PixelBuffer.Width * source.PixelBuffer.Height * 4]; + Span span = new Span(pixels); + source.CopyPixelDataTo(span); - for (int i = 0; i < pixels.Length; i += 4) - { - byte r = pixels[i]; - byte g = pixels[i + 1]; - byte b = pixels[i + 2]; - byte a = pixels[i + 3]; + data = new byte[source.PixelBuffer.Width * source.PixelBuffer.Height * 3]; + alpha = new byte[source.PixelBuffer.Width * source.PixelBuffer.Height]; + bool shouldExportAlpha = false; - data[3 * i / 4] = r; - data[3 * i / 4 + 1] = g; - data[3 * i / 4 + 2] = b; - alpha[i / 4] = a; + for (int i = 0; i < pixels.Length; i += 4) + { + byte r = pixels[i]; + byte g = pixels[i + 1]; + byte b = pixels[i + 2]; + byte a = pixels[i + 3]; - if (a != 255) - { - shouldExportAlpha = true; - } - } + data[3 * i / 4] = r; + data[3 * i / 4 + 1] = g; + data[3 * i / 4 + 2] = b; + alpha[i / 4] = a; - if (!shouldExportAlpha) + if (a != 255) { - alpha = null; + shouldExportAlpha = true; } } + + if (!shouldExportAlpha) + { + alpha = null; + } } -{{endregion}} +``` #### __[C#] Set the custom implementation to the ImagePropertiesResolver property of the FixedExtensibilityManager__ -{{region kb-create-custom-image-properties-resolver2}} +```csharp ImagePropertiesResolver customImagePropertiesResolver = new ImagePropertiesResolver(); FixedExtensibilityManager.ImagePropertiesResolver = customImagePropertiesResolver; -{{endregion}} +``` + +## See Also + +- [Cross-Platform Images Support in PdfProcessing]({%slug radpdfprocessing-cross-platform-images%}) \ No newline at end of file