WPF - Graphics.CopyFromScreen返回黑色图像

use*_*265 10 c# wpf graphics screenshot bitmap

以下方法取自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;
    }
Run Code Online (Sandbox Code Playgroud)

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

Cod*_*ked 7

我相信你需要使用Interop和BitBlt方法.这个博客解释了这是如何完成的,以及一个显示如何获取窗口边框的后续帖子.