2013-05-20 46 views
0

我想打印整个形式的大小(1415x1000)的。但它是打印尺寸(1185x740)的形式。PrintDocument的不打印完整形式

我指的是代码从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(); 
    } 

但我不能让打印输出中的完整形式。我怎么做?

+0

对不起,我不知道纸张大小。在printpreview选项本身我无法获得完整的表单。请说,我可以用纸做什么?@matzone – Developer

+0

@matzone:我也试过纸张尺寸也没有帮助。我在使用纸张大小时可能会出错,或者可能有其他问题。请帮助解决这个问题。谢谢... – Developer

+0

@matzone:已在去年第四行 – Developer

回答

0

由Graphics使用的默认缩放目的是通过创建的PrintDocument是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); 
     } 
    } 
+0

吨的感谢@Hans Passant ..它工作得很好..顺便说一下,有可能使任务栏不可见。 – Developer

+1

使用Screen.WorkingArea而不是边界。 –

+0

非常感谢您先生...再次感谢... – Developer