如何读取屏幕像素的颜色
好的,我正在寻找一个功能或其他功能,可以读取显示器上某个像素的颜色,当检测到该颜色时,将启用另一个功能。我认为使用RGB。感谢所有帮助。谢谢。
Okay, I am looking for a function or something that will read the color of a certain pixel on my monitor, and when that color is detected, another function will be enabled. I figure using RGB. All help appreciated. Thank You.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
这里的大多数答案都使用该像素的相同来源(桌面直流电)。
关键函数是
GetPixel
。我认为这是最干净、最快的方法。
注意:
如果您修改了 Windows 上“显示设置”中的默认文本大小以提高高分辨率显示屏上的可读性,则需要以同样的方式调整 GetPixel() 的坐标参数。例如,在Windows 7上,如果光标位置为(x,y),文本大小的150%,则需要调用GetPixel(x*1.5, y*1.5)来获取光标下像素的颜色。
Most answers here use the very same source of that pixel (desktop dc).
The key function is
GetPixel
.I think this is the cleanest and quickest way.
Note:
If you have modified the default text size among the Display Settings on Windows to increase readability on a high resolution display, the coordinate parameters of GetPixel() need to be adjusted the same way. For example, if the cursor location is (x,y) with 150% of text size on Windows 7, you need to call GetPixel(x*1.5, y*1.5) to get the color of the pixel under the cursor.
此函数较短,并且可以使用 System.Drawing 实现相同的结果,而无需 Pinvoke。
This function is shorter and can achieve the same result using
System.Drawing
, without Pinvoke.请检查我在之前的一个项目中使用的两个不同的函数:
1) 该函数拍摄桌面快照
2) 该函数在输入中获取图像并计算给定像素范围的 RGB 平均值。
这两个功能结合在一起可能会解决您的问题。快乐编码:)
编辑:请注意,GetPixel 是非常慢的函数。在使用它之前我会三思而后行。
Please check this two different functions I have used in one of my previous projects :
1) This function takes snapshot of Desktop
2) This function takes an image in input and calculates RGB average of pixel range given.
This two functions together might solve your problem. Happy Coding :)
EDIT : Please note that GetPixel is very slow function. I will think twice befor using it.
据我所知,最简单的方法是:
编辑
可能没有办法“等待”直到像素更改为某种颜色。您的程序可能必须经常循环并检查它,直到看到颜色。
例如:
EDIT 2
以下是一些您可以修改的示例代码。此代码只是根据给定像素中的当前颜色更改标签的颜色。这段代码避免了提到的句柄泄漏。
}
As far as I know the easiest way to do this is to:
Edit
There is probably no way to "wait" until the pixel changes to a certain color. Your program will probably have to just loop and check it every so often until it sees the color.
For example:
EDIT 2
Here is some sample code you can modify. This code just changes the color of a label based on the current color in a given pixel. This code avoids the handle leak mentioned.
}
这条线大约用了10毫秒。
This line uses About 10 ms.
这是最有效的:它抓取光标位置处的像素,并且不依赖于只有一个显示器。
现在,显然,您不必使用光标的当前位置,但这是一般的想法。
编辑:
给定上面的
GetColorAt
函数,您可以以安全、性能友好的方式轮询屏幕上的某个像素,如下所示:如果需要,您可以将其包装在线程中,或从控制台应用程序执行它。 “无论你喜欢什么,”我想。
This is the most efficient: It grabs a pixel at the location of the cursor, and doesn't rely on only having one monitor.
Now, obviously, you don't have to use the cursor's current location, but this is the general idea.
EDIT:
Given the above
GetColorAt
function you can poll a certain pixel on the screen in a safe, performance friendly way like this:You can wrap that in a Thread if you want, or execute it from a Console application. "Whatever suits your fancy," I guess.