How to embed System.Drawing.Bitmap while preserving the full image quality?


I add a System.Drawing.Bitmap to the PDF as follows:

public void AddPicture_(double Left, double Bottom, Bitmap bmp, float
Divider) {
        //Obj _JBIG2_hint = Obj.CreateArray();

      pdftron.PDF.Image img = pdftron.PDF.Image.Create(Doc, bmp);//,

     Element element = Builder.CreateImage(img, new
Matrix2D(img.GetImageWidth(), 0, 0, img.GetImageHeight(), Left,

I don't use any pictures saved to disk; this is just debugging info.
The image called test_pdftron_1 and test_pdftron3 are OK, but
test_pdftron_2 looks like a low quality jpeg.

The problem is that at the point when you are embedding
System.Drawing.Bitmap in PDF, PDFNet assumes default compression to be
JPEG (in lossy mode).

To go around this you can use encoder 'hints' to choose different
compression scheme. In you code you use JBIG2 hint, however this
applies only is the image data you are embedding is 1BPP monochrome.
Instead you can choose 'Flate' compression which corresponds to
lossless compression used in PNG format. For example:

Obj encoder_hint = Obj.CreateName("Flate");
pdftron.PDF.Image img = pdftron.PDF.Image.Create(Doc, bmp,

Alternatively you can stick with JPEG compression but increase the
Quality parameter. For example:

Obj hint2 = Obj.CreateArray();
pdftron.PDF.Image img = pdftron.PDF.Image.Create(Doc, bmp, hint2);

There is an error in the above definition of 'Flate' encoder hint. The
proper format is:

Obj encoder_hint = Obj.CreateArray("Flate");

pdftron.PDF.Image img = pdftron.PDF.Image.Create(doc, bmp,