代码之家  ›  专栏  ›  技术社区  ›  BellHopByDayAmetuerCoderByNigh

调整面板大小以打印整页

  •  0
  • BellHopByDayAmetuerCoderByNigh  · 技术社区  · 7 年前

    我在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 回复  |  直到 7 年前
        1
  •  1
  •   Matheus Avellar Will Jenkins    7 年前

    control.DrawToBitmap 确实如此。它打印 control

    DrawString