1.新建窗口Form1,在上面拖1个Panel,1个Button,1个printDocument,1个printDocument1
2.在Form1.cs中写入代码:
private void printDocument1_PrintPage_1(object sender, PrintPageEventArgs e) { //打印内容 为 局部的 this.panel1 Bitmap _NewBitmap = new Bitmap(panel.Width, panel.Height); panel.DrawToBitmap(_NewBitmap, new Rectangle(0, 0, _NewBitmap.Width, _NewBitmap.Height)); e.Graphics.DrawImage(_NewBitmap, 0, 0, _NewBitmap.Width, _NewBitmap.Height); } private void button1_Click(object sender, EventArgs e) { this.printDialog1.Document = this.printDocument1; if (this.printDialog1.ShowDialog() == DialogResult.OK) { this.printDocument1.Print(); } }
3.实现点击Button打印Panel内容