0

Is there a way to get the color of a pixel on the screen including the cursor with C#? Right now, this

//Gets color at location
public Color GetColorAt(Point location)
{
    using (Graphics gdest = Graphics.FromImage(screenPixel))
    {
        using (Graphics gsrc = Graphics.FromHwnd(IntPtr.Zero))
        {
            IntPtr hSrcDC = gsrc.GetHdc();
            IntPtr hDC = gdest.GetHdc();
            int retval = BitBlt(hDC, 0, 0, 1, 1, hSrcDC, location.X, location.Y, (int)CopyPixelOperation.SourceCopy);
            gdest.ReleaseHdc();
            gsrc.ReleaseHdc();
        }
    }

    return screenPixel.GetPixel(0, 0);
}

Will return the color of a pixel, but ignores the color of the cursor when doing so. So if I set the location to 500x500, pulled up a black picture, and then hovered the middle of my white mouse cursor at the 500x500 mark, the color returned will still be black and not white. Is there a way to get around this?

John
  • 326
  • 4
  • 21
  • 1
    The answer here will show you how to get screenshot including the cursor. Just look up the pixel at the coordinates it returns. https://stackoverflow.com/questions/6750056/how-to-capture-the-screen-and-mouse-pointer-using-windows-apis – Bradley Uffner Nov 18 '16 at 19:32
  • @BradleyUffner I'm trying to use this `try { Debug.WriteLine(CaptureScreen(true).GetPixel(Cursor.Position.X + 3, Cursor.Position.Y + 3)); } catch { }` but I'm getting an almost black color (my cursor is the standard windows white one). – John Nov 18 '16 at 19:52

0 Answers0