Aspose.pdf Add Image - A generic error occurred in GDI+

Hi,

​I am getting an exception when trying to add an image to a PDF page I have created.
​I have example code below as well as the exception I am receiving. I have been through all the support forum entries I could find, as well as stack overflow, and have yet to find a solution.

The barcode is being created correctly, as i have tested this by writing it to a file.
​I have tried writing it to the file then creating a fileStream from it and using that for the AddImage method but that also does not work.​
​​
​I am using Aspose.PDF version 18.1.0.0.​

​Thank you.

​Exception:

System.Runtime.InteropServices.ExternalException
HResult=0x80004005
Message=A generic error occurred in GDI+.
Source=System.Drawing
StackTrace:
at System.Drawing.Imaging.Metafile..ctor(Stream stream)

​Main Method - Add image

​{
​
​ Aspose.Pdf.Document genPdf = new Aspose.Pdf.Document();
Aspose.Pdf.Page genPage = genPdf.Pages.Add();
genPage.PageInfo.Margin.Right = 5;
genPage.PageInfo.Margin.Top = 20;
genPage.PageInfo.Margin.Left = 5;
genPage.PageInfo.Width = 612;

Aspose.Pdf.Rectangle docIDBarcodeRect = new Aspose.Pdf.Rectangle(200, 525, 410, 750);
genPage.AddImage(genDocIDBarcode(Context.DocTypeId), docIDBarcodeRect);

​}

///Barcode Generation method

​private MemoryStream genDocIDBarcode(int sequenceNumber)
{
MemoryStream barcode = new MemoryStream();
using (Aspose.BarCode.BarCodeBuilder builder = new Aspose.BarCode.BarCodeBuilder())
{
builder.EncodeType = Aspose.BarCode.Generation.EncodeTypes.Code128;
builder.CodeText = sequenceNumber.ToString().PadLeft(9, '0');
builder.CodeTextFont = new System.Drawing.Font("Verdana", 16f);
//builder.Resolution = new Aspose.BarCode.Resolution(840, 900, Aspose.BarCode.ResolutionMode.Graphics); //x4
builder.Resolution = new Aspose.BarCode.Resolution(1680, 1800, Aspose.BarCode.ResolutionMode.Graphics); //x8
//builder.Resolution = new Aspose.BarCode.Resolution(3360, 3600, Aspose.BarCode.ResolutionMode.Graphics); //x16


builder.CodeTextAlignment = System.Drawing.StringAlignment.Far;
builder.Save(barcode, Aspose.BarCode.BarCodeImageFormat.Png);
}


return barcode;
}

To note, this also happens in the example for AddImage in this repo: GitHub - aspose-pdf/Aspose.PDF-for-.NET: Aspose.PDF for .NET examples, plugins and showcase projects

@todd0202,

We have tested your code with the latest version 18.1 of Aspose.Pdf and Aspose.BarCode for .NET APIs, and unable to replicate the said error in our environment. Kindly let us know the barcode text value which you are using to generate the barcode image.

@imran.rafique

The barcode text is just “000000001”.
I am able to turn off that exception and it ends up producing the barcode and adding the image successfully. But it still is showing that is is catching the exception for each of the calls it makes.

To note again, I have tested this with the example solution you guys provided in GitHub - aspose-pdf/Aspose.PDF-for-.NET: Aspose.PDF for .NET examples, plugins and showcase projects

You must turn on all exceptions in visual studio to get it to throw.

Notes:
Win7 64bit Enterprise
Tested in Visual studio 2015 & 2017

Thank you.

@todd0202,

Kindly share the web address of the actual code example on GitHub and narrate all steps to replicate this error. We have executed your scenario with the latest versions Aspose APIs and could replicate the error. Your response is awaited.

@imran.rafique

Code address: https://github.com/aspose-pdf/Aspose.Pdf-for-.NET/blob/master/Examples/CSharp/AsposePDF/Images/AddImage.cs

  1. Open up solution in visual studio
  2. Un-comment line 106 in “RunExamples”.cs - The line that contains “Aspose.Pdf.Examples.CSharp.AsposePDF.Images.AddImage.Run();”
  3. Go to Exception Eettings in Visual Studio (Ctl + Alt + E, or search in quick launch bar)
  4. Make sure all options under Win32 Exceptions and Common Language Runtime Exceptions are enabled (checked).
  5. Run the project “CSharp” in debug mode
  6. Notice exception when adding image.

Thank you.

@todd0202,

Did you try with the latest copy of GitHub project and in what line of code you can see this error. Kindly send us a snapshot. We have downloaded the latest copy of the GitHub project and followed all your narrated steps, but could not replicate the error. Snapshot: https://i.imgur.com/reUoDIq.png

@imran.rafique

It was ran on the latest version of the code in git hub.

Here is a screen shot of where the exception occurs:Capture.PNG (144.7 KB)

@todd0202,

This type of error sometimes occurs when the memory is not released. Please try to load the image with Using statement, and then let us know how that goes into your environment.

@imran.rafique

That did not seem to solve the issue. See image below:

Capture2.PNG (30.6 KB)

@todd0202,

We have followed all these steps and could not replicate this error in our environment. Kindly send all details of your environment, including operating system (name and edition), Visual Studio and local language settings, etc. Please also create a small project application, which reproduces this error in your environment, and then send us a Zip of this project. We will investigate and share our findings with you.