Search code examples
pdfsharp

PDFsharp drawing images at 72 DPI, if more resolution it is drawn "smaller"


I'm drawing an image of 100 x 100 pixels in a PDFSharp document.

The image is drawn using:

 g.DrawImage(image, new Point(x, y));

The issue here is that if I draw a rectangle starting in the same coordinates x,y and using 100x100 as dimensiones... the rectangle is larger than the image.

If I use the other overloads in DrawImage, setting the container rectangle, the image fits the rectangle but it loses quality (it is enlarged).

The logo in the PDF looks like this

I think it is a problem of different resolutions or something like that.

Any ideas?

UPDATE: I resized the image to 133x133 to fit the 100x100 rectangle. What is the reason of this difference? A 33% difference.

MY SOLUTION: When retrieving the image and scaling it to fit the rectangle, you need to take into account that the size of your image is in PIXELS and when you draw in the PDF it is in points. If your image is in 96 DPI, you need to increase its dimension multiplying by "96/72" (that is the 33% I got), and that way you will see what you expect (even if you draw it using a container rectangle or just the starting coordinates).

Set image.Interpolate = false; to disable anti-aliasing and increase the sharpness of the small image.


Solution

  • There are no "pixels" in PDF. DrawImage has overloads that allow to draw the image with a specific size.

    If the size is omitted (as you do), the size will be determined by the DPI setting of the image. Could it be your image is set to 96 DPI?

    The rectangle 100x100 uses points - and there are 72 points per inch.

    The image does not lose quality when you set the size. The "quality loss" depends on the zoom level of the viewer.

    You can set a hint to prevent Adobe Reader from anti-aliasing the image.

    Update:
    Set image.Interpolate = false; to disable anti-aliasing.