Error putting image in PDF document

I’m trying to add an image to a PDF document using Aspose PDF:

Section section = pdf.Sections.Add();
section.PageInfo.PageWidth = PageSize.LetterWidth;
section.PageInfo.PageHeight = PageSize.LetterHeight;

FileStream stream = File.OpenRead(“C:\foo.jpg”);
using (Image sourceImage = Image.FromStream(stream))
{
Aspose.Pdf.Image PDFImage = new Aspose.Pdf.Image();
section.Paragraphs.Add(PDFImage);
PDFImage.ImageInfo.SystemImage = sourceImage;
PDFImage.ImageInfo.ImageFileType = ImageFileType.Jpeg;
}
pdf.Save(context.Response.OutputStream);


When I do this, I’m getting the following error on the PDF.save call.

[ArgumentException: Parameter is not valid.]
System.Drawing.Image.Save(Stream stream, ImageCodecInfo encoder, EncoderParameters encoderParams) +377890
System.Drawing.Image.Save(Stream stream, ImageFormat format) +36
Aspose.Pdf.Xml.ᐤ.ᐥ(Pdf ӕ, Section ა, Table բ, Row ࿖, Cell ՜, Image ٘, ӝ ӿ, ჿ Ꮗ, Boolean Ꮘ) +1150
Aspose.Pdf.Xml.ᖄ.ᖇ(Pdf ӕ, Section ჆, ӝ ӿ) +7962
Aspose.Pdf.Xml.ܻ.ၨ(Pdf ӕ) +2925
Aspose.Pdf.Xml.ᤧ.᤬(Ӟ ؃, Pdf ӕ) +732
Aspose.Pdf.Pdf.Save(Stream stream) +374
Test.ProcessRequest(HttpContext context) in c:\work\Test.ashx:51
System.Web.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() +181
System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously) +75
              </td>
           </tr>
        </tbody></table>

        <br><br>

Hi,

Here is a sample code on how to add an image to PDF document:
[C#]
Aspose.Pdf.Image image1 = new Aspose.Pdf.Image(sec1);
sec1.Paragraphs.Add(image1);
image1.ImageInfo.File = “C:/foo.jpg”;
image1.ImageInfo.ImageFileType = ImageFileType.Jpeg;

Please try it. If it still doesn’t work, please attach the image file so that we can reproduce the error. Thanks.

Best regards.

It does work when I load the image using the File property instead of using the SystemImage property.
So if I replace
PDFImage.ImageInfo.SystemImage = sourceImage;
with this
PDFImage.ImageInfo.File = “C:/foo.jpg”;
everything works.

This does seem to be a bug though, as I’m loading the same image each way.

Hi,

I have reproduced this error with your code and logged this issue as PDFNET-6124. We will try to fix it ASAP.

As a workaround you can modify your code like the following:

Bitmap b = new Bitmap("C:/images/10.jpg");
MemoryStream ms = new MemoryStream();

Aspose.Pdf.Image PDFImage = new Aspose.Pdf.Image();
section.Paragraphs.Add(PDFImage);
PDFImage.ImageInfo.SystemImage = b;
PDFImage.ImageInfo.ImageFileType = ImageFileType.Jpeg;

The issues you have found earlier (filed as 6124) have been fixed in this update.

Please note that I've just download the latest version; 4.1.0.0 and the issue is still there.

When the ImageInfo.SystemImage is set it doesn't work, swap it over to File or ImageStream and it works fine.

Hi,

I've tested the scenario using following code snippet and I'm unable to reproduce the problem. Can you please test the following code snippet or share some details on how you are using this property.

[C#]

Pdf pdf1 = new Pdf();
Aspose.Pdf.Section sec = pdf1.Sections.Add();

FileStream fs = File.Open(@"d:/pdftest/Aspose.JPG", FileMode.Open);
System.Drawing.Image sourceimage = System.Drawing.Image.FromStream(fs);

Aspose.Pdf.Image PDFImage = new Aspose.Pdf.Image();
PDFImage.ImageInfo.SystemImage = sourceimage;
PDFImage.ImageInfo.ImageFileType = ImageFileType.Jpeg;

sec.Paragraphs.Add(PDFImage);
pdf1.Save("d:\\pdftest\\TOC_Bookmark_Test.pdf");

As a way around, I've also used the following code snippet to test the scenario and I'm also unable to notice the issue in this case.

[C#]

Pdf pdf1 = new Pdf();
Aspose.Pdf.Section sec = pdf1.Sections.Add();

Bitmap b = new Bitmap("d:/pdftest/Aspose.JPG");
Aspose.Pdf.Image PDFImage = new Aspose.Pdf.Image();
PDFImage.ImageInfo.SystemImage = b;
PDFImage.ImageInfo.ImageFileType = ImageFileType.Jpeg;

sec.Paragraphs.Add(PDFImage);
pdf1.Save("d:\\pdftest\\TOC_Bookmark_Test.pdf");