title: How to read the screen pixels?
body: <p>I want to read a rectangular area, or whole screen pixels. As if screenshot button was pressed.</p>
  
  <p>How i do this?</p>
  
  <p><strong>Edit: Working code:</strong></p>
  
  <pre><code>void CaptureScreen(char *filename)
  {
      int nScreenWidth = GetSystemMetrics(SM_CXSCREEN);
      int nScreenHeight = GetSystemMetrics(SM_CYSCREEN);
      HWND hDesktopWnd = GetDesktopWindow();
      HDC hDesktopDC = GetDC(hDesktopWnd);
      HDC hCaptureDC = CreateCompatibleDC(hDesktopDC);
      HBITMAP hCaptureBitmap = CreateCompatibleBitmap(hDesktopDC, nScreenWidth, nScreenHeight);
      SelectObject(hCaptureDC, hCaptureBitmap); 
  
      BitBlt(hCaptureDC, 0, 0, nScreenWidth, nScreenHeight, hDesktopDC, 0,0, SRCCOPY|CAPTUREBLT); 
  
      BITMAPINFO bmi = {0}; 
      bmi.bmiHeader.biSize = sizeof(bmi.bmiHeader); 
      bmi.bmiHeader.biWidth = nScreenWidth; 
      bmi.bmiHeader.biHeight = nScreenHeight; 
      bmi.bmiHeader.biPlanes = 1; 
      bmi.bmiHeader.biBitCount = 32; 
      bmi.bmiHeader.biCompression = BI_RGB; 
  
      RGBQUAD *pPixels = new RGBQUAD[nScreenWidth * nScreenHeight]; 
  
      GetDIBits(
          hCaptureDC, 
          hCaptureBitmap, 
          0,  
          nScreenHeight,  
          pPixels, 
          &amp;bmi,  
          DIB_RGB_COLORS
      );  
  
      // write:
      int p;
      int x, y;
      FILE *fp = fopen(filename, "wb");
      for(y = 0; y &lt; nScreenHeight; y++){
          for(x = 0; x &lt; nScreenWidth; x++){
              p = (nScreenHeight-y-1)*nScreenWidth+x; // upside down
              unsigned char r = pPixels[p].rgbRed;
              unsigned char g = pPixels[p].rgbGreen;
              unsigned char b = pPixels[p].rgbBlue;
              fwrite(fp, &amp;r, 1);
              fwrite(fp, &amp;g, 1);
              fwrite(fp, &amp;b, 1);
          }
      }
      fclose(fp);
  
      delete [] pPixels; 
  
      ReleaseDC(hDesktopWnd, hDesktopDC);
      DeleteDC(hCaptureDC);
      DeleteObject(hCaptureBitmap);
  }
  </code></pre>