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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/753132/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-04 23:50:16  来源:igfitidea点击:

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

c#api

提问by MichaelICE

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

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

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.

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

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

对于显示器上任何给定的 X、Y,我想获得该像素的颜色。

采纳答案by CLaRGe

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

要从屏幕获取像素颜色,请使用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;
      }
   }

回答by Jon Skeet

There's Bitmap.GetPixelfor an image... is that what you're after? If not, could you say which x, y value you mean? On a control?

Bitmap.GetPixel一个图像......这就是你所追求的吗?如果不是,你能说出你的意思是哪个 x, y 值吗?在控件上?

Note that if you domean for an image, and you want to get lotsof pixels, and you don't mind working with unsafe code, then Bitmap.LockBitswill be a lot faster than lots of calls to GetPixel.

请注意,如果您确实对图像有意义,并且想要获得大量像素,并且不介意使用不安全的代码,那么Bitmap.LockBits将比对GetPixel.

回答by Simon Broadhead

Aside from the P/Invoke solution, you can use Graphics.CopyFromScreen to get the image data from the screen into a Graphics object. If you aren't worried about portability, however, I would recommend the P/Invoke solution.

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

回答by Eric Ouellet

For ref in WPF: (usage of PointToScreen)

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

回答by Cully Lineberger

if anyone is having trouble with this and none of the solutions are working, as how it was for me, and you are in WPF, here is how I got mine working :-).

如果有人遇到这个问题并且没有一个解决方案有效,就像我的情况一样,而你在 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);
        }