How to read the Color of a Screen Pixel

Viewed 107022

Okay, I am looking for a function or something that will read the color of a certain pixel on my monitor, and when that color is detected, another function will be enabled. I figure using RGB. All help appreciated. Thank You.

6 Answers

This function is shorter and can achieve the same result using System.Drawing, without Pinvoke.

Bitmap bmp = new Bitmap(1, 1);
Color GetColorAt(int x, int y)
{
    Rectangle bounds = new Rectangle(x, y, 1, 1);
    using (Graphics g = Graphics.FromImage(bmp))
        g.CopyFromScreen(bounds.Location, Point.Empty, bounds.Size);
    return bmp.GetPixel(0, 0);
}

This line uses About 10 ms.

int retval = BitBlt(hDC, 0, 0, 1, 1, hSrcDC, location.X, location.Y, (int)CopyPixelOperation.SourceCopy);
Related