2

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:

The resulting screenshot

Community
  • 1
  • 1
Damir Porobic
  • 681
  • 1
  • 8
  • 21
  • It looks like I'm not copping anything into the QPixmap. When I create an empty QPixmap with some size I get the same output... – Damir Porobic Oct 12 '16 at 18:59

1 Answers1

3

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.

Marek R
  • 32,568
  • 6
  • 55
  • 140
  • Thanks for pointing this out though I believe this function was introduced in Qt5.0 and I'm using 4.8. Further, it doesn't resolve my primary issue, which is that I can't capture obscured windows or frames. Just a screenshot from a window that is visible is not a problem, I have the WId and from there I get the window attributes like window position and size and they I just grab a rect at that position, that is not a problem. I'm not sure if this is actually doable with X11 or do I have to use something else... – Damir Porobic Oct 12 '16 at 05:48
  • This actually doesn't answer my question at all. I'm looking for a solution to take a screenshot of an obscured window and the answer says that this is not possible with this solution, so totally misleading "_The contents of the obscured areas in the pixmap will be undefined and uninitialized._". – Damir Porobic Nov 02 '16 at 09:59