Hot to create image from EncodedImageData

2 Answers 450 Views
PdfProcessing
Sanjin
Top achievements
Rank 1
Sanjin asked on 24 Nov 2021, 03:33 PM

I am doing images extraction from PDF. Having trouble to create image (in any format) from EncodedImageData (https://docs.telerik.com/devtools/document-processing/api/telerik.windows.documents.fixed.model.resources.encodedimagedata) structre.

Can someone post a sample code. I tired with this sample (https://www.telerik.com/forums/manipulate-image-elements) but without success.

My image have FlateDecode filter, "DeviceRGB" ColorSpace and 8 BitsPerComponent (see attached).

Environment is .NET Core.

2 Answers, 1 is accepted

Sort by
0
Martin
Telerik team
answered on 26 Nov 2021, 11:18 AM

Hello Sanjin,

If I understand your case right you need to extract an image from a PDF document. If so, you can get the EncodedImageData and depending on the filter to export it to an image file. Check the following example:

EncodedImageData encodedImageData = image.ImageSource.GetEncodedImageData();
byte[] data = encodedImageData.Data;

bool isTransperant = image.ImageSource.GetEncodedImageData().AlphaChannel != null;
if (encodedImageData.Filters.Contains("DCTDecode") && !isTransperant)
{
	File.WriteAllBytes(Path.Combine(outputDirectoryName, $"fileName{++counter}.jpeg"), data);
}
else if (encodedImageData.Filters.Contains("FlateDecode") | isTransperant)
{
	BitmapSource bitmapSource = image.GetBitmapSource();
	using (FileStream fileStream = new FileStream(Path.Combine(outputDirectoryName, $"fileName{++counter}.png"), FileMode.Create))
	{
		BitmapEncoder encoder = new PngBitmapEncoder();
		encoder.Frames.Add(BitmapFrame.Create(bitmapSource));
		encoder.Save(fileStream);
	}
}
else if (encodedImageData.Filters.Contains("JPXDecode"))
{
	File.WriteAllBytes(Path.Combine(outputDirectoryName, $"fileName{++counter}.jp2"), data);
}
I am attaching the sample project I created as well. Please, feel free to modify it in a way closer to your scenario.

Regards,
Martin
Progress Telerik

Love the Telerik and Kendo UI products and believe more people should try them? Invite a fellow developer to become a Progress customer and each of you can get a $50 Amazon gift voucher.

Sanjin
Top achievements
Rank 1
commented on 26 Nov 2021, 04:22 PM

Thank you very much Martin for your answer.

You sample works brilliantly for legacy .NET framework but unfortunately I am trying to create images from their encoded data in .NET core environment -without depending on the System.Drawing namespace or System.Drawing.Common nuget (e.g. only using ImageSharp library).

But thanks again, maybe at the end I will need to make compromise and use .NET Framework (and your sample code) for that part of my app.

Regards,

Sanjin

0
Martin
Telerik team
answered on 30 Nov 2021, 01:28 PM

Hello Sanjin,

The current implementation of the .NET Standard version of the PdfProcessing library doesn't provide an option to get the decoded image data of images with Flatedecode filter applied, so the best approach, for now, if it is possible is to use the .NET Framework version.

I logged a feature request on your behalf to provide such support: PdfProcessing: NetStandard: Provide an API for obtaining the decoded image data. You can cast your vote for the implementation as well as subscribe to the tasks by clicking the Follow button so you can be notified when its status changes.

In appreciation for bringing this to our attention, I updated your Telerik points.

Regards,
Martin
Progress Telerik

Love the Telerik and Kendo UI products and believe more people should try them? Invite a fellow developer to become a Progress customer and each of you can get a $50 Amazon gift voucher.

Tags
PdfProcessing
Asked by
Sanjin
Top achievements
Rank 1
Answers by
Martin
Telerik team
Share this question
or