g0mb4

takeScreenShot

Jul 15th, 2014
709
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.56 KB | None | 0 0
  1. unsigned int takeScreenShot(int startx, int starty,int endx,int endy, int skipPx, bool showPic){
  2.  
  3.         unsigned int ret = 0;
  4.  
  5.         HWND hDesktopWnd = NULL;
  6.         HDC hDesktopDC = NULL;
  7.         HDC hCaptureDC = NULL;
  8.         HBITMAP hCaptureBitmap = NULL;
  9.  
  10.         hDesktopWnd = GetDesktopWindow();
  11.         hDesktopDC = GetDC(hDesktopWnd);
  12.  
  13.         if (!hDesktopDC) {
  14.                 //MessageBox( NULL, L"no hdc screen!", L"Error", MB_OK|MB_ICONERROR);
  15.                 ret |= (unsigned char) 1;
  16.                 return ret;
  17.         }
  18.  
  19.         hCaptureDC = CreateCompatibleDC(hDesktopDC);
  20.  
  21.         if (!hCaptureDC) {
  22.                 //MessageBox( NULL, L"no compatible dc screen!", L"Error", MB_OK|MB_ICONERROR);
  23.                 ret |= (unsigned char) 2;
  24.                 return ret;
  25.         }
  26.  
  27.         hCaptureBitmap = CreateCompatibleBitmap(hDesktopDC, (endx - startx), (endy - starty));
  28.  
  29.         if (!hCaptureBitmap) {
  30.                 //MessageBox( NULL, L"no bitmap!", L"Error", MB_OK|MB_ICONERROR);
  31.                 ret |= (unsigned char) 3;
  32.                 return ret;
  33.         }
  34.  
  35.         SelectObject(hCaptureDC, hCaptureBitmap);
  36.  
  37.         if(!BitBlt(hCaptureDC, 0, 0, (endx - startx), (endy - starty), hDesktopDC, startx, starty, SRCCOPY | CAPTUREBLT)){
  38.                 //MessageBox( NULL, L"BitBlt() failed", L"Error", MB_OK|MB_ICONERROR);
  39.                 ret |= (unsigned char) 4;
  40.                 return ret;
  41.         }
  42.  
  43.         BITMAPFILEHEADER bmfHeader;
  44.         BITMAPINFOHEADER bi;
  45.  
  46.         DWORD dwBmpSize = (endx - startx) * 3 * (endy - starty);
  47.  
  48.         bi.biSize = sizeof(BITMAPINFOHEADER);
  49.         bi.biWidth = (endx - startx);
  50.         bi.biHeight = (endy - starty);
  51.         bi.biPlanes = 1;
  52.         bi.biBitCount = 24;
  53.         bi.biCompression = BI_RGB;
  54.         bi.biSizeImage = 0;
  55.         bi.biXPelsPerMeter = 0;
  56.         bi.biYPelsPerMeter = 0;
  57.         bi.biClrUsed = 0;
  58.         bi.biClrImportant = 0;
  59.  
  60.         char *lpbitmap = new char[dwBmpSize];
  61.  
  62.         GetDIBits(hCaptureDC, hCaptureBitmap, 0, (UINT)(endy - starty), lpbitmap, (BITMAPINFO *)&bi, DIB_RGB_COLORS);
  63.  
  64.         unsigned char R = colAvr((unsigned char*)lpbitmap, COLOR_R, dwBmpSize, skipPx);
  65.         unsigned char G = colAvr((unsigned char*)lpbitmap, COLOR_G, dwBmpSize, skipPx);
  66.         unsigned char B = colAvr((unsigned char*)lpbitmap, COLOR_B, dwBmpSize, skipPx);
  67.  
  68.         ret = (R << 24);
  69.         ret |= (G << 16);
  70.         ret |= (B << 8);
  71.         ret |= (unsigned char) 0;
  72.  
  73.         if(showPic){
  74.  
  75.                 FILE *fp;
  76.                 errno_t err;
  77.  
  78.                 if((err = fopen_s(&fp, "c:\\cica.bmp", "wb")) != NULL){
  79.                         ret |= (unsigned char) 5;
  80.                         return ret;
  81.                 }
  82.  
  83.                 DWORD dwSizeofDIB = dwBmpSize + sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER);
  84.                 bmfHeader.bfOffBits = (DWORD)sizeof(BITMAPFILEHEADER) + (DWORD)sizeof(BITMAPINFOHEADER);
  85.                 bmfHeader.bfSize = dwSizeofDIB;
  86.                 bmfHeader.bfType = 0x4D42; //BM
  87.  
  88.                 fwrite((unsigned char*)&bmfHeader, sizeof(unsigned char), sizeof(BITMAPFILEHEADER), fp);
  89.                 fwrite((unsigned char*)&bi, sizeof(unsigned char), sizeof(BITMAPINFOHEADER), fp);
  90.                 fwrite((unsigned char*)lpbitmap, sizeof(unsigned char), dwBmpSize, fp);
  91.  
  92.                 fclose(fp);
  93.         }
  94.  
  95.         delete lpbitmap;
  96.  
  97.         ReleaseDC(hDesktopWnd,hDesktopDC);
  98.         DeleteDC(hCaptureDC);
  99.         DeleteObject(hCaptureBitmap);
  100.  
  101.         return ret;
  102. }
Advertisement
Add Comment
Please, Sign In to add comment