WPF - Graphics.CopyFromScreen返回一个黑色图像 [英] WPF - Graphics.CopyFromScreen returns a black image
问题描述
下面的方法是从WinForms应用程序拍摄。它只是捕捉画面,但我需要修改它在WPF应用程序的工作。当我使用它,它返回一个黑色图像。尺寸是正确的。我还没有得到任何打开的DirectX或视频,它不会在我的桌面上,甚至工作。
The following method is taken from a WinForms app. It simply captures the screen, but I needed to modify it to work in a WPF application. When I use it, it returns a black image. The dimensions are correct. I have not got any open DirectX or videos and it wouldn't work even on my desktop.
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;
}
谁能证明我的错误,我的方式?
Can anyone show me the error in my ways?
推荐答案
我相信你需要使用互操作和BitBlt的方法。 <一href=\"http://10rem.net/blog/2011/02/08/capturing-screen-images-in-wpf-using-gdi-win32-and-a-little-wpf-interop-help\"相对=nofollow>此博客解释如何做到这一点,和的后续帖子演示了如何获得窗口边框。
I believe you need to use Interop and the BitBlt method. This blog explains how this is done, and a follow-on post that shows how to get window borders.
这篇关于WPF - Graphics.CopyFromScreen返回一个黑色图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!