问题描述
我的操作系统是 64 位 Windows 7,带有 2 个显示器.
My OS is windows 7 64-bits with 2 monitors display.
我使用 GetPixel()
,但它总是返回 CLR_INVALID
作为这样的结果:
I use GetPixel()
, but it always return CLR_INVALID
as result like that:
COLORREF result = GetPixel(dc,x,y);
我的GetDeviceCaps(RASTERCAPS)
返回RC_BITBLT
启用的结果.GetDeviceCaps(COLORMGMTCAPS)
返回结果为 CM_GAMMA_RAMP
.
My GetDeviceCaps(RASTERCAPS)
returns result that RC_BITBLT
is enabled.GetDeviceCaps(COLORMGMTCAPS)
returns result is CM_GAMMA_RAMP
.
最重要的是,如果我提前SetPixel(dc,x,y,RGB(250,250,250))
,然后GetPixel(dc,x,y)
,我可以总是像这样检索正确的结果:
Most importantly, if I SetPixel(dc,x,y,RGB(250,250,250))
in advance, and GetPixel(dc,x,y)
later, I can ALWAYS retreive correct result like that:
COLORREF result = SetPixel(dc,x,y,RGB(250,250,250));
COLORREF cr = GetPixel(dc,x,y);
所以我觉得我的协调性应该没问题.我不知道为什么 GetPixel()
总是返回 CLR_INVALID
,但 SetPixel()
总是运行良好?有什么建议吗?
So I think my coordination should be alright. I have no idea about why GetPixel()
always return CLR_INVALID
, but SetPixel()
is always worked well? Any suggestions?
推荐答案
来自 GetPixel 文档
必须在设备上下文中选择位图,否则,在所有像素上返回 CLR_INVALID.
试试下面的代码,看看它是否适用于您的设备环境.
Try the below code and see if it works for your device context.
HDC dc = ... // <-- your device context
HDC memDC = CreateCompatibleDC(dc);
HBITMAP memBM = CreateCompatibleBitmap(dc, 1, 1);
SelectObject(memDC, memBM);
int x = ... // point's coordinates
int y = ...
BitBlt(memDC, 0, 0, 1, 1, dc, x, y, SRCCOPY);
COLORREF cr = GetPixel(memDC, 0, 0);
std::cout << cr << std::endl;
DeleteDC(memDC);
DeleteObject(memBM);
这篇关于Windows API `GetPixel()' 总是返回 `CLR_INVALID`,但是 `SetPixel()` 运行良好吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!