Spire.PDF is a professional PDF library applied to creating, writing, editing, handling and reading PDF files without any external dependencies. Get free and professional technical support for Spire.PDF for .NET, Java, Android, C++, Python.

Fri Jan 19, 2018 1:44 pm

I'm attempting to use Azure Functions to create an image of the first page of PDFs as they are sent in (think thumbnail). Running locally I have no issues, but running on a published version of the function I get:

System.ArgumentException : Parameter is not valid.
at System.Drawing.Graphics.GetHdc()
at spr᫮.ᜀ(Stream A_0,Int32 A_1,Int32 A_2)
at spr᫮.ᜀ(Single A_0,Boolean A_1)
at Spire.Pdf.PdfDocumentBase.ᜀ(Stream A_0,Int32 A_1)
at Spire.Pdf.PdfDocumentBase.ᜀ(Int32 A_0,PdfImageType A_1)
at Spire.Pdf.PdfDocument.SaveAsImage(Int32 pageIndex,PdfImageType type)

Now, I did see a post in the Spire.Doc forum where they were getting the same error hosting on free sites and what not. The solution was for them to switch to an Azure Cloud Service or Virtual Machine. However, that was almost 4 years ago. I'm hoping there has been progress for a fix for this?

TSeale
 
Posts: 4
Joined: Fri Jan 19, 2018 1:36 pm

Fri Jan 19, 2018 3:23 pm

If I switch from a consumption plan to a hosted (S1) plan, the error changes:

System.Runtime.InteropServices.ExternalException : A generic error occurred in GDI+.
at System.Drawing.Imaging.Metafile..ctor(Stream stream,IntPtr referenceHdc,RectangleF frameRect,MetafileFrameUnit frameUnit,EmfType type,String description)
at System.Drawing.Imaging.Metafile..ctor(Stream stream,IntPtr referenceHdc,RectangleF frameRect,MetafileFrameUnit frameUnit,EmfType type)
at spr᫮.ᜀ(Stream A_0,Int32 A_1,Int32 A_2)
at spr᫮.ᜀ(Single A_0,Boolean A_1)
at Spire.Pdf.PdfDocumentBase.ᜀ(Stream A_0,Int32 A_1)
at Spire.Pdf.PdfDocumentBase.ᜀ(Int32 A_0,PdfImageType A_1)
at Spire.Pdf.PdfDocument.SaveAsImage(Int32 pageIndex,PdfImageType type)

So then I changed from emf save to bmp save. everything works. However, I don't want to be on a hosted plan. I'm about to try switching from hosted back to consumption with the bmp save just to see.

And now that's done, and........ that worked! so, if anyone else is having the issue, just do:

Image bmp = doc.SaveAsImage(pageNumber);
//Image emf = doc.SaveAsImage(pageNumber, Spire.Pdf.Graphics.PdfImageType.Metafile);
//Image zoomImg = new Bitmap((int)(emf.Size.Width * scale), (int)(emf.Size.Height * scale));
using (Graphics g = Graphics.FromImage(bmp))
{
g.ScaleTransform(scale, scale);
g.DrawImage(bmp, new System.Drawing.Rectangle(new Point(0, 0), bmp.Size), new System.Drawing.Rectangle(new Point(0, 0), bmp.Size), GraphicsUnit.Pixel);
}

using (var ms = new MemoryStream())
{
bmp.Save(ms, ImageFormat.Png);
return ms.ToArray();
}

Do not do the emf/zoomImg bit.

TSeale
 
Posts: 4
Joined: Fri Jan 19, 2018 1:36 pm

Fri Jan 19, 2018 3:24 pm

Well, the scaling is broken now, but the image saves. Off to figure out the scaling. Good talks everyone! 8)

TSeale
 
Posts: 4
Joined: Fri Jan 19, 2018 1:36 pm

Fri Jan 19, 2018 3:32 pm

final working:

Image bmp = doc.SaveAsImage(pageNumber);
Image zoomImg = new Bitmap((int)(bmp.Size.Width * scale), (int)(bmp.Size.Height * scale));
using (Graphics g = Graphics.FromImage(zoomImg))
{
g.ScaleTransform(scale, scale);
g.DrawImage(bmp, new System.Drawing.Rectangle(new Point(0, 0), bmp.Size), new System.Drawing.Rectangle(new Point(0, 0), bmp.Size), GraphicsUnit.Pixel);
}

using (var ms = new MemoryStream())
{
zoomImg.Save(ms, ImageFormat.Png);
return ms.ToArray();
}

TSeale
 
Posts: 4
Joined: Fri Jan 19, 2018 1:36 pm

Mon Jan 22, 2018 6:46 am

Hello,

Thanks for your post and workaround. If you have any other question, please feel free to contact us.

Best regards,
Simon
E-iceblue support team
User avatar

Simon.yang
 
Posts: 620
Joined: Wed Jan 11, 2017 2:03 am

Return to Spire.PDF