Issue 927 in pdfium: Extract images in the PDF

1,014 views
Skip to first unread message

vijayana… via monorail

unread,
Nov 1, 2017, 10:33:14 PM11/1/17
to pdfiu...@googlegroups.com
Status: New
Owner: ----
Labels: Type-Defect Priority-Medium

New issue 927 by vijayana...@gmail.com: Extract images in the PDF
https://bugs.chromium.org/p/pdfium/issues/detail?id=927

I am trying to extract the images available in the PDF from Pdfium public methods. I am accessing pdfium from c# using DllImport option. I am using the below lines of code:
var bmHandle = NativeMethods.FPDFImageObj_GetBitmap(imageObj);
var width = NativeMethods.FPDFBitmap_GetWidth(bmHandle);
var height = NativeMethods.FPDFBitmap_GetHeight(bmHandle);
var stride = NativeMethods.FPDFBitmap_GetStride(bmHandle);

var buffer = NativeMethods.FPDFBitmap_GetBuffer(bmHandle);

var bitmap = new Bitmap(width, height, stride, PixelFormat.Format32bppArgb, buffer);

bitmap.Save("image.png");


I am getting error "A generic error occurred in GDI+". Not sure if i'm doing something wrong. I really appreciate if there is any help.

I am using Windows 7 and the latest pdfium.dll




--
You received this message because:
1. The project was configured to send all issue notifications to this address

You may adjust your notification preferences at:
https://bugs.chromium.org/hosting/settings

dsincl… via monorail

unread,
Nov 2, 2017, 8:49:51 AM11/2/17
to pdfiu...@googlegroups.com

Comment #1 on issue 927 by dsin...@chromium.org: Extract images in the PDF
https://bugs.chromium.org/p/pdfium/issues/detail?id=927#c1

Does it tell you which line the error occurred on? That seems more like it would be an error in the bitmap.Save then in the PDF code unless the bitmap is invalid.

vijayana… via monorail

unread,
Nov 2, 2017, 9:30:24 AM11/2/17
to pdfiu...@googlegroups.com

Comment #2 on issue 927 by vijayana...@gmail.com: Extract images in the PDF
https://bugs.chromium.org/p/pdfium/issues/detail?id=927#c2

Below is my stack trace and it is not showing which line the error occurred on:

at System.Drawing.Image.Save(String filename, ImageCodecInfo encoder, EncoderParameters encoderParams)
at PdfCompare.PdfPage.GetBitmap(IntPtr imageObj, Int32 page, Int32 pos, IntPtr docHandle) in C:\dev\code\pdfcompare\pdfium\10272017\PdfCompare\PdfCompare\PdfPage.cs:line 215

Maybe I can put my questions like "Am I using the correct public method to extract an image? (FPDFBitmap_GetBuffer)" I am looking for a public method that returns Intptr object of the image.

thes… via monorail

unread,
Nov 2, 2017, 4:56:42 PM11/2/17
to pdfiu...@googlegroups.com

Comment #3 on issue 927 by the...@chromium.org: Extract images in the PDF
https://bugs.chromium.org/p/pdfium/issues/detail?id=927#c3

My question is, what is "Bitmap" here? I'm guessing that's not PDFium code. So if it's failing, one needs to figure out what kind of input it expects.

Maybe Format32bppArgb is the wrong format? How do you know that's the format of bitmap? Did you try calling FPDFBitmap_GetFormat() to see if the bitmap is actually ARGB? FWIW, FPDFBitmap_GetFormat()'s possible return values list doesn't contain ARGB.

vijayana… via monorail

unread,
Nov 2, 2017, 5:36:54 PM11/2/17
to pdfiu...@googlegroups.com

Comment #4 on issue 927 by vijayana...@gmail.com: Extract images in the PDF
https://bugs.chromium.org/p/pdfium/issues/detail?id=927#c4

Bitmap is a C# class to work with images.

Yes, I have used FPDFBitmap_GetFormat() to see if the bitmap is ARGB.

BTW, I figured out the way to extract the image by using the below lines of code:

var imageObj = NativeMethods.FPDFPage_GetObject(pageHandle, index);
uint length = NativeMethods.FPDFImageObj_GetImageDataRaw(imageObj, null, 0);
byte[] buffer = new byte[length];
NativeMethods.FPDFImageObj_GetImageDataRaw(imageObj, buffer, length);

MemoryStream ms = new MemoryStream(buffer);
Image i = Image.FromStream(ms);
i.Save("image.png");

Thank you for the support!

Please close the ticket.

thes… via monorail

unread,
Nov 2, 2017, 5:39:29 PM11/2/17
to pdfiu...@googlegroups.com
Updates:
Status: Invalid

Comment #5 on issue 927 by the...@chromium.org: Extract images in the PDF
https://bugs.chromium.org/p/pdfium/issues/detail?id=927#c5

Glad you worked it out. Closing.

Please try using the PDFium mailing list for support questions in the future, rather than filing bugs for things that aren't bugs.

vijayana… via monorail

unread,
Nov 3, 2017, 9:19:15 AM11/3/17
to pdfiu...@googlegroups.com

Comment #6 on issue 927 by vijayana...@gmail.com: Extract images in the PDF
https://bugs.chromium.org/p/pdfium/issues/detail?id=927#c6

Sure, thank you!
Reply all
Reply to author
Forward
0 new messages