Search code examples
cbitmappixelbitblt

Get pixel color at mouse position fastest way


So actually i got a very fast and nice code from "Vodemki" in here - Get Pixel color fastest way?

But only problem it scans the whole image while i need to scan only my current mouse position

Here's the code -

HDC hdc, hdcTemp;
RECT rect;
BYTE* bitPointer;
int x, y;
int red, green, blue, alpha;

while(true)
{
hdc = GetDC(HWND_DESKTOP);
GetWindowRect(hWND_Desktop, &rect);
        int MAX_WIDTH = rect.right;
    int MAX_HEIGHT = rect.bottom;

hdcTemp = CreateCompatibleDC(hdc);
BITMAPINFO bitmap;
bitmap.bmiHeader.biSize = sizeof(bitmap.bmiHeader);
bitmap.bmiHeader.biWidth = MAX_WIDTH;
bitmap.bmiHeader.biHeight = MAX_HEIGHT;
bitmap.bmiHeader.biPlanes = 1;
bitmap.bmiHeader.biBitCount = 32;
bitmap.bmiHeader.biCompression = BI_RGB;
bitmap.bmiHeader.biSizeImage = MAX_WIDTH * 4 * MAX_HEIGHT;
bitmap.bmiHeader.biClrUsed = 0;
bitmap.bmiHeader.biClrImportant = 0;
HBITMAP hBitmap2 = CreateDIBSection(hdcTemp, &bitmap, DIB_RGB_COLORS, (void**)(&bitPointer), NULL, NULL);
SelectObject(hdcTemp, hBitmap2);
BitBlt(hdcTemp, 0, 0, MAX_WIDTH, MAX_HEIGHT, hdc, 0, 0, SRCCOPY);

for (int i=0; i<(MAX_WIDTH * 4 * MAX_HEIGHT); i+=4)
{
    red = (int)bitPointer[i];
    green = (int)bitPointer[i+1];
    blue = (int)bitPointer[i+2];
    alpha = (int)bitPointer[i+3];

    x = i / (4 * MAX_HEIGHT);
    y = i / (4 * MAX_WIDTH);

    if (red == 255 && green == 0 && blue == 0)
    {
        SetCursorPos(x,y);
        mouse_event(MOUSEEVENTF_LEFTDOWN, 0, 0, 0, 0);
        Sleep(50);
        mouse_event(MOUSEEVENTF_LEFTUP, 0, 0, 0, 0);
        Sleep(25);
    }
}
}

I'm not familiar with BitBlt, So if anyone can help me modify that code i'll appreciate it

Thanks!


Solution

  • Alright I made it finally. I still got problem with the timing, makes my puter very slow... I'll work for it later...

    Here's the code -

    //Globals
    
    int sX, sY, x, y;
    
    BYTE* sData = 0;
    
    POINT cursorPos;
    
    HDC hScreen;
    HDC hdcMem;
    HBITMAP hBitmap;
    HGDIOBJ hOld;
    
    
    void PixelFunction();   // Get the pixel rgb function
    
    int main()
    {
        PixelFunction();
    
        ReleaseDC(NULL, hScreen);
        DeleteDC(hdcMem);
    
        return 0;
    }
    
    
    void PixelFunction()
    {
        int Red, Green, Blue;
    
        hScreen = GetDC(NULL);
    
        sX = GetDeviceCaps(hScreen, HORZRES);
        sY = GetDeviceCaps(hScreen, VERTRES);
    
        hdcMem = CreateCompatibleDC (hScreen);
        hBitmap = CreateCompatibleBitmap(hScreen, sX, sY);
    
        BITMAPINFOHEADER bm = {0};
        bm.biSize = sizeof(BITMAPINFOHEADER);
        bm.biPlanes = 1;
        bm.biBitCount = 32;
        bm.biWidth = sX;
        bm.biHeight = -sY;
        bm.biCompression = BI_RGB;
        bm.biSizeImage = 0; // 3 * sX * sY;
    
        while (1) {
    
            hOld = SelectObject(hdcMem, hBitmap);
            BitBlt(hdcMem, 0, 0, sX, sY, hScreen, 0, 0, SRCCOPY);
            SelectObject(hdcMem, hOld);
    
            free(sData);
            sData = (BYTE*)malloc(4 * sX * sY);
    
            GetDIBits(hdcMem, hBitmap, 0, sY, sData, (BITMAPINFO*)&bm, DIB_RGB_COLORS);
    
            GetCursorPos(&cursorPos);
            x = cursorPos.x;
            y = cursorPos.y;
    
            Red = sData[4 * ( (y * sX) + x) +2];
            Green = sData[4 * ( ( y * sX) + x) +1];
            Blue = sData[4 * ( (y * sX) + x)];
    
            // Check for color
            if (Red == 255 && Green == 0 && Blue == 0) {
                mouse_event(MOUSEEVENTF_LEFTDOWN, 0, 0, 0, 0);
                Sleep(5);
                mouse_event(MOUSEEVENTF_LEFTUP, 0, 0, 0, 0);
            }
            Sleep(10);
        }
    }
    

    Hope it helped!