C# Get pixel color from another window always return black or white

0

I spent a lot of time, trying to fix this problem, but right now i've got no idea, what might be wrong. The code I have is:

    class ScreenCheckerClass
{
    [DllImport("user32.dll")]
    static extern IntPtr GetDC(IntPtr hwnd);

    [DllImport("user32.dll")]
    static extern Int32 ReleaseDC(IntPtr hwnd, IntPtr hdc);

    [DllImport("gdi32.dll")]
    static extern uint GetPixel(IntPtr hdc, int nXPos, int nYPos);

    static public System.Drawing.Color GetPixelColor(IntPtr dhwnd, int x, int y)
    {
        IntPtr hdc = GetDC(dhwnd);
        uint pixel = GetPixel(hdc, x, y);
        ReleaseDC(dhwnd, hdc);
        Color color = Color.FromArgb((int)(pixel & 0x000000FF),
                     (int)(pixel & 0x0000FF00) >> 8,
                     (int)(pixel & 0x00FF0000) >> 16);
        return color;
    }
}

The problem is: It's always return black or white color in RGB. It works when I change to dhwnd = IntPtr.Zero, but I need it, to work "background", not even on totally minimalized window.

c#
asked on Stack Overflow Aug 16, 2017 by Mateusz

0 Answers

Nobody has answered this question yet.


User contributions licensed under CC BY-SA 3.0