Background
Image class in Aspose.Pdf offers the capability to convert an image from various sources into PDF format. This may include an image from particular location over the hard-drive, an image from MemoryStream or image placed over some web location. ImageInfo class has a property named ImageStream which is used specify MemoryStream as image source .
C#
//Instantiate Pdf instance by calling its empty constructor Aspose.Pdf.Generator.Pdf pdf1 = new Aspose.Pdf.Generator.Pdf(); //Add a section into the pdf document Aspose.Pdf.Generator.Section sec = pdf1.Sections.Add(); // Create a FileStream object to read the imag file FileStream fs = File.OpenRead(@"d:\pdftest\Aspose.jpg"); // Read the image into Byte array byte[] data = new byte[fs.Length]; fs.Read(data, 0, data.Length); // Create a MemoryStream object from image Byte array MemoryStream ms = new MemoryStream(data); //Create an image object in the section Aspose.Pdf.Generator.Image imageht = new Aspose.Pdf.Generator.Image(sec); //Set the type of image using ImageFileType enumeration imageht.ImageInfo.ImageFileType = Aspose.Pdf.Generator.ImageFileType.Jpeg; // Specify the image source as MemoryStream imageht.ImageInfo.ImageStream = ms; //Add image object into the Paragraphs collection of the section sec.Paragraphs.Add(imageht); //Save the Pdf pdf1.Save("d:/pdftest/Image_From_MemoryStream.pdf"); // Close the MemoryStream Object ms.Close();
VB.NET
'Instantiate Pdf instance by calling its empty constructor Dim pdf1 As Aspose.Pdf.Generator.Pdf = New Aspose.Pdf.Generator.Pdf() 'Add a section into the pdf document Dim sec As Aspose.Pdf.Generator.Section = pdf1.Sections.Add() ' Create a FileStream object to read the imag file Dim fs As FileStream = File.OpenRead("d:\pdftest\Aspose.jpg") ' Read the image into Byte array Dim data(fs.Length - 1) As Byte fs.Read(data, 0, data.Length) ' Create a MemoryStream object from image Byte array Dim ms As MemoryStream = New MemoryStream(data) 'Create an image object in the section Dim imageht As Aspose.Pdf.Generator.Image = New Aspose.Pdf.Generator.Image(sec) 'Set the type of image using ImageFileType enumeration imageht.ImageInfo.ImageFileType = Aspose.Pdf.Generator.ImageFileType.Jpeg ' Specify the image source as MemoryStream imageht.ImageInfo.ImageStream = ms 'Add image object into the Paragraphs collection of the section sec.Paragraphs.Add(imageht) 'Save the Pdf pdf1.Save("d:/pdftest/Image_From_MemoryStream.pdf") ' Close the MemoryStream Object ms.Close()
