I am trying to print huge images (much bigger than target paper). I try
and use e.PageSettings. HardMarginX and e.PageSettings. HardMarginY in the
PrintDocument's PrintPage event to try and determine the maximum area
that I can print on. However, the edge of the image invariably gets cut
off, as if the HardMargin info is wrong. I posted the code below as I
can't understand what I am doing wrong. Is information in
e.PageSettings reliable?
public void Print()
{
if (PicBox.Image.H eight <= PicBox.Image.Wi dth)
printImage.Defa ultPageSettings .Landscape = true;
else
printImage.Defa ultPageSettings .Landscape = false;
printImage.Prin t();
}
private void printImage_Prin tPage(object sender,
System.Drawing. Printing.PrintP ageEventArgs e)
{
// depending on the size of the image,
// we might have to constrain
// the width and height or neither
// if we constrain width, we must proportionately
// constrain the height as well and vice versa
//Rectangle printRectangle = e.PageBounds;
float widthConstraint Ratio = 0;
float heightConstrain tRatio = 0;
float commonConstrain tRatio = 0;
// increase the hard margin just for safety.
float hardMarginX = e.PageSettings. HardMarginX;
float hardMarginY = e.PageSettings. HardMarginY;
RectangleF printRectangle = new RectangleF(
hardMarginX,
hardMarginY,
e.PageBounds.Wi dth - (hardMarginX * 2),
e.PageBounds.He ight - (hardMarginY * 2));
if (printRectangle .Width < PicBox.Width)
widthConstraint Ratio =
(float)printRec tangle.Width / (float)PicBox.W idth;
if (printRectangle .Height < PicBox.Height)
heightConstrain tRatio =
(float)printRec tangle.Height / (float)PicBox.H eight;
if (widthConstrain tRatio 0 && heightConstrain tRatio 0)
{
if (widthConstrain tRatio >= heightConstrain tRatio)
commonConstrain tRatio = heightConstrain tRatio;
else
commonConstrain tRatio = widthConstraint Ratio;
}
else if (widthConstrain tRatio 0 &&
heightConstrain tRatio == 0)
commonConstrain tRatio = widthConstraint Ratio;
else if (widthConstrain tRatio == 0 &&
heightConstrain tRatio 0)
commonConstrain tRatio = heightConstrain tRatio;
if (commonConstrai ntRatio 0)
{
printRectangle. Height =
PicBox.Height * commonConstrain tRatio;
printRectangle. Width =
PicBox.Width * commonConstrain tRatio;
}
e.Graphics.Draw Image(PicBox.Im age, printRectangle) ;
e.HasMorePages = false;
}