WPF - Graphics.CopyFromScreen returns a black image

后端 未结 2 1933
长发绾君心
长发绾君心 2021-02-08 14:32

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

相关标签:
2条回答
  • 2021-02-08 15:06

    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();
    
    0 讨论(0)
  • 2021-02-08 15:15

    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.

    0 讨论(0)
提交回复
热议问题