c# 如何打印panel里的内容?

如题
能把打印原理讲一遍吗?
当前上下文不存在名称 BitBlt print_image printDocument1

private void btnPrint_Click(object sender, System.EventArgs e)
{
Graphics graphic = panelReports.CreateGraphics();
Size s = panelReports.Size;
Bitmap memImage = new Bitmap(s.Width, s.Height, graphic);
Graphics memGraphic = Graphics.FromImage(memImage);
IntPtr dc1 = graphic.GetHdc();
IntPtr dc2 = memGraphic.GetHdc();
BitBlt(dc2, 0, 0, panelReports.ClientRectangle.Width, panelReports.ClientRectangle.Height,
dc1, 0, 0, 13369376);

//Clone the bitmap so we can dispose it.
print_image = (Image)memImage.Clone();
graphic.ReleaseHdc(dc1);
memGraphic.ReleaseHdc(dc2);
graphic.Dispose();
memGraphic.Dispose();
memImage.Dispose();

PrintPreviewDialog dlg = new PrintPreviewDialog() ;
dlg.Width = 800;
dlg.Height = 600;
dlg.Document = printDocument1;
if (dlg.ShowDialog() == DialogResult.OK)
printDocument1.Print();
}

panelReports是panel的name
温馨提示:答案为网友推荐,仅供参考
第1个回答  2020-04-02
//panel1中包含的图片都必须存在,如果是动态添加的就要保存成数据流来显示,显示后不要释放。或定义PictureBox
Image的显示文件路径
//以panel1为新图象大小设定图片的宽高和其它参数,System.Drawing.Imaging.PixelFormat中还有像素,Alpha等,根据需要自己定义
Bitmap
bmp
=
new
Bitmap(panel1.Width,
panel1.Height,
System.Drawing.Imaging.PixelFormat.Format24bppRgb);
//新建绘制到bmp
Graphics
g
=
Graphics.FromImage(bmp);
g.Clear(Color.White);//上底色
foreach
(PictureBox
pb
in
panel1.Controls)
{
//绘制包含的图象
g
=
Graphics.FromImage(pb.Image);
g.DrawImage(bmp,
pb.Location.X,
pb.Location.Y);
}
//panel1外的pictureBox输出,要保存写成bmp.Save(路径,格式);
this.pictureBox3.Image
=
(Image)bmp;
g.Dispose();
第2个回答  2012-02-27
我也遇到了同样的问题。。