WPF - Graphics.CopyFromScreen returns a black image

后端 未结 2 1928
长发绾君心
长发绾君心 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();
    

提交回复
热议问题