Q: I had found that code in ElementReaderAdvTest. I also have
confirmed that this is an inline image I am dealing with. The trouble
I have is in converting the image data into some kind of Image, either
a PDFTron.PDF.Image or a System.Drawing.Image(or Bitmap). I get an
error when I try to convert the image data returned from
getImageData() into an image, for instance:
Image2RGB img_conv = new Image2RGB(imageElement);
FilterReader reader = new FilterReader(img_conv);
byte[] image_data_out = new
byte[imageElement.GetImageDataSize()];
reader.Read(image_data_out);
Bitmap bm = new Bitmap(image_data_out);
Returns the following error:
Parameter is not valid.
at System.Drawing.Bitmap..ctor(Stream stream)
How can the image data from Element.GetImageData() be used to
construct a correct System.Drawing.Bitmap?
------------
A: Unfortunately System.Drawing.Bitmap constructor does not allow you
to directly pass-in the raw image data. You would need to create a new
Bitmap of given dimensions (i.e.
element.GetImageWidth(),element.GetImageHeight()) and pixel format
(e.g. 24-bit RGB). Then you would need to copy RGB data into the image
using bmp.LockBits(new Rect(0, 0, width, heigh), ImageLockModeWrite,
PixelFormat24bppRGB, BitmapData). You can find more on this in MSDN
documentation or GDI+/.NET related forums (e.g.
www.bobpowell.net/faqmain.htm).
While copying image data byte-by-byte you may need to skip some
padding bytes (stride-3*width) at the end of each line. This is
necessary because GDI+/.NET stores bitmap scan-lines aligned on a 4
byte boundary, whereas Image2RGB returns a data stream without any
padding bytes at the end of each line).