Can I get a bitmap of an arbitrary window in another application process?

旧街凉风 提交于 2019-12-06 06:03:47

This modification of the PrintWindow API example on the pinvoke.net site seems to have done the trick.

Bitmap bmp = new Bitmap((int)rect.Width, (int)rect.Height);
Graphics memoryGraphics = Graphics.FromImage(bmp);
IntPtr dc = memoryGraphics.GetHdc();
bool success = PrintWindow(hwnd, dc, 0);
memoryGraphics.ReleaseHdc(dc);
bmp.Save(@"c:\screenshot.bmp");

This works if the app is covered by another window, but it doesn't work if the app is minimized. I think I can live with that.

There is no reliable way to get a bitmap from a different app if that app is not on top. That's because the app's controls do not even render if the app isn't visible, and Windows does not necessarily even remember what the last known contents of the control were after that control loses the topmost position in the z-order.

Your best bet is to move the target app to the front of the z-order at the time that you need to take the screenshot, and then optionally restore the original z-order after capturing the image.

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