简体   繁体   中英

How to read a range of pixels c++

I'm doing a program that reads a range of pixels and if find the red color, do a click with left mouse button, but it is too slow, like if the red dot shows up on the range, it takes 2 seconds or more to click.

This is a representation of the problem:

在此处输入图片说明

So if some red dot cross the green line, click with left mouse button. The green line do not exist, I drew it just to make it clearer.

Can someone propose a solution to read this interval in milliseconds? I think that the problem is to use a for loop to check the pixel colors.

I tried to use the solution proposed in the topic "Get Pixel color fastest way?" but this made the computer very slow and did not work. (I made some adjustments to adapt the solution to my problem). Also, because my problem is much simpler, I think there is a simpler solution than the one in the other topic, which is more complex and did not work for me.

This is my code:

int main()
{
    HDC dc = GetDC(NULL);
    POINT pt;

    int x = 150;

    while (1) {
        for (int y = 205; y >= 0; y--) {
            dc = GetDC(NULL);
            COLORREF color = GetPixel(dc, x, y);

            // the red color has value = 220
            if (color == 220) {
                GetCursorPos(&pt);
                mouse_event(MOUSEEVENTF_LEFTDOWN, pt.x, pt.y, 0, 0);
                mouse_event(MOUSEEVENTF_LEFTUP, pt.x, pt.y, 0, 0);
            }
        }
    }
    ReleaseDC(NULL, dc);
}

Should you be getting a new image every time you're checking the pixel color? Off the top of my head maybe GETDC should be before the for loop.

while (1) {
        dc = GetDC(NULL);
        for (int y = 205; y >= 0; y--) {

            COLORREF color = GetPixel(dc, x, y);

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