Adding Images

I’ve seen some new additions for using images, but I can’t seem to find corresponding documentation. For example, I can’t seem to find an example showing how to add an image via a base64 encoded string or byte array.

Also, I’m trying to use
Image.FromSystemImage() using an embedded image as my source, and I keep getting “Invalid image File name” as the error.

When I try to use a MemoryStream I pass in, I keep getting an error that the stream is closed, even though it’s open and readable, even at the time the exception is thrown.

Any help here would be great, kind of stuck with each way of adding an image.

-Jeff

Hello Jeff,

Thanks for considering Aspose

Concerning your question for using Byte array we used to have a Memory Data Property of imgaInfo class but now its obsolete. Aspose apologizes for any inconvenience you may have experienced. Please use ImageStream instead.

While accessing an image file form system through FromSystemImage, first you need to set path for Image object of Drawing class

System.Drawing.Image sysimg = System.Drawing.Image.FromFile("test.jpg");

where "test.jpg" is the name of the file, placed in the same folder. Once the path is set than we will call FromSystemImage() method in which we will pass sysimg as argument that holds the address of the source image file.

Aspose.Pdf.Image img1 = Aspose.Pdf.Image.FromSystemImage(sysimg);

For more information kindly check FromSystemImage

Another way for embedding an image into a PDF documents is through Aspose.Pdf.Image class. Using ImageInfo object that is encapsulated in Image class, we can set the path of local disk image and its image file type.

//Set the path of image file

image1.ImageInfo.File = "C:/Images/Apple.jpg";

For more information please visit Use Image from Local Disk

For information regarding using images from memory, kindly visit Use Image from Memory section.

Thanks,

I’ve been using the “Image from Memory” for a while, because it’s been my only option. I don’t have a physical location on the disk to store the images, so their embedded in the actual DLL that I use Aspose.Pdf.

While looking through release notes though I saw:
3514
- Embedded ImageData as Base64 string is supported in XML

But I can’t find an example of this anywhere. Is this using the MemoryData you were referring to?

I don’t like using the in memory option because I need to keep my memory stream open until after the pdf is saved.

-Jeff

Dear Jeff,

Your query regarding an example on using " Embedded ImageData as Base64 string, supported in XML" uhas been forwarded to our development team. you will soon be updated with the information. Additional information on using Image from Memory will also be provided.

Thanks for considering Aspose.

Hi,

The Base64 string is supported in XML only. It is not needed in API since you can use stream object. An example of Base64 string is attached.

As for the Image from Memory issue, can you please provide example project that can reproduce this error?

Thanks for the reply, I’m not sure exactly what I did, but I got the MemoryStream version working again, and I can’t seem to reproduce the error I was getting. It was most likely my mistake.

As far as the Base64 Image. Could I use Pdf.CreateObjFromXml against an xml segment like that to produce an Image object then? If so, that would work just as well as what I’m doing.

Hello Jeff,<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />

Thanks for your patience. Regarding your query "Could I use Pdf.CreateObjFromXml against an xml segment like that to produce an Image object then?"

The answer is, yes. Pdf.CreateObjFromXml works for Base64 images.

For more understanding kindly take a look over following code example. XML file is also in attachment.


Pdf p = new Pdf();

Section sec1 = p.Sections.Add();

Aspose.Pdf.Image img =
Pdf.CreateObjFromXml("d:/temp/ImageData.xml",null) as Aspose.Pdf.Image;

sec1.Paragraphs.Add(img);

p.Save("d:/test/test.pdf");