在窗体中添加一个按钮,PrintDialog控件,printDocument控件,为printDocument控件添加事件
private void printDocument1_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
{
//打印内容 为 局部的 this.panel1
Bitmap _NewBitmap = new Bitmap(panel1.Width, panel1.Height);
panel1.DrawToBitmap(_NewBitmap, new Rectangle(0, 0, _NewBitmap.Width, _NewBitmap.Height));
e.Graphics.DrawImage(_NewBitmap, 0, 0, _NewBitmap.Width, _NewBitmap.Height);
}
//给按钮添加如下事件
private void DaYinBtn_Click(object sender, EventArgs e)
{
DaYinBtn.Visible = false;//在打印时隐藏按钮
this.printDialog1.Document = this.printDocument1;
if (this.printDialog1.ShowDialog() == DialogResult.OK)
{
this.printDocument1.Print();
}
DaYinBtn.Visible = true;//打印完成回复按钮显示
}