WPF - Graphics.CopyFromScreen 返回黑色图像
以下方法取自 WinForms 应用程序。它只是捕获屏幕,但我需要修改它才能在 WPF 应用程序中工作。当我使用它时,它返回黑色图像。尺寸正确。我没有任何打开的 DirectX 或视频,即使在我的桌面上它也无法工作。
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;
}
谁能告诉我我的方法有错误吗?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我相信您需要使用 Interop 和 BitBlt 方法。 此博客解释了这是如何完成的,并且后续帖子 显示如何获取窗口边框。
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.
我认为 g.CopyFromScreen 的前两个参数应该是 0。
这是适合我的代码:
I think the first two params to g.CopyFromScreen should be 0.
Here's code that works for me: