WPF - Graphics.CopyFromScreen 返回黑色图像

发布于 2024-11-02 00:38:09 字数 987 浏览 0 评论 0原文

以下方法取自 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

音盲 2024-11-09 00:38:09

我相信您需要使用 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.

删除→记忆 2024-11-09 00:38:09

我认为 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();

I think the first two params to g.CopyFromScreen should be 0.

Here's code that works for me:

        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();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文