title: How to take screenshot of obscured window in C++ on Linux
body: <p>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. </p>
  
  <p>I've found an example here on this link <a href="https://stackoverflow.com/questions/21512177/get-a-screenshot-of-a-window-that-is-cover-or-not-visible-or-minimized-with-xcom">Get a screenshot of a window that is cover or not visible or minimized with Xcomposite extension for X11</a> 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.  </p>
  
  <pre><code>  XID xid = windowID;  // Checked and confirmed that the window ID is correct
    XGetWindowAttributes( display, windowID, &amp;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, &amp;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;
  </code></pre>
  
  <p><strong>(Edit:</strong> <em>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.<strong></em>)</strong><br>
  And this is how a screenshot of my konsole window looks:  </p>
  
  <p><a href="https://i.stack.imgur.com/9eyZC.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/9eyZC.png" alt="The resulting screenshot"></a></p>