在 C++ 中获取像素颜色
我想获取屏幕上不同 x、y 坐标处像素的 RGB 值。 我将如何在 C++ 中解决这个问题?
我正在尝试创建自己的高斯模糊效果。
这将在 Windows 7 中进行。
编辑
需要包含哪些库才能运行?
我要做什么:
#include <iostream>
using namespace std ;
int main(){
HDC dc = GetDC(NULL);
COLORREF color = GetPixel(dc, 0, 0);
ReleaseDC(NULL, dc);
cout << color;
}
I would like to get the RGB values of a pixel at different x, y coordinates on the screen.
How would I go about this in C++?
I'm trying to create my own gaussian blur effect.
This would be in Windows 7.
Edit
What libraries need to be included for this to run?
What I have going:
#include <iostream>
using namespace std ;
int main(){
HDC dc = GetDC(NULL);
COLORREF color = GetPixel(dc, 0, 0);
ReleaseDC(NULL, dc);
cout << color;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以在
NULL
窗口上使用GetDC
来获取整个屏幕的设备上下文,然后可以调用GetPixel
:当然,在执行所有像素读取以提高效率时,您只想获取和释放设备上下文一次。
You can use
GetDC
on theNULL
window to get a device context for the whole screen, and can follow that up with a call toGetPixel
:Of course, you'd want to only acquire and release the device context once while doing all the pixel-reading for efficiency.
正如上一篇文章中提到的,您需要 GetPixel来自 Win32 API 的 函数。
GetPixel 位于 gdi32.dll 内部,因此如果您有正确的环境设置,您应该能够包含 windows.h(其中包括 wingdi.h),并且您应该是黄金。
如果出于某种原因您有一个最小的环境设置,您也可以直接在 gdi32.dll 上使用 LoadLibrary。
GetPixel 的第一个参数是设备上下文的句柄,可以通过调用 GetDC 函数(也可以通过
获得)来检索该句柄。从 dll 加载 GetPixel 并打印出当前光标位置处的像素颜色的基本示例如下。
As mentioned in a previous post, you want the GetPixel function from the Win32 API.
GetPixel sits inside gdi32.dll, so if you have a proper environment setup, you should be able to include windows.h (which includes wingdi.h) and you should be golden.
If you have a minimal environment setup for whatever reason, you could also use LoadLibrary on gdi32.dll directly.
The first parameter to GetPixel is a handle to the device context, which can be retrieved by calling the GetDC function(which is also available via
<windows.h>
).A basic example that loads GetPixel from the dll and prints out the color of the pixel at your current cursor position is as follows.