title: Get a screenshot of a window that is cover or not visible or minimized with Xcomposite extension for X11
body: <p>I have the follow starting code to get a screenshot of a X window (the window can be covered, not visibled, or minimized).</p>
  
  <pre><code>#include &lt;stdlib.h&gt;
  #include &lt;stdio.h&gt;
  
  #include &lt;X11/Xlib.h&gt;
  #include &lt;X11/X.h&gt;
  #include &lt;X11/extensions/Xcomposite.h&gt;
  #include &lt;X11/extensions/Xrender.h&gt;
  
  int
  main ()
  {
    Display *display = XOpenDisplay (NULL);
    XID xid = 90177543; // xdotool search --name "World of Warcraft" | head -1
  
    // Check if Composite extension is enabled
    int event_base_return;
    int error_base_return;
    if (XCompositeQueryExtension (display, &amp;event_base_return, &amp;error_base_return))
      printf ("COMPOSITE IS ENABLED!\n");
  
    // Requests the X server to direct the hierarchy starting at window to off-screen storage
    XCompositeRedirectWindow (display, xid, CompositeRedirectAutomatic);
    // Preventing the backing pixmap from being freed when the window is hidden/destroyed
    // If you want the window contents to still be available after the window has been destroyed,
    // or after the window has been resized (but not yet redrawn), you can increment the backing
    // pixmaps ref count to prevent it from being deallocated.
    Pixmap pixmap = XCompositeNameWindowPixmap (display, xid);
  
    // Get window attributes
    XWindowAttributes attr;
    Status s = XGetWindowAttributes (display, xid, &amp;attr);
    if (s == 0)
      printf ("Fail to get window attributes!\n");
  
    // Extract the data
    XRenderPictFormat *format = XRenderFindVisualFormat (display, attr.visual);
    int width = attr.width;
    int height = attr.height;
    int depth = attr.depth;
  
    // What we need to do now is to create an XRender Picture for the window,
    // which we'll need to draw it with the Render extension.
    // A picture is a basically a handle to a server side struct with some
    // additional information about a drawable (in this case a window),
    // such as its format, which clipping region should be used when
    // drawing it (if any), whether it should be tiled etc.
    XRenderPictureAttributes pa;
    pa.subwindow_mode = IncludeInferiors;
    Picture picture = XRenderCreatePicture (display, xid, format, CPSubwindowMode, &amp;pa);
  
    // We now have all the information we need in order to be able to draw the window
    // using the Xrender extension, and we've created and prepared a source picture
    // for the window for this purpose.
    // The Xrender function we'll use to draw the window is XRenderComposite().
  
    //XRenderComposite (display, PictOpSrc, picture, None, ???destination???, 0,0, 0,0, 0,0, width, height);
  
    XFreePixmap (display, pixmap);
    XCompositeUnredirectWindow (display, xid, CompositeRedirectAutomatic);
  
    return 0;
  }
  </code></pre>
  
  <p>[Compile the code with <code>gcc file.c -lX11 -lXcomposite -lXrender</code> and run with <code>./a.out</code>]</p>
  
  <p>[You can get a valid XID of a window of your Desktop with the command <code>xdotool search --name "Title of a window" | head -1</code>]</p>
  
  <p>Now I have two problem:</p>
  
  <ol>
  <li>I get a <code>BadMatch (invalid parameter attributes)</code> error when I call the <code>XRenderFindVisualFormat</code> function. I dont know why. The window with XID 90177543 exists.</li>
  <li>I don't know how to get the buffer of the <code>Picture picture</code> to save it as a PNG image. I don't want use QT library to do this.</li>
  </ol>
  
  <p>Can you help me?</p>