如何在WinFormc#中打印面板
本文关键字:打印 WinFormc# | 更新日期: 2023-09-27 18:26:53
我有一个面板,里面有标签和数据网格视图。我正在尝试使用此代码打印面板及其内容
PrintDialog myPrintDialog = new PrintDialog();
System.Drawing.Bitmap memoryImage = new System.Drawing.Bitmap(panel2.Width, panel2.Height);
panel2.DrawToBitmap(memoryImage, panel2.ClientRectangle);
myPrintDialog.ShowDialog();
System.Drawing.Printing.PrinterSettings values;
values = myPrintDialog.PrinterSettings;
myPrintDialog.Document = printDocument1;
printDocument1.PrintController = new StandardPrintController();
printDocument1.Print();
printDocument1.Dispose();
但它什么也没印。我删除了数据网格视图,但仍然不打印任何内容。然后我改变了面板的背面颜色,但它再次打印出白色页面。请指导我怎么做?
将此代码添加到PrintPage
事件并从PrintDocument
类对象调用Print()
方法
private void doc_PrintPage(object sender, PrintPageEventArgs e)
{
float x = e.MarginBounds.Left;
float y = e.MarginBounds.Top;
Bitmap bmp = new Bitmap(panel2.Width, panel2.Height);
panel2.DrawToBitmap(bmp, new Rectangle(0, 0, panel2.Width, panel2.Height));
e.Graphics.DrawImage((Image)bmp, x, y);
}
调用类似的方法
PrintDocument doc = new PrintDocument();
doc.PrintPage += this.doc_PrintPage;
PrintDialog dlg = new PrintDialog();
dlg.Document = doc;
if (dlg.ShowDialog() == DialogResult.OK)
{
doc.Print();
}