如何在WinFormc#中打印面板?

问题描述 投票:0回答:2

我有一个带有标签和数据网格视图的面板。我正在尝试使用此代码打印面板及其内容

    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();

但它什么也没打印。我删除了 datagridview 但仍然不打印任何内容。然后我更改面板背景颜色,但它再次打印白色页面。请指导我如何做到这一点?

c# winforms printing panel
2个回答
5
投票

将此代码添加到

PrintPage
事件并从
Print()
类对象调用
PrintDocument
方法

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();
}

0
投票

如何在一页上打印相同的两个面板?

最新问题
© www.soinside.com 2019 - 2025. All rights reserved.