Getting the colour of a pixel at specified coordinate on the screen

Viewed 66

I need to get the colour of a pixel in the specified location. After some research this is what I have.

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

int main()
{

    HDC dc = GetDC(NULL);
    COLORREF color = GetPixel(dc, 100, 100);
    ReleaseDC(NULL, dc);

    std::cout << GetRValue(color);
    std::cout << GetGValue(color);
    std::cout << GetBValue(color);

    return 0;
}

I don't get any error but there are three weird characters displayed(which I cannot seem to copy and paste here). What can I do to get the pixel colour successfully?

1 Answers

This is just a matter of the way you are printing the values. Try this instead

std::cout << static_cast<unsigned>(GetRValue(color)) << '\n';

GetRValue(color) etc returns a byte value, which cout << is interpreting as a character. By casting to unsigned it forces cout << to print the integer value instead.

Related