Skip to content

Update create-custom-image-properties-resolver-net-standard.md #598

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
228 changes: 118 additions & 110 deletions knowledge-base/create-custom-image-properties-resolver-net-standard.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,11 @@ res_type: kb
<td>RadPdfProcessing</td>
<td><a href="https://www.telerik.com/blogs/author/yoan-karamanov">Yoan Karamanov</a></td>
</tr>
<tr>
<td>2.0.0*</td>
<td>SixLabors.ImageSharp</td>
<td>-</td>
</tr>
</tbody>
</table>

Expand All @@ -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<Type, Action> decoderSwitch = new Dictionary<Type, Action>
{
{ 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<Rgb24> frameRgb24 = frame as ImageFrame<Rgb24>;
if (frameRgb24 != null)
IImageDecoder decoder = null;
Dictionary<Type, Action> decoderSwitch = new Dictionary<Type, Action>
{
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<Rgba32> frameRgba32 = frame as ImageFrame<Rgba32>;
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<Rgb24> frameRgb24 = frame as SixLabors.ImageSharp.ImageFrame<Rgb24>;
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<Rgba32> frameRgba32 = frame as SixLabors.ImageSharp.ImageFrame<Rgba32>;
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<Rgb24> source, out byte[] data)
catch (Exception ex)
{
byte[] pixels = new byte[source.PixelBuffer.Width * source.PixelBuffer.Height * 3];
Span<byte> span = new Span<byte>(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<Rgba32> source, out byte[] data, out byte[] alpha)
{
byte[] pixels = new byte[source.PixelBuffer.Width * source.PixelBuffer.Height * 4];
Span<byte> span = new Span<byte>(pixels);
source.CopyPixelDataTo(span);
private static void GetRawDataFromRgbSource(SixLabors.ImageSharp.ImageFrame<Rgb24> source, out byte[] data)
{
byte[] pixels = new byte[source.PixelBuffer.Width * source.PixelBuffer.Height * 3];
Span<byte> span = new Span<byte>(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<Rgba32> source, out byte[] data, out byte[] alpha)
{
byte[] pixels = new byte[source.PixelBuffer.Width * source.PixelBuffer.Height * 4];
Span<byte> span = new Span<byte>(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%})