使用 BitBlt 捕捉桌面像素颜色 [英] Using BitBlt to capture desktop pixel colors
问题描述
现在我正在使用 GetPixel() 从桌面检索大约 64 个像素以获得它们的颜色.我读到 GetPixel() 很慢,但不认为它对几个像素有影响,但每次运行例程需要 1.5 秒.在做了一些研究之后,我得出结论,bitblt 似乎是我正在寻找的.我想要做的是抓取桌面的定义区域(包括所有窗口),然后抓取给定偏移量的像素颜色.这是我现在正在做的:
Right now I'm using GetPixel() to retrieve about 64 pixels from the desktop in order to obtain their color. I read about GetPixel() being slow but didn't think it would matter for a few pixels but it's taking like 1.5 seconds each time I run the routine. After doing some research I've concluded that bitblt seems like what I'm looking for. What I want to do is grab a defined area of the desktop (including all windows) and then grab pixel colors at given offsets. Here's what I'm doing now:
for (y=0;y<=7;y++) {
for (x=0;x<=7;x++) {
//gameScreen is a struct containing the offset from the top left of the monitor
//to the area of the screen I need
grid[y][x]=getColor(gameScreen.x+((x*40)+20),gameScreen.y+((y*40)+20));
}
}
int getColor(int x, int y) {
//create new point at given coordinates
POINT point;
point.x=x;
point.y=y;
//convert to logical points
DPtoLP(desktopDC,&point,2);
//get pixel color
//desktopDC is an HDC from GetWindowDC(GetDesktopWindow())
int pixel=GetPixel(desktopDC,point.x,point.y);
return pixel;
}
我找到了大量的教程和文档,但对 Windows API 来说太新了,他们对我来说并没有做太多.谢谢!
I've found a decent amount of tutorial and documentation, but being so new to the windows API they aren't doing to much for me. Thanks!
推荐答案
您可能想要:
- 创建CompatibleDC
- 创建CompatibleBitmap
- SelectObject,保存原始位图
- BitBlt
- 获取DIBits
- SelectObject,放回原始位图
- 删除位图
- 删除DC
如果您定期执行此操作,那么您应该只执行前三个步骤一次,重复 BitBlt
和 GetDIBits
,并在程序完成时重复最后三个步骤.
If you're doing this periodically, then you should do the first three steps only once, repeat BitBlt
and GetDIBits
, and the last three when your program finishes.
这篇关于使用 BitBlt 捕捉桌面像素颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!