We're sorry Aspose doesn't work properply without JavaScript enabled.

Free Support Forum - aspose.com

Saving bitmap to a pdf file leads to a black image


I'm currently evaluating the use of Aspose.Pdf for converting a Bitmap, which is generated in my code, into a pdf file.
But it doesn't work, the output pdf file is always completely black.

This is the code I use:

private void SaveBitmapAsPdf(Bitmap bmp)
// Save Bitmap to file -> for comparison with the resulting pdf file

using (MemoryStream ms = new MemoryStream())
// Save Bitmap to MemoryStream
bmp.Save(ms, System.Drawing.Imaging.ImageFormat.Bmp);

//Instantiate a Pdf object
Aspose.Pdf.Generator.Pdf pdf1 = new Aspose.Pdf.Generator.Pdf();

// Reset margins
pdf1.PageSetup.Margin.Bottom = 0;
pdf1.PageSetup.Margin.Left = 0;
pdf1.PageSetup.Margin.Top = 0;
pdf1.PageSetup.Margin.Right = 0;

//Create a new section in the Pdf document
Aspose.Pdf.Generator.Section sec1 = new Aspose.Pdf.Generator.Section(pdf1);

//Add the section in the sections collection of the Pdf document

//Create an image object
Aspose.Pdf.Generator.Image image1 = new Aspose.Pdf.Generator.Image(sec1);

//Add the image into paragraphs collection of the section

image1.ImageInfo.ImageFileType = Aspose.Pdf.Generator.ImageFileType.Bmp;

//Set the ImageStream to the MemoryStream object
image1.ImageInfo.ImageStream = ms;

//Save the Pdf

The code has two resulting files (see attached zip file):
TestImage.bmp and TestPdf.pdf

The goal is that both files look completely the same, but as you can see the pdf file is black.
Can you show me, what I'm doing wrong?


Hi Felix,

Thanks for your inquiry. Please use New DOM approach to convert an image to PDF.

Please find a sample code snippet and output is enclosed for reference. Hopefully it will help you to accomplish the task.

string outFile =
myDir + ImagetoPDF.pdf”;

string inFile = myDir + ("TestImage.bmp");

Document doc = new Document();

Page page = doc.Pages.Add();

FileStream fs = new FileStream(inFile, FileMode.Open, FileAccess.Read);

// Set margins so image will fit, etc.

page.PageInfo.Margin.Bottom = 0;

page.PageInfo.Margin.Top = 0;

page.PageInfo.Margin.Left = 0;

page.PageInfo.Margin.Right = 0;

page.PageInfo.Height = Aspose.Pdf.PageSize.A4.Height;

page.PageInfo.Width = Aspose.Pdf.PageSize.A4.Width;

//Create an image object

Aspose.Pdf.Image image1 = new Aspose.Pdf.Image();

//Add the image into paragraphs collection of the section


//Set the ImageStream to a MemoryStream object

image1.ImageStream = fs;

//Set desired image scale

//doc.Convert(new MemoryStream(), PdfFormat.PDF_A_1B, ConvertErrorAction.Delete);


Please feel free to contact us for any further assistance.

Best Regards,


thank you for your response.
It works perfect if I use a FileStream of my saved Bitmap file.
But it still doesn’t work if I save the bitmap into a MemoryStream and take this stream as ImageStream (the image is still black). With the solution of taking the FileStream I always have to save the Bitmap into a file before converting it to pdf.
Do you have any idea how to solve my problem without saving the bitmap into a file before conversion?


Hi Felix,

Thanks for sharing your findings.We have managed to reproduce the reported issue and logged it in our bug tracking system as PDFNEWNET-36933 for further investigation and resolution. We will notify you via this thread as soon as it is resolved.

Please feel free to contact us for any further assistance.

Best Regards,

Hi Felix,

Thanks for your patience.

We have further investigated the issue reported earlier as PDFNEWNET-36933 and it does not seem to be a bug. The conversion to BMP does not work but as the input file has PNG format, so it can be imported in this format. Please take a look over following code snippet.


System.Drawing.Image img = System.Drawing.Image.FromFile(inFile);<o:p></o:p>

MemoryStream imgms = new MemoryStream();

img.Save(imgms, ImageFormat.Bmp);

img = new System.Drawing.Bitmap(imgms);


In case you still face the same problem or you have any further query, please feel free to contact.


but I don’t have an input file here, I only have a Bitmap-Object which I myself create before executing the code (which I posted above).
So how can this Bitmap-Object have PNG format?


Hi again,

you were right, I’ve just done it!
I simply had to call bmp.Save(…) with PNG as image format (“bmp.Save(ms, ImageFormat.Png);” instead of “bmp.Save(ms, ImageFormat.Bmp);”), then it worked.

Thanks a lot, the problem is solved!

Hi Felix,

Thanks for your inquiry. It is good to know that you have manged to resolve the issue.

Please keep using our APIs and feel free to ask any question or concern. We will be more than happy to extend our support.

Best Regards,