-->

Graphics.CopyFromScreen returns black screen

2020-08-01 08:35发布

问题:

I have a small application that takes screen shots and saves them to the folder its in. It works fine most of the time, but in some cases, for example while in Team Fortress 2 or while running Warcraft 3 in OpenGL mode it just returns a totally black(or white) image. Does anyone have a way to fix this?

I am using the c# standard:

Bitmap bmp;
Graphics gfx;
bmp = new Bitmap(Screen.PrimaryScreen.Bounds.Width, Screen.PrimaryScreen.Bounds.Height, PixelFormat.Format32bppArgb);
gfx = Graphics.FromImage(bmpScreenshot);
gfx.CopyFromScreen(Screen.PrimaryScreen.Bounds.X, Screen.PrimaryScreen.Bounds.Y, 0, 0, Screen.PrimaryScreen.Bounds.Size, CopyPixelOperation.SourceCopy);
bmpScreenshot.Save("image.jpg", ImageFormat.Jpeg);

回答1:

Windows screenshot functions do not copy data from DirectX surfaces. You have to do that manually like described in this article.



回答2:

I understand it is because those types of graphics are over-laid by your graphics card. It's also why you probably can't grab bits of the screen showing video too.

Control Panel --> Display Properties --> Settings --> Advanced --> Troubleshoot

I think there is an option in there to reduce hardware acceleration which should allow you to grab them.

I know I had to do this in order to grab screenshots from video, hopefully this will help with your opengl stuff.



回答3:

You are assuming that the screen is in PixelFormat.Format32bppArgb this might not always be the case, though I don't know whether this would be relevant given the other answers.

If it is relevant you could try Screen.PrimaryScreen.BitsPerPixel to get the depth of the screen. I haven't tried it so I don't know whether it would work or not.