我試圖獲得一個按鈕來打印出我當前的表單,並嘗試了所有我可以在這裏找到的代碼,但它仍然打印空白頁,我無法解決原因。在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看看它不在工作 –