Advertisement
Guest User

get screenshot

a guest
Jan 20th, 2014
398
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.01 KB | None | 0 0
  1. //
  2. // Cteni screenshotu. Dynamicky vytvori handles, pokud je jeste nemame.
  3. // Typy promennych - viz MSDN.
  4. //
  5. void get_screenshot()
  6. {
  7.   // vyrez obrazu, ktery chceme cist
  8.   int x = image_x;   // offset
  9.   int y = image_y;
  10.   int cx = image_w;  // sirka, vyska od offsetu
  11.   int cy = image_h;  
  12.  
  13.   // Retrieve the handle to a display device context for the client area of the window.
  14.   if (!hdcScreen) hdcScreen = GetDC(NULL);
  15.  
  16.   // Create a compatible DC which is used in a BitBlt from the window DC
  17.   if (!hdcMemDC) hdcMemDC = CreateCompatibleDC(hdcScreen);
  18.  
  19.   if(!hdcMemDC)
  20.   {
  21.     printf("!hdcMemDC\n");
  22.     return;
  23.   }
  24.  
  25.   // Create a compatible bitmap from the Window DC
  26.   if (!hbmScreen) hbmScreen = CreateCompatibleBitmap(hdcScreen, cx, cy);
  27.  
  28.   if(!hbmScreen)
  29.   {
  30.     printf("!hbmScreen\n");
  31.     return;
  32.   }
  33.  
  34.   // Select the compatible bitmap into the compatible memory DC.
  35.   SelectObject(hdcMemDC,hbmScreen);
  36.  
  37.   // Bit block transfer into our compatible memory DC.
  38.   if(!BitBlt(hdcMemDC,
  39.     0,0,
  40.     cx, cy,
  41.     hdcScreen,
  42.     x,y,
  43.     SRCCOPY | CAPTUREBLT))
  44.   {
  45.     printf("BitBlt has failed\n");
  46.     return;
  47.   }
  48.  
  49.   // Get the BITMAP from the HBITMAP
  50.   GetObject(hbmScreen,sizeof(BITMAP),&bmpScreen);
  51.  
  52.   // BITMAPFILEHEADER   bmfHeader;    
  53.   BITMAPINFOHEADER   bi;
  54.  
  55.   bi.biSize = sizeof(BITMAPINFOHEADER);    
  56.   bi.biWidth = bmpScreen.bmWidth;    
  57.   bi.biHeight = bmpScreen.bmHeight;  
  58.   bi.biPlanes = 1;    
  59.   bi.biBitCount = 32;    
  60.   bi.biCompression = BI_RGB;    
  61.   bi.biSizeImage = 0;  
  62.   bi.biXPelsPerMeter = 0;    
  63.   bi.biYPelsPerMeter = 0;    
  64.   bi.biClrUsed = 0;    
  65.   bi.biClrImportant = 0;
  66.  
  67.   DWORD dwBmpSize = ((bmpScreen.bmWidth * bi.biBitCount + 31) / 32) * 4 * bmpScreen.bmHeight;
  68.  
  69.   // alokace vnitrniho bufferu - ten pak vnitrne pouzivame
  70.   if (!hDIB) hDIB = new unsigned char[dwBmpSize];
  71.  
  72.   // Zkopirovani do bufferu
  73.   GetDIBits(hdcScreen, hbmScreen, 0,
  74.     (UINT)bmpScreen.bmHeight,
  75.     hDIB,//lpbitmap,
  76.     (BITMAPINFO *)&bi, DIB_RGB_COLORS);
  77. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement