如何在 Linux 上的 C++ 中截取模糊窗口的屏幕截图 [英] How to take screenshot of obscured window in C++ on Linux

查看:66
本文介绍了如何在 Linux 上的 C++ 中截取模糊窗口的屏幕截图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想弄清楚如何截取当前未聚焦的窗口的屏幕截图,因此该窗口很可能会被其他窗口部分或完全遮挡.

我在此链接上找到了一个示例

解决方案

首先 Qt 有这个功能.您可以使用:QScreen::grabWindow.问题是文档说:

<块引用>

注意 X11,如果给定窗口的深度与根窗口,另一个窗口部分或完全遮挡了你抓住一个,你不会从覆盖的窗口中获得像素.这像素图中被遮挡区域的内容将是未定义的,并且未初始化.

因此这将简化您的代码,但窗口的模糊部分仍然是一个问题.看起来 x11 的功能无法解决此问题.

有一个很好的例子如何使用这个功能.

I'm trying to figure out how to take a screenshot of a window that is currently not focused, so there is a good chance that the window will be partially or fully obscured by other windows.

I've found an example here on this link Get a screenshot of a window that is cover or not visible or minimized with Xcomposite extension for X11 but I can't make it work, any time I take a screenshot I get only strange output, mostly black, like I'm accessing the wrong buffer or something.

  XID xid = windowID;  // Checked and confirmed that the window ID is correct
  XGetWindowAttributes( display, windowID, &attrributes );

  XCompositeRedirectWindow (display, xid, CompositeRedirectAutomatic);

  Pixmap pixmap = XCompositeNameWindowPixmap (display, xid);

  // Extract the data
  XRenderPictFormat *format = XRenderFindVisualFormat (display, attrributes.visual);

  XRenderPictureAttributes pa;
  pa.subwindow_mode = IncludeInferiors;
  Picture picture = XRenderCreatePicture (display, xid, format, CPSubwindowMode, &pa);

  QPixmap finalPix (attrributes.width, attrributes.height);
  XRenderComposite (display, PictOpSrc, picture, None, finalPix.x11PictureHandle(), 0,0, 0,0, 0,0, attrributes.width, attrributes.height);

  XFreePixmap (display, pixmap);
  XCompositeUnredirectWindow (display, xid, CompositeRedirectAutomatic);

  return finalPix;

(Edit: This screenshot was taken from a fully visible window, not an obscured window, so I guess currently the issue is not even that X11 doesn't draw it but my implementation seems to be not working and I can't figure out why.)
And this is how a screenshot of my konsole window looks:

解决方案

First of all Qt has this feature. You can use: QScreen::grabWindow. Problem is that documentation says:

Note on X11 that if the given window doesn't have the same depth as the root window, and another window partially or entirely obscures the one you grab, you will not get pixels from the overlying window. The contents of the obscured areas in the pixmap will be undefined and uninitialized.

So this will simplify your code, but obscured parts of window will still remains as a problem. Looks like functionality of x11 won't let to resolve this issue.

There is a good example how to use this feature.

这篇关于如何在 Linux 上的 C++ 中截取模糊窗口的屏幕截图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆