I'm having trouble with getpixel function.

Sometimes it returns the correct pixel ref and others it returns "4294967295" which I think is "CLR_INVALID"

COLORREF color;
HDC hdc_ = GetDC(NULL); //get dc of whole screen

color = GetPixel(hdc_, 10, 10);

std::cout << color << endl;

That is the simple code, any idea why this might happen?
Any thoughts appreciated.

It's been a while since I worked with the Win32 API. But if memory serves me correctly, you have to do the following:

  HDC hdc = GetDC(NULL);
             DWORD color = GetPixel(hdc, x, y);
             unsigned int r = GetRValue(color);
             unsigned int g = GetGValue(color);
             unsigned int b = GetBValue(color);
    cout << "red: " << r << endl;
    cout << "green: " << g << endl;
    cout << "blue: " << b << endl;

If I do that, (it's not what I need) it sometimes returns the rgb colors and sometimes just returns 255 for them all which is incorrect.

It's rather strange.

If the color is white, it will sometimes give "16777215" (correct) and sometimes give "4294967295" (incorrect)

Alpha channel perhaps?

The COLORREF is a typedef of DWORD, which is a typedef of unsigned long, but you'd use a COLORREF variable so others reviewing your code will know what it is.

The order of the bytes from left to right is reserved, blue, green, red. The high, reserved byte is usually set to 0, although when you use special Windows color palettes such as those found in 256 color mode, you may put something else into the reserved byte, such as a flag indicating that the remaining three bytes contain an index into a special Windows palette object.

The code below indicates that both values (16777215 and 4294967295) give the RGB values for a totally white pixel but the "incorrect" value of 4294967295 has the reserved byte set to 255.

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

int main(void)
{
    COLORREF value = 4294967295;

    int iA = (value >> 24) & 0xff;
    int iR = (value >> 16) & 0xff;
    int iG = (value >> 8) & 0xff;
    int iB = (value) & 0xff;

    std::cout << "4294967295 =  "<< iA << "  " << iR << " " << iB << "  " << iG <<std::endl;

    value = 16777215;

    iA = (value >> 24) & 0xff;
    iR = (value >> 16) & 0xff;
    iG = (value >> 8) & 0xff;
    iB = (value) & 0xff;

    std::cout << "16777215 =  "<< iA << "  " << iR << " " << iB << "  " << iG <<std::endl;

    return 0;
}

Output:
4294967295 = 255 255 255 255
16777215 = 0 255 255 255

I think I understand that, and thank you all for replies.

I still cannot understand though how to fix my problem

"4294967295" is just randomly returned no matter what the real color is.

How can I eliminate the "high, reserved byte" or ensure it is set to 0?

How can I eliminate the "high, reserved byte" or ensure it is set to 0?

color = GetPixel(hdc_, 10, 10) & 0xFFFFFF;

That seems to have solved the problem.
I thank you very kindly.

Like stated above it returns bgr or gbr rather than rgb, but I can live with that.

Thanks again to all.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.