views:

416

answers:

1

As part of a print procedure of my application I'm trying to print a list of images scaled down to a specified width and placed one below the other. The problem is I can not figure out how to transform the height in pixels of the images to the height in the units used by the graphics object during printing. How do I calculate the imageHeightPrint variable correctly?

This code snippet is the part of the image printing loop that scales down the image and calculates it's height and the placement of the next image.

Image image = Image.FromStream(imageStream);

// Get proportional correct height
int imageHeight = image.Height * imageWidth / image.Width;

Image imageToPrint = image.GetThumbnailImage(imageWidth, imageHeight, null, IntPtr.Zero);

float imageHeightPrint = e.Graphics.DpiY * imageToPrint.Height / imageToPrint.VerticalResolution;

e.Graphics.DrawImage(imageToPrint, e.MarginBounds.Left, yPos);

yPos += imageHeightPrint;
+1  A: 

I found the correct solution my self after dissecting the documentation.

This line:

float imageHeightPrint = e.Graphics.DpiY * imageToPrint.Height / imageToPrint.VerticalResolution;

Should be changed into this:

float imageHeightPrint = imageToPrint.Height / 
                         imageToPrint.VerticalResolution * 100;

The biggest thing I missed was that the height-in-print should be in hundredths of an inch.

Joakim Karlsson