PrintDocument不会打印完整的表单

时间:2013-05-20 08:35:04

标签: c# printdocument

我想打印整个形式的大小(1415x1000)。但它正在打印尺寸形式(1185x740)。

我参考了msdn网站上的代码:

 [System.Runtime.InteropServices.DllImport("gdi32.dll")]
    public static extern long BitBlt(IntPtr hdcDest, int nXDest, int nYDest, int nWidth, int nHeight, IntPtr hdcSrc, int nXSrc, int nYSrc, int dwRop);
    private Bitmap memoryImage;

    private void CaptureScreen()
    {

        Graphics mygraphics = this.CreateGraphics();

        Size s = this.Size;

        memoryImage = new Bitmap(s.Width, s.Height, mygraphics);

        Graphics memoryGraphics = Graphics.FromImage(memoryImage);
        IntPtr dc1 = mygraphics.GetHdc();

        IntPtr dc2 = memoryGraphics.GetHdc();
        BitBlt(dc2, 0, 0, this.ClientRectangle.Width, this.ClientRectangle.Height, dc1, 0, 0, 13369377);//13369376);

        mygraphics.ReleaseHdc(dc1);

        memoryGraphics.ReleaseHdc(dc2);

    }

  private void printDocument1_PrintPage(object sender, PrintPageEventArgs e)
    {
        e.Graphics.DrawImage(memoryImage, 0, 0);
    }

 private void button1_Click_1(object sender, EventArgs e)
    {
        panel1.Hide();
        CaptureScreen();
        PrintDocument myDoc = new PrintDocument();
        myDoc.PrinterSettings.DefaultPageSettings.PaperSize = new    System.Drawing.Printing.PaperSize("Custom", 1415, 1000);
        PrintPreviewDialog print_dlg = new PrintPreviewDialog();
        print_dlg.Document = printDocument1;
        print_dlg.ShowDialog();
    }

但我无法在打印输出中获得完整的表格。我该怎么做?

1 个答案:

答案 0 :(得分:0)

PrintDocument创建的Graphics对象使用的默认缩放是GraphicsUnit.Display。将100像素映射到一英寸。这通常与视频适配器的每英寸点数设置很匹配,默认值为每英寸96像素。因此,无论您在纸上打印的大小与显示器上的大小大致相同。

对于您的屏幕截图而言效果不佳,但是您的显示器太大了。或者你使用的纸张太小,请选择;)你必须把它画得更小。使用Graphics.DrawImage(Image,Rectangle)重载,或者更简单,使用Graphics.ScaleTransform()将其绘制得更小。您可能还想使用Graphics.TranslateTransform(),因此图像以纸张为中心。使用e.MarginBounds查找纸张的大小。 PrintDocument事件处理程序的一些示例代码,演示如何使其适用于任何监视器大小:

    private void printDocument1_QueryPageSettings(object sender, System.Drawing.Printing.QueryPageSettingsEventArgs e) {
        e.PageSettings.Landscape = true;
    }

    private void printDocument1_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e) {
        // Make screenshot
        var scr = Screen.FromPoint(this.Location);
        using (var bmp = new Bitmap(scr.Bounds.Width, scr.Bounds.Height)) {
            using (var gr = Graphics.FromImage(bmp)) {
                gr.CopyFromScreen(new Point(scr.Bounds.Left, scr.Bounds.Top), Point.Empty, bmp.Size);
            }
            // Determine scaling
            float scale = 1.0f;
            scale = Math.Min(scale, (float)e.MarginBounds.Width / bmp.Width);
            scale = Math.Min(scale, (float)e.MarginBounds.Height / bmp.Height);
            // Set scaling and offset
            e.Graphics.TranslateTransform(e.MarginBounds.Left + (e.MarginBounds.Width - bmp.Width * scale) / 2, 
                                          e.MarginBounds.Top + (e.MarginBounds.Height - bmp.Height * scale) / 2);
            e.Graphics.ScaleTransform(scale, scale);
            // And draw
            e.Graphics.DrawImage(bmp, 0, 0);
        }
    }