【发布时间】:2015-05-16 16:37:14
【问题描述】:
我试图获得一个按钮来打印我当前的表单,并尝试了我可以在此处找到的所有代码,但它一直在打印空白页,我不知道为什么。
我使用的代码如下
Bitmap bitmap;
private void btnPrint_Click(object sender, EventArgs e)
{
//Add a Panel control.
Panel panel = new Panel();
this.Controls.Add(panel);
//Create a Bitmap of size same as that of the Form.
Graphics grp = panel.CreateGraphics();
Size formSize = this.ClientSize;
bitmap = new Bitmap(formSize.Width, formSize.Height, grp);
grp = Graphics.FromImage(bitmap);
//Copy screen area that that the Panel covers.
Point panelLocation = PointToScreen(panel.Location);
grp.CopyFromScreen(panelLocation.X, panelLocation.Y, 0, 0, formSize);
//Show the Print Preview Dialog.
printPreviewDialog1.Document = printDocument1;
printPreviewDialog1.PrintPreviewControl.Zoom = 1;
printPreviewDialog1.ShowDialog();
}
private void PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
{
//Print the contents.
e.Graphics.DrawImage(bitmap, 0, 0);
}
这从表单 (Form2) 上的按钮 (btnPrint) 以及大量文本框和图形运行)
点击后会弹出打印预览对话框,但页面为空白。如果我按打印,它会打印一个空白页。
知道为什么它不复制表单吗??
【问题讨论】:
-
您通过弄乱该面板将这段代码黑死了。 printDocument1.PrintPage 事件处理程序的事件处理程序看起来很糟糕。使用调试器,在 PrintPage 事件处理程序上设置断点。预测它不会破裂。在设计器中双击 printDocument1 组件以添加事件处理程序。
-
或者更好:扔掉它,写一个像样的打印例程,而不是将屏幕分辨率转储到您的打印机..
-
对不起,我对此很陌生,我只是直接从这里的一篇文章中使用了这段代码,只是看不到它在哪里不起作用
-
我已经编辑了你的标题。请参阅“Should questions include “tags” in their titles?”,其中的共识是“不,他们不应该”。
标签: c# winforms printing desktop-application