调整面板大小以打印整页

时间:2017-08-17 18:57:15

标签: c# winforms printing panel

我在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);
    }

1 个答案:

答案 0 :(得分:1)

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

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