以下方法取自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;
}
任何人都可以用我的方式向我顯示錯誤嗎?
仍然無法正常工作,圖像尺寸似乎正確,但整個圖像是黑色的。謝謝,不過。 – user646265 2011-04-18 21:29:39