This is a migrated thread and some comments may be shown as answers.

Manipulate image elements

4 Answers 373 Views
PdfProcessing
This is a migrated thread and some comments may be shown as answers.
Mark Stevens
Top achievements
Rank 1
Mark Stevens asked on 23 May 2017, 12:54 AM
How do I convert an image element back to an image to be able to manipulate the image.

4 Answers, 1 is accepted

Sort by
0
Boby
Telerik team
answered on 25 May 2017, 08:36 AM
Hi Mark,

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

0
Mark Stevens
Top achievements
Rank 1
answered on 25 May 2017, 07:23 PM

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.

0
Boby
Telerik team
answered on 26 May 2017, 01:06 PM
Hello Mark,

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

0
Mark Stevens
Top achievements
Rank 1
answered on 29 May 2017, 08:30 PM

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();
                            }

Tags
PdfProcessing
Asked by
Mark Stevens
Top achievements
Rank 1
Answers by
Boby
Telerik team
Mark Stevens
Top achievements
Rank 1
Share this question
or