如何使用C#获得X,Y处像素的颜色? [英] How to get the colour of a pixel at X,Y using c#?

查看:54
本文介绍了如何使用C#获得X,Y处像素的颜色?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

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

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.

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

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

推荐答案

要从屏幕获取像素颜色,这是

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

这篇关于如何使用C#获得X,Y处像素的颜色?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆