我试图获得一个按钮来打印出我当前的表单,并尝试了所有我可以在这里找到的代码,但它仍然打印空白页,我无法解决原因。在Windows应用程序中打印表格
我使用的代码如下
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);
}
这从按钮(btnPrint),它是一个窗体(Form 2)上运行,以文本框的负荷和图形)
沿着当点击它带来打印预览对话框正常,但页面为空白。如果我按打印它打印一个空白页。
任何想法为什么它不复制的形式?
您可以通过与面板搞乱砍死这个代码死亡。您的printDocument1.PrintPage事件处理程序的事件处理程序看起来很糟糕。使用调试器,在PrintPage事件处理程序上设置断点。预测它不会中断。双击设计器中的printDocument1组件以添加事件处理程序。 –
或更好的是:扔掉它,写一个体面的打印例程,而不是倾倒屏幕分辨率到您的打印机.. – TaW
对不起,我对此很新,我只是直接从这里的文章使用此代码,只是couldn' t看看它不在工作 –