title: A weird error about GetDIBits and device context
body: <p>A very weird problem I encountered today. Running codes below on VC6 with MFC project and it is black screen, it works perfectly and shows desktop picture if I take out the comment. However these codes are executed in a infinite loop, so I try to reduce memory copy and memory spend like <code>BitBlt</code> and <code>CreateCompatibleBitmap</code> etc. I do not understand how is my program related to these commented codes. Anyone knows what cause the problem and why?</p>
  
  <pre><code>HDC hdcDesktop = ::CreateDC("DISPLAY", NULL, NULL, NULL);
  RECT desktopRect;
  ::GetWindowRect(::GetDesktopWindow(), &amp;desktopRect);
  int desktopWidth = desktopRect.right - desktopRect.left;
  int desktopHeight = desktopRect.bottom - desktopRect.top;
  
  HBITMAP hBitmap = CreateCompatibleBitmap(hdcDesktop, desktopWidth, desktopHeight);
  /*
  HDC hdcMemory = CreateCompatibleDC(hdcDesktop);
  SelectObject(hdcMemory, hBitmap);
  BitBlt(hdcMemory, 0, 0, desktopWidth, desktopHeight, hdcDesktop, 0, 0, SRCCOPY);
  */
  
  BITMAPINFO bitmapInfo = {0};
  bitmapInfo.bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
  
  GetDIBits(hdcDesktop, hBitmap, 0, 0, NULL, &amp;bitmapInfo, DIB_RGB_COLORS);
  BYTE *pData = new BYTE[bitmapInfo.bmiHeader.biSizeImage];
  memset(pData, 0, bitmapInfo.bmiHeader.biSizeImage);
  GetDIBits(hdcDesktop, hBitmap, 0, bitmapInfo.bmiHeader.biHeight, pData, &amp;bitmapInfo, DIB_RGB_COLORS);
  
  CRect destRect;
  GetClientRect(&amp;destRect);
  StretchDIBits(::GetDC(m_hWnd), 0, 0, destRect.Width(), destRect.Height(), 0, 0, bitmapInfo.bmiHeader.biWidth, bitmapInfo.bmiHeader.biHeight, 
      pData, &amp;bitmapInfo, DIB_RGB_COLORS, SRCCOPY);
  </code></pre>