C# – How to get the colour of a pixel at X,Y using c#


How do I get the color of a pixel at X,Y using c#?

As for the result, I can convert the results to the color format I
need. I am sure there is an API call for this.

For any given X,Y on the monitor, I want to get the color of that

Best Solution

To get a pixel color from the Screen here's code from Pinvoke.net:

  using System;
  using System.Drawing;
  using System.Runtime.InteropServices;

  sealed class Win32
      static extern IntPtr GetDC(IntPtr hwnd);

      static extern Int32 ReleaseDC(IntPtr hwnd, IntPtr hdc);

      static extern uint GetPixel(IntPtr hdc, int nXPos, int nYPos);

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