繁体   English   中英

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

[英]How to detect the color of a pixel on the monitor in C#?

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

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

首先导入这些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);

首先,捕获屏幕。

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

然后,从位图中获取像素

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM