打印指定的区域
Code
public void PrintToGraphics(Graphics graphics, Rectangle bounds)
{
Bitmap bitmap = new Bitmap(this.Width, this.Height);
this.DrawToBitmap(bitmap, new Rectangle(0, 0, bitmap.Width, bitmap.Height));
Rectangle target = new Rectangle(0, 0, bounds.Width, bounds.Height);
double xScale = (double)bitmap.Width / bounds.Width;
double yScale = (double)bitmap.Height / bounds.Height;
if (xScale < yScale)
target.Width = (int)(xScale * target.Width / yScale);
else
target.Height = (int)(yScale * target.Height / xScale);
graphics.PageUnit = GraphicsUnit.Display;
graphics.DrawImage(bitmap, target);
}
private void printDocument1_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
{
PrintToGraphics(e.Graphics, e.MarginBounds);
}
private void button2_Click(object sender, EventArgs e)
{
this.printDocument1.Print();
}
public void PrintToGraphics(Graphics graphics, Rectangle bounds)
{
Bitmap bitmap = new Bitmap(this.Width, this.Height);
this.DrawToBitmap(bitmap, new Rectangle(0, 0, bitmap.Width, bitmap.Height));
Rectangle target = new Rectangle(0, 0, bounds.Width, bounds.Height);
double xScale = (double)bitmap.Width / bounds.Width;
double yScale = (double)bitmap.Height / bounds.Height;
if (xScale < yScale)
target.Width = (int)(xScale * target.Width / yScale);
else
target.Height = (int)(yScale * target.Height / xScale);
graphics.PageUnit = GraphicsUnit.Display;
graphics.DrawImage(bitmap, target);
}
private void printDocument1_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
{
PrintToGraphics(e.Graphics, e.MarginBounds);
}
private void button2_Click(object sender, EventArgs e)
{
this.printDocument1.Print();
}