【问题标题】:Resize Panel To Print Full Page调整面板大小以打印整页
【发布时间】:2017-08-17 18:57:14
【问题描述】:

我在 tabcontrolpanel 上有一个面板,它占据了大约 35% 的屏幕。我正在打印面板,它会打印出屏幕上的确切尺寸。是否可以在 C# 和 winforms 中缩放图像以在 8.5x11 的纸上打印(即使我必须横向打印)?

这是我当前的代码--

private void btnPrint_Click(object sender, EventArgs e)
{
    PrintDocument doc = new PrintDocument();
    doc.PrintPage += new SPrintPageEventHandler(doc_PrintPage);
    doc.Print();
}

private void doc_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
{
    Bitmap image = new Bitmap(panelToPrint.Width, panelToPrint.Height, panelToPrint.CreateGraphics());
    panelToPrint.DrawToBitmap(image, new Rectangle(0, 0, panelToPrint.Width, panelToPrint.Height));
    RectangleF bounds = e.PageSettings.PrintableArea;
    float factor = ((float)image.Height / (float)image.Width);
    e.Graphics.DrawImage(image, bounds.Left, bounds.Top, bounds.Width, factor * bounds.Width);
}

编辑
我尝试将我的语法修改为默认以横向模式打印,但我仍然得到与以前相同的输出,只是图像大约打印在页面的顶部 15%

private void btnPrint_Click(object sender, EventArgs e)
    {
        System.Drawing.Printing.PrintDocument doc = new System.Drawing.Printing.PrintDocument();
        doc.DefaultPageSettings.Landscape = true;
        PrintDialog pdi = new PrintDialog();
        pdi.Document = doc;
        if (pdi.ShowDialog() == DialogResult.OK)
        {
            doc.PrintPage += new System.Drawing.Printing.PrintPageEventHandler(doc_PrintPage);
            doc.Print();
        }
        else
        {
            MessageBox.Show("User cancelled the print job");
        }
    }

    private void doc_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
    {
        Bitmap image = new Bitmap(panelToPrint.Width, panelToPrint.Height, panelToPrint.CreateGraphics());
        panelToPrint.DrawToBitmap(image, new Rectangle(0, 0, panelToPrint.Width, panelToPrint.Height));
        RectangleF bounds = e.PageSettings.PrintableArea;
        float factor = ((float)image.Height / (float)image.Width);
        e.Graphics.DrawImage(image, bounds.Left, bounds.Top, bounds.Width, factor * bounds.Width);
    }

【问题讨论】:

    标签: c# winforms printing panel


    【解决方案1】:

    control.DrawToBitmap 正是这样做的。它将control(如您在屏幕上看到的那样)打印到位图。如果您想以不同的尺寸打印它,您必须在打印前调整面板的大小。

    更好的方法是使用DrawString 等图形方法将面板中包含的数据直接绘制到页面。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-09-15
      • 2011-11-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-08-18
      相关资源
      最近更新 更多