GDI+ image to PDF conversion not working correctly

TOOL PROBLEMS:

When loading creating a PDF from a GDI+ image, the conversion is not working correctly. The picture only has some of the text drawn, but most is solid black in color. See the code below:

System.IO.MemoryStream mstream = new System.IO.MemoryStream();
System.Drawing.Bitmap myBMP = new Bitmap(500, 1000);
System.Drawing.Image image = myBMP;
Graphics gr = Graphics.FromImage(image);
// use gr to draw on surface
myBMP.Save(mstream,System.Drawing.Imaging.ImageFormat.Bmp);
Pdf pdf1 = new Pdf();
Section sec1 = new Section(pdf1);

pdf1.Sections.Add(sec1);

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

sec1.Paragraphs.Add(image1);
image1.ImageInfo.ImageFileType = ImageFileType.MemoryBmp;
image1.ImageInfo.OpenType = ImageOpenType.Memory;
image1.ImageScale = 1F; [//0.5F](https://0.5f/);

System.IO.BinaryReader reader = new System.IO.BinaryReader(mstream);
mstream.Position = 0;

image1.ImageInfo.MemoryData = reader.ReadBytes((int)mstream.Length);

myBMP.Dispose();
byte[] buf = new byte[1];

MemoryStream st = new MemoryStream();
pdf1.Save(st);
buf = st.GetBuffer();

SendPDFToBrowser(buf);

WEBSITE PROBLEMS:

PROBLEM #1

The paging mechanism on the Aspose.Pdf Forums is broken. When you click on a page number, it stays on the first page.

PROBLEM #2

This is the message I get whenever I try to search for specific text in the Aspose.Pdf forum.

“We are unable to serve your request”. This problem only happens on the Aspose.Pdf forum.

Thanks for considering Aspose.

As you can see from the API document, the ImageInfo.MemoryData is absolete. Please use following code line instead.

image1.ImageInfo.ImageStream = mstream ;

If it still won't work, please let me know.

Thanks for reporting the two website problems, we will solve it right now.

Ok. I switched the code to use ImageStream instead. I have the same problem however. See code below:

int width = 765;

int height = 1050;

System.IO.MemoryStream mstream = new System.IO.MemoryStream();

System.Drawing.Bitmap myBMP = new Bitmap(width, height);

System.Drawing.Image image = myBMP;

Graphics gr = Graphics.FromImage(image);

// Draw image using gr

myBMP.Save(mstream,System.Drawing.Imaging.ImageFormat.Bmp);

Pdf pdf1 = new Pdf();

Section sec1 = new Section(pdf1);

pdf1.Sections.Add(sec1);

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

sec1.Paragraphs.Add(image1);

image1.ImageInfo.ImageFileType = ImageFileType.Bmp;

System.IO.BinaryReader reader = new System.IO.BinaryReader(mstream);

mstream.Position = 0;

image1.ImageInfo.ImageStream = mstream;

byte[] buf = new byte[1];

MemoryStream st = new MemoryStream();

pdf1.Save(st);

buf = st.GetBuffer();

SendPDFToBrowser(buf);

I have not found any error. If you save the bmp to disk you can find it is a solid dark image. Please elaborate the problem if it still won’t work.

The directions above are directly from your website. However, they still direct me to use “obsolete” calls. Also, the directions on this page to not match the C# example or the VB.NET example.

Where can I get updated documentation?

We are simply doing some GDI+ drawing and want to create in memory PDF’s that we can stream to the browser. We can do this with Adobe PDF Writer, but not your product. If we can get your product to work, we will purchase it. Below is code that is currently failing on

pdf1.Save(st); System.ApplicationException: Can’t open image file

----------------- CODE ---------------------------

System.Drawing.Bitmap myBMP = new Bitmap(200, 200);
System.Drawing.Image image = myBMP;
Graphics gr = Graphics.FromImage(image);
// Draw image using gr
System.Drawing.Rectangle _myrectangle = new System.Drawing.Rectangle(20,20,50,50);
Pen mypen = new Pen(System.Drawing.Color.Red,3);
gr.DrawRectangle( mypen, _myrectangle);
System.IO.MemoryStream mstream = new System.IO.MemoryStream();
myBMP.Save(mstream,System.Drawing.Imaging.ImageFormat.Bmp);
Pdf pdf1 = new Pdf();
Section sec1 = new Section(pdf1);
pdf1.Sections.Add(sec1);
Aspose.Pdf.Image image1 = new Aspose.Pdf.Image(sec1);
sec1.Paragraphs.Add(image1);
image1.ImageInfo.ImageFileType = ImageFileType.MemoryBmp;
image1.ImageInfo.OpenType = ImageOpenType.Memory;
image1.ImageScale = 1F;
image1.ImageInfo.ImageStream = mstream;
byte[] buf = new byte[1];
MemoryStream st = new MemoryStream();
pdf1.Save(st);
buf = st.GetBuffer();
SendPDFToBrowser(buf);

Thanks for considering Aspose.

We will investigate this issue then give you a reply ASAP.

Sorry for the confusing. Since we have supported memory image, the MemoryBmp type is not needed now. We will make it clear in the document. If you change the code as the following it works:

image1.ImageInfo.ImageFileType = ImageFileType.Bmp;

// image1.ImageInfo.OpenType = ImageOpenType.Memory;

Yes, that helped. However I’m still having issues:
my GDI+ image is in pixels and the ASPOSE image is in points. The translation causes text and images to be very blurry. I looked at this thread…
and realized I was having the same issue. Here are the details:
with an image height of 765 and width of 1050, my image fit perfectly on a printed page (using Adobe PDF Print driver). These same settings (even after setting all margins to zero) create a page that is larger than the ASPOSE image page.
Also, my text and images are clearly being translated to a larger size. I tried the idea from the previous article of setting the scale differently:

image1.ImageScale = .75F    // 75%

but its still doing something funky to the image and text.

My GDI+ images are displayed on screen, preview, or printed (using Adobe PDF writer) and the images are crystal clear. The translation that ASPOSE is doing is messing up my image. How can I stop this from happening, or… how can I get ASPOSE to do the correct translation?

Thanks for your help, see below for my code:

int width = 600; // 735 - 765
int height = 800; // 1000 - 1050
System.Drawing.Bitmap myBMP = new Bitmap(width, height);
System.Drawing.Image image = myBMP;
Graphics gr = Graphics.FromImage(image);
// Draw image using gr
System.Drawing.Rectangle _myrectangle = new System.Drawing.Rectangle(20,20,500,100);
Pen mypen = new Pen(System.Drawing.Color.Red,3);
gr.DrawRectangle( mypen, _myrectangle);
gr.FillRectangle(System.Drawing.Brushes.White,_myrectangle);
gr.DrawString("determining the purposes of listening such as for gaining information", new Font("Arial", 7), System.Drawing.Brushes.Black,_myrectangle);<BR>_myrectangle.Y += 10;
gr.DrawString("determining the purposes of listening such as for gaining information", new Font("Arial", 7), System.Drawing.Brushes.Black,_myrectangle);
_myrectangle.Y += 10;
gr.DrawString("determining the purposes of listening such as for gaining information", new Font("Arial", 8), System.Drawing.Brushes.Black,_myrectangle);
_myrectangle.Y += 10;
gr.DrawString("determining the purposes of listening such as for gaining information", new Font("Arial", 9), System.Drawing.Brushes.Black,_myrectangle);
System.IO.MemoryStream mstream = new System.IO.MemoryStream();
myBMP.Save(mstream,System.Drawing.Imaging.ImageFormat.Bmp);
Pdf pdf1 = new Pdf();
Section sec1 = new Section(pdf1);
sec1.PageInfo.Margin.Top = 2;
sec1.PageInfo.Margin.Bottom = 2;
sec1.PageInfo.Margin.Left = 2;
sec1.PageInfo.Margin.Right = 2;
pdf1.Sections.Add(sec1);
Aspose.Pdf.Image image1 = new Aspose.Pdf.Image(sec1);
sec1.Paragraphs.Add(image1);
image1.ImageInfo.ImageFileType = ImageFileType.Bmp;
//image1.ImageInfo.OpenType = ImageOpenType.Memory;
image1.ImageScale = 1F;
image1.ImageInfo.ImageStream = mstream;
byte[] buf = new byte[1];
MemoryStream st = new MemoryStream();
pdf1.Save(st);
buf = st.GetBuffer();
SendPDFToBrowser(buf);

You should use the original size (765*1075) when creating the GDI+ image and set ImageScale to 0.75 when adding it to PDF.