0

I need to know when my cursor is changing color in c#. I wanted to use code that takes cursor position and retrieves color of pixel under it. Sadly by offsetting position to 'hit' cursor i cannot get color of it. I used following code.

Bitmap screenPixel = new Bitmap(1, 1, PixelFormat.Format32bppArgb);
        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);
        }
sticky bit
  • 31,711
  • 12
  • 26
  • 38
  • 1
    Does this answer your question? [C# (WinForms) - Find Colour of Cursor](https://stackoverflow.com/questions/30551618/c-sharp-winforms-find-colour-of-cursor) – NetMage May 19 '20 at 23:08
  • maybe duplicate of https://stackoverflow.com/questions/1483928/how-to-read-the-color-of-a-screen-pixel – Abdurrahim May 19 '20 at 23:25
  • Native call `GetDC(NULL)`. _["If this value is NULL, GetDC retrieves the DC for the **entire screen**."](https://docs.microsoft.com/en-us/windows/win32/api/winuser/nf-winuser-getdc)_ – MickyD May 19 '20 at 23:47

0 Answers0