C#中如何打印出整个PANEL的内容?
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了C#中如何打印出整个PANEL的内容?相关的知识,希望对你有一定的参考价值。
需要将整个PANEL中的内容打印出来,使用了如下代码,但打印预览里还是一片空白?何解?
1、private Bitmap memImage = null;//声明全局变量 下面代码会用到的
2、 private void button2_Click(object sender, EventArgs e)//按键执行的代码
Graphics graphic = panelReports.CreateGraphics();
Size s = panelReports.Size;
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.
Image 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();
3、[System.Runtime.InteropServices.DllImportAttribute("gdi32.dll")]//应用API函数
private static extern bool BitBlt(
IntPtr hdcDest, // 目标设备的句柄
int nXDest, // 目标对象的左上角的X坐标
int nYDest, // 目标对象的左上角的X坐标
int nWidth, // 目标对象的矩形的宽度
int nHeight, // 目标对象的矩形的长度
IntPtr hdcSrc, // 源设备的句柄
int nXSrc, // 源对象的左上角的X坐标
int nYSrc, // 源对象的左上角的X坐标
System.Int32 dwRop // 光栅的操作值
);
4、 private void printDocument1_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)//打印文件的事件设置
e.Graphics.DrawImage(memImage,0,0);
##注意##
调试时候,增加printDocument1,panelReports为panel的name
回一楼...白搭...
Graphics g = Graphics.FromImage(memImage );
g.CopyFromScreen(panelReports.Location.X, panelReports.Location.Y, 0, 0, panelReports.Size);
这样子看看
以下这个是我自己一个打印预览的可以显示,你看看是否有帮助
private Image myFormImage;//定义打印图像
///预览打印
private void button3_Click(object sender, EventArgs e)
try
button1.Visible = false; //打印时不打印 打印按钮
button2.Visible = false;//打印时不打印 返回按钮
button3.Visible = false;//打印时不打印 打印预览按钮
myFormImage = new Bitmap(localpages.Width - 5, localpages.Height);
Graphics g = Graphics.FromImage(myFormImage);
g.CopyFromScreen(this.Location.X + 5, this.Location.Y + 30, 0, 0, localpages.Size);
this.printPreviewDialog1.Document = this.printDocument1;
this.printPreviewDialog1.ShowDialog();
button1.Visible = true;//打印完后显示 打印按钮
button2.Visible = true;//打印完后显示 返回按钮
button3.Visible = true;//打印完后显示 打印预览按钮
catch (Exception)
private void printDocument1_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)//执行打印
try
e.Graphics.DrawImage(this.myFormImage, 0, 0);
catch (Exception)
本回答被提问者采纳 参考技术B 请问你是怎么解决的???
以上是关于C#中如何打印出整个PANEL的内容?的主要内容,如果未能解决你的问题,请参考以下文章