以下方法是从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;
    }

谁能以我的方式向我显示错误?

最佳答案

我相信您需要使用Interop和BitBlt方法。 This blog解释了如何做到这一点,以及follow-on post显示了如何获取窗口边框。

关于c# - WPF-Graphics.CopyFromScreen返回黑色图像,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/5708985/

10-09 13:18