title: Capture and Save the Screenshot of a Window in MFC (C++)
body: <p>I want to capture the screenshot of a window in MFC and save it as an image file. Following the examples, I got something as below for it</p>
  <pre><code>OnSave()
  {
      CRect rect;
      GetWindowRect(&amp;rect);
      CImage* img = new CImage();
      img-&gt;Create(rect.Width(), rect.Height(), 32);
      HDC device_context_handle = img-&gt;GetDC();
      HWND hwnd = this-&gt;GetSafeHwnd();
      bool IsPrint =::PrintWindow(hwnd, device_context_handle, PW_CLIENTONLY);
      HRESULT res = img-&gt;Save(L&quot;image12.bmp&quot;, Gdiplus::ImageFormatBMP);
      img-&gt;ReleaseDC();
  
      delete img;
  }
  </code></pre>
  <p>Doing this, I get the file with the correct dimensions but it is always black. I checked and the result saving (res) is also always 0.  I tried to check different examples online but I could not get this black image issue fixed. Is there a suggestion to modify this or do this differently?</p>