1
votes

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.

1
Time to update the thread title: PDFsharp (s is lower case) draws images with the correct size, but not with the size you expect. Set your image to 72 DPI and the image will fill the rectangle.I liked the old Stack Overflow
Your "solution" does not make much sense. PDFsharp uses points as the default unit and one point is 1/72 inch. If you draw images without specifying a size, they will be drawn with the size specified in the image file (which is 96 DPI for many images created under Windows). A "point" is not a pixel, it is 1/72 inch. I cannot recommend resizing all images just to be able to use DrawImage without specifying a size.I liked the old Stack Overflow
Nope... My images are larger... I resized it to 100x100 now I'm resizing it to 133×133 (if it is 96Dpi) to fit the 100x100 points square. Drawing it with or without specifying size is irrelevant.Romias

1 Answers

0
votes

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.