繁体   English   中英

如何使用C#获取X,Y处像素的颜色?

[英]How to get the colour of a pixel at X,Y using c#?

如何使用C#获得X,Y处像素的颜色?

至于结果,我可以将结果转换为所需的颜色格式。 我敢肯定有一个API调用。

对于监视器上给定的任何X,Y,我想获取该像素的颜色。

要从屏幕上获取像素颜色,请参见Pinvoke.net的以下代码:

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

  sealed class Win32
  {
      [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);

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

Bitmap.GetPixel用于图像的Bitmap.GetPixel ...这就是您所追求的吗? 如果不是,您能否说出您指的是x,y值? 在控制上?

请注意,如果您确实要表示图像,并且想要获取很多像素,并且不介意使用不安全的代码,则Bitmap.LockBits将比许多GetPixel调用要快得多。

对于WPF中的引用:(PointToScreen的用法)

    System.Windows.Point position = Mouse.GetPosition(lightningChartUltimate1);

    if (lightningChartUltimate1.ViewXY.IsMouseOverGraphArea((int)position.X, (int)position.Y))
    {
        System.Windows.Point positionScreen = lightningChartUltimate1.PointToScreen(position);
        Color color = WindowHelper.GetPixelColor((int)positionScreen.X, (int)positionScreen.Y);
        Debug.Print(color.ToString());


      ...

      ...


public class WindowHelper
{
    // ******************************************************************
    [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);

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

除了P / Invoke解决方案之外,您还可以使用Graphics.CopyFromScreen将图像数据从屏幕获取到Graphics对象。 但是,如果您不担心可移植性,我建议您使用P / Invoke解决方案。

如果有人对此有麻烦,并且没有任何解决方案在起作用,就像对我来说是这样,而您在WPF中,那么这就是我的工作方式:-)。

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

        public static void getColor()
        {
            IntPtr hdc = GetDC(IntPtr.Zero);
            uint pixel = GetPixel(hdc, Cursor.Position.X, Cursor.Position.Y);
            ReleaseDC(IntPtr.Zero, hdc);
            System.Drawing.Color color = System.Drawing.Color.FromArgb((int)pixel);
            Console.WriteLine("Color is {0}", color);
        }

暂无
暂无

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

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