taji01
taji01

Reputation: 2615

Crop and Print Image Documents without Distortion In C#

I'm using WinForms. In my form I have a picturebox I use to display image documents. The problem is when I crop the image and then print the document out the image becomes slightly distorted. If I don't crop the image document and print it regularly the image document does not become distorted.

How do I crop and print the image documents without them being distorted?

Or is there a better way to code this so it can crop and print without the image document being distorted? If so, how can i do it?

Notes:

enter image description here

Test: Slightly Distorted:

enter image description here

Test: Not Distorted:

enter image description here

enter image description here

Test: The picture above is a test. This is what happened when i took the below code out from pictureBox1_MouseUp:

Bitmap originalImage = new Bitmap(pictureBox1.Image, pictureBox1.Width, pictureBox1.Height);

And edited/replaced (originalImage to pictureBox1.Image):

g.DrawImage(pictureBox1.Image, 0, 0, rect, GraphicsUnit.Pixel);

Upvotes: 14

Views: 1348

Answers (1)

Hans Passant
Hans Passant

Reputation: 942177

Bitmap originalImage = new Bitmap(pictureBox1.Image, pictureBox1.Width, pictureBox1.Height);

That is most likely where the problem started. This can cause pictureBox1.Image to be rescaled to force-fit it to the pictureBox1 size. Depends on whether the picturebox has borders and its SizeMode property value. Rescaling causes the image to be resampled, the color of a pixel in the new bitmap is calculated from the values of its neighboring pixels in the original image as directed by the selected InterpolationMode.

This in effect blurs the resulting image. That works well on a photo but this is text that critically depends on anti-aliasing pixels to look decent on a low-resolution monitor. Slight changes to those pixels ruins the effect and they no longer smoothly blend the letter shape against the background anymore. They become more visible, best way to describe it is that the resulting text looks "fat".

I see no obvious reason to do this at all in the posted code. Delete the statement and replace originalImage with pictureBox1.Image.

Also beware that printing this image is likely to be disappointing. Now those anti-aliasing pixels get turned into 6x6 blobs of ink on paper. That only ever looks good when you have long arms. As long as the font size is this small and you have no control over the anti-aliasing choice then there's very little you can do about that. Printed text only ever looks good when you use PrintDocument and Graphics.DrawString().

Upvotes: 1

Related Questions