如何在C#中检测显示器上像素的颜色?

时间:2010-11-17 11:54:13

标签: c# colors

我需要检测显示器上像素的颜色。如何在C#中的坐标(x,y)上检索它?

4 个答案:

答案 0 :(得分:5)

使用Graphics.CopyFromScreen复制1x1位图Bitmap.GetPixel()以获取其颜色。

答案 1 :(得分:2)

首先导入这些Dll

    [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);

然后编写此方法GetPixelColor(x,y);

      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;
      }

调用方法Color clr = GetPixelcolor(50,50);

答案 2 :(得分:1)

首先,捕捉屏幕。

Rectangle screenRegion = Screen.AllScreens[0].Bounds;
Bitmap screen = new Bitmap(screenRegion.Width, screenRegion.Height, PixelFormat.Format32bppArgb);

Graphics screenGraphics = Graphics.FromImage(screenBitmap);
screenGraphics.CopyFromScreen(screenRegion.Left, screenRegion.Top, 0, 0, screenRegion.Size);

然后,来自位图的get the pixel

答案 3 :(得分:0)

您可以使用winapi GetPixel(...)