2

I have a remote application that the a screenshot using "windows handle". ( I mean HDC, HBITMAP, ....).

The code look like this :

int nScreenWidth = GetSystemMetrics(SM_CXSCREEN);
int nScreenHeight = GetSystemMetrics(SM_CYSCREEN);
HDC hDesktopDC = CreateDC(TEXT("DISPLAY"),NULL,NULL,NULL);
HDC hCaptureDC = CreateCompatibleDC(hDesktopDC);

HBITMAP hCaptureBitmap =CreateCompatibleBitmap(
    hDesktopDC, 
    nScreenWidth,
    nScreenHeight);

SelectObject(hCaptureDC,hCaptureBitmap); 

BitBlt(
    hCaptureDC,
    0,0,
    nScreenWidth,nScreenHeight,
    hDesktopDC,
    0,0,
    SRCCOPY); 

BITMAPINFOHEADER   info;

info.biSize = sizeof(BITMAPINFOHEADER);
info.biWidth = nScreenWidth;   
info.biHeight = nScreenHeight; 
info.biPlanes = 1; 
info.biBitCount = 32;
info.biCompression = BI_RGB;    
info.biSizeImage = 0;
info.biXPelsPerMeter = 0;
info.biYPelsPerMeter = 0;
info.biClrUsed = 0;
info.biClrImportant = 0;

//reteive the image data 
byte *bits= (byte*)malloc(nScreenWidth*nScreenHeight*4);
GetDIBits(hDesktopDC,   // handle to DC
    hCaptureBitmap,     // handle to bitmap
    0,                  // first scan line to set
    nScreenHeight,      // number of scan lines to copy
    bits,               // array for bitmap bits
    (BITMAPINFO*)&info, // bitmap data buffer
    DIB_RGB_COLORS      // RGB 
    );

DeleteDC(hDesktopDC);
DeleteDC(hCaptureDC);
DeleteObject(hCaptureBitmap);

After many loop the (about 2000) the CreateDC() function return NULL. And, if I old the DC (mean intialize it once, then destroyed on application exit) my application window flick (or part of it) or even get entirely invisible.

Thus, I'll need to know how to figure this issue out or know any other better way to get the screen image (bits/RGB data).

Captain Obvlious
  • 19,754
  • 5
  • 44
  • 74
Monero Jeanniton
  • 441
  • 8
  • 20
  • Would you be so kind as to remove all the extra whitespace? It makes the code really annoying to read. – Borgleader Aug 02 '13 at 22:01
  • Here is the tutorial about capturing the screen by various methods. http://www.codeproject.com/Articles/5051/Various-methods-for-capturing-the-screen. I hope it helps. – AnkitJain Aug 03 '13 at 16:55
  • I have tried both directx and dc way. And the directx itself return black image after a lot of capture. @AnkitJain – Monero Jeanniton Aug 19 '13 at 12:09

0 Answers0