2011-04-18 60 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

相關問題