
4 Answers, 1 is accepted
You can convert PdfProcessing's using Telerik.Windows.Documents.Fixed.Model.Objects.Image to .NET Framework's native image using the GetBitmapSource method:
BitmapSource bitmapSource = img.ImageSource.GetBitmapSource();
Regards,
Boby
Telerik by Progress

I would of thought so but unfortunately I get a "No imaging component suitable to complete this operation was found" exception. The image I'm trying to retrieve is an embedded Tiff. CCITT Group 4 encoded and I've managed to extract the images using Spire and PdfSharp with the PdfSharp.PdfExtension but I am already using Telerik for other parts of the app so would like to stick with it if possible. I've attached the properties of the Image for your info.
One option would be to use BitMiracle's LibTiff.Net, which is open source, to decode the image. PdfProcessing's API has properties to extract the raw stream, namely GetEncodedImageData method.
I am not expert in LibTiff.Net, but the following seems to work for the PDF documents containing TIFF we have locally:
if
(element
is
Image)
{
string
tiffName = $
"D:\\temp\\test{i++}.tif"
;
Image img = (Image)element;
var data = img.ImageSource.GetEncodedImageData();
Tiff tiff = Tiff.Open(tiffName,
"w"
);
tiff.SetField(TiffTag.IMAGEWIDTH, data.Width);
tiff.SetField(TiffTag.IMAGELENGTH, data.Height);
tiff.SetField(TiffTag.COMPRESSION, Compression.CCITTFAX4);
tiff.SetField(TiffTag.BITSPERSAMPLE, data.BitsPerComponent);
tiff.SetField(TiffTag.SAMPLESPERPIXEL, 1);
tiff.WriteRawStrip(0, data.Data, data.Data.Length);
tiff.Close();
var image = System.Drawing.Image.FromFile(tiffName);
}
Sources:
https://stackoverflow.com/questions/2641770/extracting-image-from-pdf-with-ccittfaxdecode-filter
https://stackoverflow.com/questions/401561/how-to-open-a-multi-frame-tiff-imageformat-image-in-net-2-0
Regards,
Boby
Progress Telerik

Thanks that works for me but I made a small change to be able to manipulate in memory and not have to save to file. I'm using the BitMiracle.LibTiff library.
Telerik.Windows.Documents.Fixed.Model.Objects.Image img = (Telerik.Windows.Documents.Fixed.Model.Objects.Image)element;
ImageSource imageSource = img.ImageSource;
EncodedImageData encodedImageData = imageSource.GetEncodedImageData();
Bitmap bmp;
using (MemoryStream ms = new MemoryStream()) {
TiffStream ts = new TiffStream();
Tiff tiffInline = Tiff.ClientOpen("inline", "w", ms, ts);
tiffInline.SetField(TiffTag.IMAGEWIDTH, encodedImageData.Width);
tiffInline.SetField(TiffTag.IMAGELENGTH, encodedImageData.Height);
tiffInline.SetField(TiffTag.COMPRESSION, Compression.CCITTFAX4);
tiffInline.SetField(TiffTag.BITSPERSAMPLE, encodedImageData.BitsPerComponent);
tiffInline.SetField(TiffTag.SAMPLESPERPIXEL, 1);
tiffInline.WriteRawStrip(0, encodedImageData.Data, encodedImageData.Data.Length);
tiffInline.Close();
using (MemoryStream tmpMs = new MemoryStream(ms.ToArray()))
{
TiffBitmapDecoder decoder = new TiffBitmapDecoder(tmpMs, BitmapCreateOptions.PreservePixelFormat, BitmapCacheOption.Default);
BitmapSource bitmapSource = decoder.Frames[0];
bmp = BitmapFromSource(bitmapSource) ;
}
ms.Close();
}