Screenshot captured using BitBlt in C# results a black image on Windows 10

时光总嘲笑我的痴心妄想 提交于 2019-11-30 15:34:04

Hardware accelerated windows are rendered using overlay mode, which means that your BitBlt only gets pixels that say "Hey, this is overlay!". When the overlay isn't being rendered, this results in a black image - and if it is being rendered, you always see the current render, not something frozen in time. You're not capturing the pixels being shown on the screen, just some internal details of how window rendering works.

The solution is fortunately quite simple:

BitBlt(hdcDest, 0, 0, width, height, hdcSrc, 0, 0, 
       CopyPixelOperation.SourceCopy | CopyPixelOperation.CaptureBlt);

(you can modify your BitBlt P/Invoke definition to use CopyPixelOperation instead of int, or just cast those values to int yourself).

As a side-note, please don't forget to check the return values and handle errors accordingly.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!