title: How to access image data of a win32 thumbnail
body: <p>I am trying to access the image data of a win32 thumbnail to use it with another library using the following steps:</p>
  
  <ol>
  <li>Get the thumbnail (an ISharedBitmap) from the windows thumbnail cache</li>
  <li>Get the HBITMAP of the thumbnail</li>
  <li>Get the pixel data using GetDIBits</li>
  </ol>
  
  <p>Thumbnail retrieval is based on <a href="https://stackoverflow.com/a/19529633/3165122">https://stackoverflow.com/a/19529633/3165122</a></p>
  
  <p>Pixel data retrieval is based on <a href="https://stackoverflow.com/a/3688682/3165122">https://stackoverflow.com/a/3688682/3165122</a></p>
  
  <p>This is what I got so far:</p>
  
  <pre><code>// error checks omitted
  LPCTSTR path = TEXT("&lt;path-to-jpg-image&gt;");
  
  HRESULT hr = CoInitialize(nullptr);
  
  // Get the thumbnail
  IShellItem* item = nullptr;
  hr = SHCreateItemFromParsingName(path, nullptr, IID_PPV_ARGS(&amp;item));
  
  IThumbnailCache* cache = nullptr;
  hr = CoCreateInstance(
      CLSID_LocalThumbnailCache,
      nullptr,
      CLSCTX_INPROC,
      IID_PPV_ARGS(&amp;cache)); 
  
  ISharedBitmap* shared_bitmap;
  hr = cache-&gt;GetThumbnail(
      item,
      1024,
      WTS_EXTRACT,
      &amp;shared_bitmap,
      nullptr,
      nullptr);
  
  // Retrieve thumbnail HBITMAP
  HBITMAP hbitmap = NULL;
  hr = shared_bitmap-&gt;GetSharedBitmap(&amp;hbitmap);
  
  HDC dc = GetDC(NULL);
  HDC dc_mem = CreateCompatibleDC(dc);
  
  // Get required buffer size
  BITMAPINFO bmi;
  bmi.bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
  GetDIBits(dc_mem, hbitmap, 0, 0, nullptr, &amp;bmi, DIB_RGB_COLORS); // &lt;- this fails
  
  WTS_ALPHATYPE alpha_type;
  hr = shared_bitmap-&gt;GetFormat(&amp;alpha_type);
  
  bmi.bmiHeader.biBitCount = alpha_type == WTSAT_RGB ? 24 : 32;
  bmi.bmiHeader.biHeight = std::abs(bmi.bmiHeader.biHeight);
  bmi.bmiHeader.biCompression = BI_RGB;
  
  // Get image data
  std::vector&lt;char&gt; buffer(bmi.bmiHeader.biSizeImage);
  GetDIBits(dc_mem, hbitmap, 0, bmi.bmiHeader.biHeight,
          &amp;buffer[0], &amp;bmi, DIB_RGB_COLORS);
  
  // use buffer...
  </code></pre>
  
  <p>The first call to GetDIBits to retrieve the required buffer size fails with return value 0. I am guessing I'm using an incompatible DC? How do I get a compatible DC?</p>
  
  <p><a href="https://stackoverflow.com/a/14207674/3165122">https://stackoverflow.com/a/14207674/3165122</a> states "[...] if you don't know what your HBITMAP is pointing to, you can't expect to do anything useful with it."</p>
  
  <p>Is there really no way? Am I doing something wrong? Is there a different approach?</p>