Converting PDF to Bitmap - parameter is not valid

Has anyone come up with a solution for this? Apparently, it started happening a few years ago, but in all my searching I really haven’t found an answer.

I’m trying to convert a multi-page PDF file to individual bitmaps. Actually, my ultimate goal is to return an output stream from an HttpModule:
bmp.Save(Context.Response.OutputStream, ImageFormat.Jpeg);

The following code works:

PdfConverter conv = new PdfConverter();
conv.BindPdf(FilePath);
conv.DoConvert();
MemoryStream ms = new MemoryStream();
conv.SavaAsTIFF(ms);
Bitmap bmp = (Bitmap)Bitmap.FromStream(ms);
(I convert my bmp to pages after this)

but it’s really, really, really slow.

I don’t know if this code is any faster because it doesn’t work:

PdfFileEditor pdfEditor = new PdfFileEditor();
MemoryStream[] ms = pdfEditor.SplitToPages(FilePath);
MemoryStream ms2 = ms[CurrentPage];
ms2.Position = 0;
bmp = (Bitmap)System.Drawing.Image.FromStream(ms2);
bmp = new Bitmap(ms2);

the last two lines are alternatives, but they both fail with the invalid parameter error.

Any help greatly appreciated!

Hi Larry,

Can you please share the sample PDF file(s) with us, so we could test the issue at our end? You’ll be updated with the results accordingly.

We’re sorry for the inconvenience.
Regards,

Hi Shahzad, and thanks for responding!

Here's the PDF

Hi Larry,

I have tested this issue at my end and noticed that the following code works and can serve your purpose:

PdfConverter converter = new PdfConverter();
converter.BindPdf(“AFF200932101_651_001.pdf”);
converter.DoConvert();
MemoryStream ms = new MemoryStream();
while (converter.HasNextImage())
{
converter.GetNextImage(ms, System.Drawing.Imaging.ImageFormat.Bmp);
Bitmap bmp = (Bitmap)System.Drawing.Image.FromStream(ms);
bmp.Save(DateTime.Now.Ticks.ToString() + “.bmp”);
//or use the following single line if you want to save it to file on disk
//converter.GetNextImage(DateTime.Now.Ticks.ToString() + “.bmp”, System.Drawing.Imaging.ImageFormat.Bmp);
}

Can you please try this code at your end and see if it helps? If you think that doesn’t resolve your issue then please do let us know, so we could investigate it further.

We’re sorry for the inconvenience.
Regards,

THANK YOU!

That's much better code than what I was working with before (the previous guy was using GDI+ to separate out the pages).

However, it's still not very fast, though a little faster than using the SavaAsTiff method. It still takes some 5 seconds to load the main image and 20 more seconds to load the 5 thumbnail pages - each thumbnail is a separate page so the user can select a page from the thumbnails).

Maybe I can save the pages the first time so I don't have to reload them on every subsequent page selection.

It looks like the bottleneck now is converter.HasNextImage. That's what's taking the time.

Again, thanks! Hopefully there's some other way to speed this up.

Oh, is there a way to pick out a specific page? Suppose I just want page 3. Now I'm enumerating through the pages until I get to the page selected, then create the bmp and exit the loop.

Thanks!

Nevermind. Just set StartPage and EndPage to the page I want and the enumeration is only over that one page. This is much faster for the thumbnails!

EXCELLENT!!

With this page trick the main page is faster too. The total load time is now about 8 seconds.

I consider this problem solved!!!

THANK YOU!

Hi Larry,

We’re very much pleased to know that that this issue is resolved. If you need any further assistance or find some other issue, please do let us know.

Regards,

Hi Shahzad,

I might have to open this back up. I noticed that if there is only one page/image to the pdf, then the following appears to hang indefinitiely.

converter.HasNextImage()

I never get beyond that line. I tried converter.GetNextImage too, and that also hangs indefinitely.

Your help greatly appreciated!

Hi again,

Apparently I'm behind on Aspose releases. I just downloaded and installed the version for .net 4.3.0. What exactly do I replace with what? I'm assuming I want "bin - net35". Do I copy both the xml and ddl to my applicatin bin folder?

I'll experiment while waiting a reply.

Thanks!

I installed the newest release, copied over the xml and ddl (same res - I didn’t see a new one), and it still hangs indefinitely.

To tell you the truth, I'm becoming completely fed up with your entire suite. It's very unstable, sometimes gives me errors and sometimes doesn't on the same code, and is impossible to learn.

I tried installing your latest release and it's worse than your old one. Now I can't seem to go back, keep getting strange errors.

In your new release the line

convPDF.HasNextImage()

hangs, and sometimes gives an "Out of memory" error and other times gives a "Parameter not valid" error.

I'm going to start looking at other options for our company.

Hi Larry,

Once you have installed the new version at your end, please do the following:

1. Remove any existing reference to the component assembly, from your project
2. Build the project once without adding new reference
3. Add reference to the new DLL (i…e 4.3.0, select a version appropriate for your .NET version i.e. 1.1, 2.0 or 3.5 - we have provided separate DLLs for each)
4. Rebuild your project with new reference
5. Execute your project

If it doesn’t resolve the issue then most probably it is due to the particular contents or origin of that specific file. Please share the PDF file with us, so we could test the issue at our end. You’ll be updated with the results accordingly.

We’re sorry for the inconvenience and looking forward to help you out.
Regards,