Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <windows.h>
- #include <iostream>
- #define Winver 0x500
- #include <tlhelp32.h>
- #include <stdio.h>
- #include <winable.h>
- #include <fstream>
- #include <istream>
- #include <string>
- using namespace std;
- int ScreenWidth;
- int ScreenHeight;
- void get_bitmap_ava(HWND ava_hwnd, int size_x, int size_y, bool get_pixel_color = false)
- {
- int pos_x, pos_y; //position of the mouse
- POINT p;
- GetCursorPos(&p); //get the current mouse position
- pos_x = p.x; //set pos_x to the mouse x coordinate
- pos_y = p.y; //set pos_y to the mouse y coordinate
- int radius = 25; //size of the box to record
- HDC ava_dc = GetDC(ava_hwnd); //get the device context of the window handle
- HDC ava_dc_cap = CreateCompatibleDC(ava_dc); //create a compatible DC for BitBlt to use
- HBITMAP hCaptureBitmap = CreateCompatibleBitmap(ava_dc, size_x, size_y); //create a bitmap of the DC
- SelectObject(ava_dc_cap, hCaptureBitmap); //select the bitmap/ dc in memory
- BitBlt(ava_dc_cap,0,0,size_x,size_y, ava_dc, 0, 0, SRCCOPY); //call BitBlt on the object
- //getting the size of the picture
- BITMAP bm;
- GetObject(hCaptureBitmap, sizeof(bm), &bm); //get the object from memory and place it in bm
- int width(bm.bmWidth), height(bm.bmHeight); //set the width and height of the bitmap for the header info
- //creating a bitmapheader for getting the dibits
- BITMAPINFOHEADER bminfoheader;
- ::ZeroMemory(&bminfoheader, sizeof(BITMAPINFOHEADER));
- bminfoheader.biSize = sizeof(BITMAPINFOHEADER); //set the size of the header
- bminfoheader.biWidth = width; //set the width
- bminfoheader.biHeight = -height; //and height
- bminfoheader.biPlanes = 1; //must be set to 1
- bminfoheader.biBitCount = 32; //32 bits per pixel
- bminfoheader.biCompression = BI_RGB; //easiest way to get the RGB of a pixel
- bminfoheader.biSizeImage = width * 4 * height; //set the size of the image
- bminfoheader.biClrUsed = 0; //require all colors
- bminfoheader.biClrImportant = 0; //reference http://msdn.microsoft.com/en-us/library/windows/desktop/dd183376(v=vs.85).aspx for a better explanation
- //create a buffer and let the GetDIBits fill in the buffer
- unsigned char* pPixels = new unsigned char[(width * 4 * height)];
- if( !GetDIBits(CreateCompatibleDC(0), hCaptureBitmap, 0, height, pPixels, (BITMAPINFO*) &bminfoheader, DIB_RGB_COLORS)) // load pixel info
- {
- //return if fails but first delete the resources
- DeleteObject(hCaptureBitmap);
- delete [] pPixels; // delete the array of objects
- cout << "fail" << endl;
- return;
- }
- int x, y; // fill the x and y coordinate for grabbing pixels
- x = 0;
- y = 0;
- for(int iter = 0; iter < (size_x * size_y); iter++){
- if(x < size_x){ //go from left to right across the screen
- x++;
- }
- else{
- x = 0;
- y++; //and top to bottom
- }
- unsigned int r = pPixels[(width*y+x) * 4 + 2]; //get the pixel colors
- unsigned int g = pPixels[(width*y+x) * 4 + 1];
- unsigned int b = pPixels[(width*y+x) * 4 + 0];
- if(get_pixel_color){ //for grabbing one area
- if(x - pos_x < radius && x - pos_x > -1*radius && y - pos_y < radius && y - pos_y > -1*radius ){ //creates a box
- cout << "RED: " << r << " GREEN: " << g << " BLUE: " << b << "\n"; //print the values
- }
- }
- }
- //clean up the bitmap and buffer unless you still need it
- DeleteObject(hCaptureBitmap);
- delete [] pPixels; // delete the array of objects
- ReleaseDC(ava_hwnd, ava_dc);
- DeleteDC(ava_dc_cap);
- DeleteObject(hCaptureBitmap);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement