2011-04-18 113 views
9

以下方法取自WinForms应用程序。它只是捕获屏幕,但我需要修改它在WPF应用程序中工作。当我使用它时,它会返回一个黑色的图像。尺寸是正确的。我还没有任何开放的DirectX或视频,即使在我的桌面上也不行。WPF - Graphics.CopyFromScreen返回一个黑色图像

public static Bitmap CaptureScreen() 
    { 
     // Set up a bitmap of the correct size 

     Bitmap CapturedImage = new Bitmap((int)SystemParameters.VirtualScreenWidth, 
      (int)SystemParameters.VirtualScreenHeight, System.Drawing.Imaging.PixelFormat.Format32bppArgb); 

     // Create a graphics object from it 
     System.Drawing.Size size = new System.Drawing.Size((int)SystemParameters.VirtualScreenWidth, (int)SystemParameters.VirtualScreenHeight); 

     using (Graphics g = Graphics.FromImage(CapturedImage)) 
     { 
      // copy the entire screen to the bitmap 
      g.CopyFromScreen((int)SystemParameters.VirtualScreenWidth, (int)SystemParameters.VirtualScreenHeight, 0, 0, 
       size, CopyPixelOperation.SourceCopy); 
     } 
     return CapturedImage; 
    } 

任何人都可以用我的方式向我显示错误吗?

回答

6

我相信你需要使用互操作和BitBlt的方法。 This blog解释了这是如何完成的,以及follow-on post,它显示了如何获得窗口边界。

1

我认为首先两个参数来g.CopyFromScreen应该是0

这里的代码为我的作品:

 var size = System.Windows.Forms.Screen.PrimaryScreen.Bounds; 
     var capture = new System.Drawing.Bitmap(size.Width, size.Height); 

     var g = System.Drawing.Graphics.FromImage(capture); 
     g.CopyFromScreen(0, 0, 0, 0, new System.Drawing.Size(size.Width, size.Height)); 
     g.Dispose(); 
+0

仍然无法正常工作,图像尺寸似乎正确,但整个图像是黑色的。谢谢,不过。 – user646265 2011-04-18 21:29:39