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.Height <= PicBox.Image.Width)
printImage.DefaultPageSettings.Landscape = true;
else
printImage.DefaultPageSettings.Landscape = false;
printImage.Print();
}
private void printImage_PrintPage(object sender,
System.Drawing.Printing.PrintPageEventArgs 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 widthConstraintRatio = 0;
float heightConstraintRatio = 0;
float commonConstraintRatio = 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.Width - (hardMarginX * 2),
e.PageBounds.Height - (hardMarginY * 2));
if (printRectangle.Width < PicBox.Width)
widthConstraintRatio =
(float)printRectangle.Width / (float)PicBox.Width;
if (printRectangle.Height < PicBox.Height)
heightConstraintRatio =
(float)printRectangle.Height / (float)PicBox.Height;
if (widthConstraintRatio 0 && heightConstraintRatio 0)
{
if (widthConstraintRatio >= heightConstraintRatio)
commonConstraintRatio = heightConstraintRatio;
else
commonConstraintRatio = widthConstraintRatio;
}
else if (widthConstraintRatio 0 &&
heightConstraintRatio == 0)
commonConstraintRatio = widthConstraintRatio;
else if (widthConstraintRatio == 0 &&
heightConstraintRatio 0)
commonConstraintRatio = heightConstraintRatio;
if (commonConstraintRatio 0)
{
printRectangle.Height =
PicBox.Height * commonConstraintRatio;
printRectangle.Width =
PicBox.Width * commonConstraintRatio;
}
e.Graphics.DrawImage(PicBox.Image, printRectangle);
e.HasMorePages = false;
}