@@ -23,6 +23,11 @@ res_type: kb
2323 <td>RadPdfProcessing</td>
2424 <td><a href="https://www.telerik.com/blogs/author/yoan-karamanov">Yoan Karamanov</a></td>
2525 </tr>
26+ <tr>
27+ <td>2.0.0*</td>
28+ <td>SixLabors.ImageSharp</td>
29+ <td>-</td>
30+ </tr>
2631</tbody >
2732</table >
2833
@@ -34,150 +39,149 @@ res_type: kb
3439
3540## Solution
3641
37- 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.
42+ 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.
3843
3944#### __ [ C#] Create a custom implementation inheriting the ImagePropertiesResolverBase abstract class__
4045
4146{{region kb-create-custom-image-properties-resolver1}}
4247
43- public class ImagePropertiesResolver : ImagePropertiesResolverBase
48+ public class ImagePropertiesResolver : ImagePropertiesResolverBase
49+ {
50+ public override Telerik.Documents.Primitives.Size GetImageSize(byte[ ] imageData)
4451 {
45- public override Telerik.Documents.Primitives.Size GetImageSize(byte[] imageData)
52+ var size = Telerik.Documents.Primitives.Size.Empty;
53+ try
4654 {
47- var size = Telerik.Documents.Primitives.Size.Empty;
48- try
55+ using (SixLabors.ImageSharp.Image image = SixLabors.ImageSharp.Image.Load(imageData))
4956 {
50- using (ImageSharp image = ImageSharp.Load(imageData))
51- {
52- size = new Telerik.Documents.Primitives.Size(image.Width, image.Height);
53- }
57+ size = new Telerik.Documents.Primitives.Size(image.Width, image.Height);
5458 }
55- catch (UnknownImageFormatException ex)
56- {
57- // Image format not recognized.
58- throw new NotSupportedException("Unsupported image format.", ex);
59- }
60-
61- return size;
6259 }
63-
64- public override bool TryGetRawImageData(byte[] imageData, out byte[] rawRgbData, out byte[] rawAlpha, out Telerik.Documents.Primitives.Size size)
60+ catch (SixLabors.ImageSharp.UnknownImageFormatException ex)
6561 {
66- try
67- {
68- IImageFormat imageFormat;
69- using (ImageSharp image = ImageSharp.Load(imageData, out imageFormat))
70- {
71- size = new Telerik.Documents.Primitives.Size(image.Width, image.Height);
72-
73- IImageDecoder decoder = null;
74- Dictionary<Type, Action> decoderSwitch = new Dictionary<Type, Action>
75- {
76- { typeof(PngFormat), () => decoder = new PngDecoder() },
77- { typeof(BmpFormat), () => decoder = new BmpDecoder() },
78- { typeof(GifFormat), () => decoder = new GifDecoder() },
79- { typeof(JpegFormat), () => decoder = new JpegDecoder() },
80- { typeof(PbmFormat), () => decoder = new PbmDecoder() },
81- { typeof(TgaFormat), () => decoder = new TgaDecoder() },
82- { typeof(TiffFormat), () => decoder = new TiffDecoder() },
83- { typeof(WebpFormat), () => decoder = new WebpDecoder() },
84- };
85-
86- if (decoderSwitch.ContainsKey(imageFormat.GetType()))
87- {
88- decoderSwitch[imageFormat.GetType()]();
89- }
90- else
91- {
92- rawRgbData = null;
93- rawAlpha = null;
94-
95- return false;
96- }
62+ // Image format not recognized.
63+ throw new NotSupportedException("Unsupported image format.", ex);
64+ }
9765
98- Configuration configuration = new Configuration() ;
99- ImageSharp decodedImage = decoder.Decode(configuration, new MemoryStream(imageData));
66+ return size ;
67+ }
10068
101- ImageFrame frame = decodedImage.Frames[0];
69+ public override bool TryGetRawImageData(byte[] imageData, out byte[] rawRgbData, out byte[] rawAlpha, out Telerik.Documents.Primitives.Size size)
70+ {
71+ try
72+ {
73+ IImageFormat imageFormat;
74+ using (SixLabors.ImageSharp.Image image = SixLabors.ImageSharp.Image.Load(imageData, out imageFormat))
75+ {
76+ size = new Telerik.Documents.Primitives.Size(image.Width, image.Height);
10277
103- ImageFrame<Rgb24> frameRgb24 = frame as ImageFrame<Rgb24> ;
104- if (frameRgb24 != null)
78+ IImageDecoder decoder = null ;
79+ Dictionary<Type, Action> decoderSwitch = new Dictionary<Type, Action>
10580 {
106- GetRawDataFromRgbSource(frameRgb24, out rawRgbData);
107- rawAlpha = null;
81+ { typeof(PngFormat), () => decoder = new PngDecoder() },
82+ { typeof(BmpFormat), () => decoder = new BmpDecoder() },
83+ { typeof(GifFormat), () => decoder = new GifDecoder() },
84+ { typeof(JpegFormat), () => decoder = new JpegDecoder() },
85+ { typeof(PbmFormat), () => decoder = new PbmDecoder() },
86+ { typeof(TgaFormat), () => decoder = new TgaDecoder() },
87+ { typeof(TiffFormat), () => decoder = new TiffDecoder() },
88+ { typeof(WebpFormat), () => decoder = new WebpDecoder() },
89+ };
90+
91+ if (decoderSwitch.ContainsKey(imageFormat.GetType()))
92+ {
93+ decoderSwitch[imageFormat.GetType()]();
94+ }
95+ else
96+ {
97+ rawRgbData = null;
98+ rawAlpha = null;
10899
109- return true ;
110- }
100+ return false ;
101+ }
111102
112- ImageFrame<Rgba32> frameRgba32 = frame as ImageFrame<Rgba32>;
113- if (frameRgba32 != null)
114- {
115- GetRawDataFromRgbaSource(frameRgba32, out rawRgbData, out rawAlpha);
103+ SixLabors.ImageSharp.Configuration configuration = new SixLabors.ImageSharp.Configuration();
104+ SixLabors.ImageSharp.Image decodedImage = decoder.Decode(configuration, new MemoryStream(imageData));
116105
117- return true;
118- }
106+ SixLabors.ImageSharp.ImageFrame frame = decodedImage.Frames[0];
119107
120- throw new NotSupportedException("Not supported image pixel format.");
121- }
122- }
123- catch (Exception ex)
124- {
125- if (ex is UnknownImageFormatException || ex is ImageProcessingException)
108+ SixLabors.ImageSharp.ImageFrame<Rgb24> frameRgb24 = frame as SixLabors.ImageSharp.ImageFrame<Rgb24>;
109+ if (frameRgb24 != null)
126110 {
127- rawRgbData = null ;
111+ GetRawDataFromRgbSource(frameRgb24, out rawRgbData) ;
128112 rawAlpha = null;
129- size = new Telerik.Documents.Primitives.Size();
130113
131- return false ;
114+ return true ;
132115 }
133- else
116+
117+ SixLabors.ImageSharp.ImageFrame<Rgba32> frameRgba32 = frame as SixLabors.ImageSharp.ImageFrame<Rgba32>;
118+ if (frameRgba32 != null)
134119 {
135- throw ex;
120+ GetRawDataFromRgbaSource(frameRgba32, out rawRgbData, out rawAlpha);
121+
122+ return true;
136123 }
124+
125+ throw new NotSupportedException("Not supported image pixel format.");
137126 }
138127 }
139-
140- private static void GetRawDataFromRgbSource(ImageFrame<Rgb24> source, out byte[] data)
128+ catch (Exception ex)
141129 {
142- byte[] pixels = new byte[source.PixelBuffer.Width * source.PixelBuffer.Height * 3];
143- Span<byte> span = new Span<byte>(pixels);
144- source.CopyPixelDataTo(span);
145- data = span.ToArray();
130+ if (ex is SixLabors.ImageSharp.UnknownImageFormatException || ex is SixLabors.ImageSharp.ImageProcessingException)
131+ {
132+ rawRgbData = null;
133+ rawAlpha = null;
134+ size = new Telerik.Documents.Primitives.Size();
135+
136+ return false;
137+ }
138+ else
139+ {
140+ throw ex;
141+ }
146142 }
143+ }
147144
148- private static void GetRawDataFromRgbaSource(ImageFrame<Rgba32> source, out byte[] data, out byte[] alpha)
149- {
150- byte[] pixels = new byte[source.PixelBuffer.Width * source.PixelBuffer.Height * 4];
151- Span<byte> span = new Span<byte>(pixels);
152- source.CopyPixelDataTo(span);
145+ private static void GetRawDataFromRgbSource(SixLabors.ImageSharp.ImageFrame<Rgb24> source, out byte[] data)
146+ {
147+ byte[] pixels = new byte[source.PixelBuffer.Width * source.PixelBuffer.Height * 3];
148+ Span<byte> span = new Span<byte>(pixels);
149+ source.CopyPixelDataTo(span);
150+ data = span.ToArray();
151+ }
153152
154- data = new byte[source.PixelBuffer.Width * source.PixelBuffer.Height * 3];
155- alpha = new byte[source.PixelBuffer.Width * source.PixelBuffer.Height];
156- bool shouldExportAlpha = false;
153+ private static void GetRawDataFromRgbaSource(SixLabors.ImageSharp.ImageFrame<Rgba32> source, out byte[] data, out byte[] alpha)
154+ {
155+ byte[] pixels = new byte[source.PixelBuffer.Width * source.PixelBuffer.Height * 4];
156+ Span<byte> span = new Span<byte>(pixels);
157+ source.CopyPixelDataTo(span);
157158
158- for (int i = 0; i < pixels.Length; i += 4)
159- {
160- byte r = pixels[i];
161- byte g = pixels[i + 1];
162- byte b = pixels[i + 2];
163- byte a = pixels[i + 3];
159+ data = new byte[source.PixelBuffer.Width * source.PixelBuffer.Height * 3];
160+ alpha = new byte[source.PixelBuffer.Width * source.PixelBuffer.Height];
161+ bool shouldExportAlpha = false;
164162
165- data[3 * i / 4] = r;
166- data[3 * i / 4 + 1] = g;
167- data[3 * i / 4 + 2] = b;
168- alpha[i / 4] = a;
163+ for (int i = 0; i < pixels.Length; i += 4)
164+ {
165+ byte r = pixels[i];
166+ byte g = pixels[i + 1];
167+ byte b = pixels[i + 2];
168+ byte a = pixels[i + 3];
169169
170- if (a != 255)
171- {
172- shouldExportAlpha = true;
173- }
174- }
170+ data[3 * i / 4] = r;
171+ data[3 * i / 4 + 1] = g;
172+ data[3 * i / 4 + 2] = b;
173+ alpha[i / 4] = a;
175174
176- if (!shouldExportAlpha )
175+ if (a != 255 )
177176 {
178- alpha = null ;
177+ shouldExportAlpha = true ;
179178 }
180179 }
180+
181+ if (!shouldExportAlpha)
182+ {
183+ alpha = null;
184+ }
181185 }
182186
183187{{endregion}}
0 commit comments