3

我正在使用 CreateGraphics 函数打印我的活动表单(formMain),如下代码

打印表单 (Visual C#)

环境:经典模式下的windows 7专业版,VisuaStudio 2008

[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, 13369376);
   mygraphics.ReleaseHdc(dc1);
   memoryGraphics.ReleaseHdc(dc2);
}
private void printDocument1_PrintPage(System.Object sender, System.Drawing.Printing.PrintPageEventArgs e)
{
   e.Graphics.DrawImage(memoryImage, 0, 0);
}
private void printButton_Click(System.Object sender, System.EventArgs e)
{
   if (MessageBox.Show("test", "test", MessageBoxButtons.YesNo) == DialogResult.Yes)
   {
        CaptureScreen();
        printDocument1.Print();
   }
}

case1 : MessageBox 默认显示位置时,formMain 可以清晰打印 清楚地

情况2: 但是,如果使用鼠标将messageBox移出默认位置,则打印结果是脏的。它现在在 mainForm 中包含 messageBox(图形)。

肮脏的

4

2 回答 2

1

而不是直接使用 WinAPI (BitBlt, ...) 尝试使用这个:

Bitmap bmp = new Bitmap(this.Width, this.Height);
this.DrawToBitmap(bmp, this.Bounds);
bmp.Save(@"c:\temp\test.png", System.Drawing.Imaging.ImageFormat.Png); // For testing

这应该只绘制表单的内容。
我用另一个最顶层的表格成功地测试了这个。

将其放在您的代码中:

private void CaptureScreen() {
    memoryImage = new Bitmap(this.Width, this.Height);
    this.DrawToBitmap(memoryImage, this.Bounds);
}
于 2013-08-12T08:39:05.683 回答
0

像XP。一种对策是将 this.Update(); 放在 CaptureScreen() 调用之前。

我认为我的系统是 windows 7,但使用经典模式 -> 作为 windows xp

于 2013-08-13T00:45:34.760 回答