c# - 如何在 WinForms# 中打印面板?

标签 c# winforms printing panel

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

    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 但仍然不打印任何内容。然后我改变面板的背景颜色,但它再次打印白页。请指导我该怎么做?

最佳答案

将此代码添加到 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();
}

关于c# - 如何在 WinForms# 中打印面板?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26436033/

相关文章:

c# - 多层架构和身份。为什么要使用UserManager和RoleManager?

c# - 将图片框锁在角落?

c# - 覆盖 winform 窗口的调整大小行为

iphone - 需要有关将 UIView 打印到纸质打印机的帮助

javascript - PDFBox setOpenAction 打印

python - 在 PyQt 中打印图像时出错

c# - context.Database.ExecuteSqlCommand - 错误代码 701 - Microsoft Azure

c# - 如何确定正在执行的程序是否为 64 位编译?

c# - ASP.NET Core API 如何在操作方法中将 ActionResult<T> 转换为 T

winforms - 如何创建一个像 VS 2010 一样形状奇特的高质量闪屏?