简体   繁体   中英

GetPixel is WAY too slow

I have a bunch of squares and each has a specific identity/symbol I need to identify. So far I have something like:

#include <iostream>
#include <windows.h>

using namespace std;

int main() {
    HDC dc = GetDC(0);
    COLORREF color;
    int sum, x, y;

    while (true) {
        sum = 0;
        Sleep(100);
        for (x = 512; x < 521; x++) {
            for (y = 550; y < 565; y++) {
                color = GetPixel(dc, x, y);
                sum = GetRValue(color) + GetBValue(color) + GetGValue(color);
            }
        }
        cout << "SUM: " << sum << endl;
    }

    return 0;
}

Obviously it only scans one block so far. The problem is somehow even though it's only just over 100 pixels, it takes an INSANELY long time. I can't even imagine what could be going on. It takes well over a second, maybe two seconds, for each repetition. What can I do? There has to be a faster way to do this. If I can't query individual pixels, would there be a way to get a region of the screen? The zone is not inside my program's window.

Jonathan comments in the question to use DIB, but there's no answer showing how. For the sake of completeness, here's Mister B's code :

COLORREF getcolor(POINT pt) {
    HDC hDc = GetDC(0);
    HDC hDcmem = CreateCompatibleDC(0);
    HBITMAP hBmp = CreateCompatibleBitmap(hDc, 1, 1);
    SelectObject(hDcmem, hBmp);
    BitBlt(hDcmem, 0, 0, 1, 1, hDc, pt.x, pt.y, SRCCOPY);
    LPBITMAPINFO lpbmi = new BITMAPINFO;
    lpbmi->bmiHeader.biBitCount = 24;
    lpbmi->bmiHeader.biCompression = BI_RGB;
    lpbmi->bmiHeader.biPlanes = 1;
    lpbmi->bmiHeader.biHeight = 1;
    lpbmi->bmiHeader.biWidth = 1;
    lpbmi->bmiHeader.biSize = sizeof(BITMAPINFO);
    BYTE lpvBits[4];
    GetDIBits(hDcmem, hBmp, 0, 1, lpvBits, lpbmi, DIB_RGB_COLORS);
    COLORREF currColor = RGB(lpvBits[2], lpvBits[1], lpvBits[0]);
    delete lpbmi;
    DeleteObject(hBmp);
    DeleteDC(hDcmem);
    ReleaseDC(0, hDc);
    return currColor;
}

This is still very slow as Ben points out in a comment, and the solution is to use these building blocks to copy the entire area at once. Your code would become something like this:

#include <iostream>
#include <vector>
#include <windows.h>

int main() {
    int const startX = 512;
    int const endX = 521;
    int const startX = 550;
    int const endY = 565;
    int const width = endX - startX;
    int const height = endY - startY;

    HDC const hDc = GetDC(0);
    HDC const hDcmem = CreateCompatibleDC(0);
    HBITMAP const hBmp = CreateCompatibleBitmap(hDc, width, height);
    auto const oldBmp = SelectObject(hDcmem, hBmp);

    BITMAPINFO bmi{};
    bmi.bmiHeader.biBitCount = 32;
    bmi.bmiHeader.biCompression = BI_RGB;
    bmi.bmiHeader.biPlanes = 1;
    bmi.bmiHeader.biHeight = height;
    bmi.bmiHeader.biWidth = width;
    bmi.bmiHeader.biSize = sizeof(BITMAPINFO);

    std::vector<RGBQUAD> pixels(height * width);

    while (true) {
        Sleep(100);

        BitBlt(hDcmem, 0, 0, width, height, hDc, startX, startY, SRCCOPY);
        GetDIBits(hDcmem, hBmp, 0, height, &pixels[0], &bmi, DIB_RGB_COLORS);
        int sum = 0;
        for (int i = 0; i < height * width; ++i) {
            sum = pixels[i].R + pixels[i].G + pixels[i].B;
        }

        std::cout << "SUM: " << sum << std::endl;
    }

    SelectObject(hDcmem, oldBmp);
    DeleteObject(hBmp);
    DeleteDC(hDcmem);
    ReleaseDC(0, hDc);

    return 0;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM