//用到的三个控件分别是 printDocument(区域) 、printPreviewDialog(预览)、(pageSetupDialog设置)
protected void Init()
{
this.printDocument1.OriginAtMargins = true; //启用页边距
this.printPreviewDialog1.Document = this.printDocument1;
this.pageSetupDialog1.Document = this.printDocument1;
this.labPrintTime.Text = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
}
/// <summary>
/// 设置打印区域 核心
/// </summary>
private void printDocument1_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
{
//打印区域为GroupBox中的内容
int width = this.groupBox1.Width;
int height = this.groupBox1.Height;
Bitmap bitmap = new Bitmap(width, height);
this.groupBox1.DrawToBitmap(bitmap, new Rectangle(0, 0, width, height));
e.Graphics.DrawImage(bitmap, 0, 0, width, height);
}
/// <summary>
/// 打印预览
/// </summary>
private void btnPrintView_Click(object sender, EventArgs e)
{
DialogResult dr = this.printPreviewDialog1.ShowDialog();
if (dr == System.Windows.Forms.DialogResult.OK)
{
this.printDocument1.Print();
}
}
/// <summary>
/// 打印设置
/// </summary>
private void btnPrintSetting_Click(object sender, EventArgs e)
{
this.pageSetupDialog1.ShowDialog();
}
C#打印GroupBox控件区域
本文转载:CSDN博客