0

我想打印整个表格的尺寸(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();
    }

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

4

1 回答 1

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);
        }
    }
于 2013-05-20T10:19:27.453 回答