Advertisement
asqapro

ScreenRead

Oct 15th, 2013
149
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.70 KB | None | 0 0
  1. #include <windows.h>
  2. #include <iostream>
  3. #define Winver 0x500
  4. #include <tlhelp32.h>
  5. #include <stdio.h>
  6. #include <winable.h>
  7. #include <fstream>
  8. #include <istream>
  9. #include <string>
  10.  
  11. using namespace std;
  12.  
  13. int ScreenWidth;
  14. int ScreenHeight;
  15.  
  16. void get_bitmap_ava(HWND ava_hwnd, int size_x, int size_y, bool get_pixel_color = false)
  17. {
  18.     int pos_x, pos_y; //position of the mouse
  19.     POINT p;
  20.     GetCursorPos(&p); //get the current mouse position
  21.     pos_x = p.x; //set pos_x to the mouse x coordinate
  22.     pos_y = p.y; //set pos_y to the mouse y coordinate
  23.  
  24.     int radius = 25; //size of the box to record
  25.  
  26.     HDC ava_dc = GetDC(ava_hwnd); //get the device context of the window handle
  27.     HDC ava_dc_cap = CreateCompatibleDC(ava_dc); //create a compatible DC for BitBlt to use
  28.     HBITMAP hCaptureBitmap = CreateCompatibleBitmap(ava_dc, size_x, size_y); //create a bitmap of the DC
  29.     SelectObject(ava_dc_cap, hCaptureBitmap); //select the bitmap/ dc in memory
  30.     BitBlt(ava_dc_cap,0,0,size_x,size_y, ava_dc, 0, 0, SRCCOPY); //call BitBlt on the object
  31.  
  32.     //getting the size of the picture
  33.     BITMAP bm;
  34.     GetObject(hCaptureBitmap, sizeof(bm), &bm); //get the object from memory and place it in bm
  35.     int width(bm.bmWidth), height(bm.bmHeight); //set the width and height of the bitmap for the header info
  36.  
  37.     //creating a bitmapheader for getting the dibits
  38.     BITMAPINFOHEADER bminfoheader;
  39.     ::ZeroMemory(&bminfoheader, sizeof(BITMAPINFOHEADER));
  40.     bminfoheader.biSize        = sizeof(BITMAPINFOHEADER); //set the size of the header
  41.     bminfoheader.biWidth       = width; //set the width
  42.     bminfoheader.biHeight      = -height; //and height
  43.     bminfoheader.biPlanes      = 1; //must be set to 1
  44.     bminfoheader.biBitCount    = 32; //32 bits per pixel
  45.     bminfoheader.biCompression = BI_RGB; //easiest way to get the RGB of a pixel
  46.  
  47.     bminfoheader.biSizeImage = width * 4 * height; //set the size of the image
  48.     bminfoheader.biClrUsed = 0; //require all colors
  49.     bminfoheader.biClrImportant = 0; //reference http://msdn.microsoft.com/en-us/library/windows/desktop/dd183376(v=vs.85).aspx for a better explanation
  50.  
  51.  
  52.     //create a buffer and let the GetDIBits fill in the buffer
  53.     unsigned char* pPixels = new unsigned char[(width * 4 * height)];
  54.     if( !GetDIBits(CreateCompatibleDC(0), hCaptureBitmap, 0, height, pPixels, (BITMAPINFO*) &bminfoheader, DIB_RGB_COLORS)) // load pixel info
  55.     {
  56.         //return if fails but first delete the resources
  57.         DeleteObject(hCaptureBitmap);
  58.         delete [] pPixels; // delete the array of objects
  59.         cout << "fail" << endl;
  60.  
  61.         return;
  62.     }
  63.  
  64.     int x, y; // fill the x and y coordinate for grabbing pixels
  65.     x = 0;
  66.     y = 0;
  67.  
  68.     for(int iter = 0; iter < (size_x * size_y); iter++){
  69.         if(x < size_x){ //go from left to right across the screen
  70.             x++;
  71.         }
  72.         else{
  73.             x = 0;
  74.             y++; //and top to bottom
  75.         }
  76.         unsigned int r = pPixels[(width*y+x) * 4 + 2]; //get the pixel colors
  77.         unsigned int g = pPixels[(width*y+x) * 4 + 1];
  78.         unsigned int b = pPixels[(width*y+x) * 4 + 0];
  79.         if(get_pixel_color){ //for grabbing one area
  80.             if(x - pos_x < radius && x - pos_x > -1*radius && y - pos_y < radius && y - pos_y > -1*radius ){ //creates a box
  81.                 cout << "RED: " << r << "   GREEN: " << g << "   BLUE: " << b << "\n"; //print the values
  82.             }
  83.         }
  84.     }
  85.     //clean up the bitmap and buffer unless you still need it
  86.     DeleteObject(hCaptureBitmap);
  87.     delete [] pPixels; // delete the array of objects
  88.  
  89.     ReleaseDC(ava_hwnd, ava_dc);
  90.     DeleteDC(ava_dc_cap);
  91.     DeleteObject(hCaptureBitmap);
  92. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement