【问题标题】:WPF - Graphics.CopyFromScreen returns a black imageWPF - Graphics.CopyFromScreen 返回黑色图像
【发布时间】:2011-08-08 05:10:31
【问题描述】:

以下方法取自 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;
    }

谁能以我的方式告诉我错误?

【问题讨论】:

    标签: c# wpf graphics bitmap screenshot


    【解决方案1】:

    我相信您需要使用 Interop 和 BitBlt 方法。 This blog 解释了这是如何完成的,follow-on post 显示了如何获取窗口边框。

    【讨论】:

    • 10rem.net 的链接已失效
    • @grim - 他们在为我工作。也许它昨天才下来?
    • 是的,抱歉。一定是一劳永逸。域名续订可能会延迟!
    • 链接失效
    • @VictorioPui - 两个链接都对我有用,但不支持 HTTPS。也许这就是您看到的问题?
    【解决方案2】:

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

    【讨论】:

    • 仍然无法正常工作,图像尺寸再次看起来正确,但整个图像是黑色的。不过,谢谢。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-04-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-09-12
    • 2019-06-21
    • 1970-01-01
    相关资源
    最近更新 更多