使用 BitBlt 捕获桌面像素颜色
现在我正在使用 GetPixel() 从桌面检索大约 64 个像素以获得它们的颜色。我读到有关 GetPixel() 速度很慢的消息,但认为这对于几个像素来说并不重要,但每次运行该例程大约需要 1.5 秒。经过一些研究后,我得出的结论是 bitblt 似乎就是我正在寻找的。我想要做的是抓取桌面的定义区域(包括所有窗口),然后抓取给定偏移处的像素颜色。这就是我现在正在做的事情:
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 来说,它们对我来说并没有多大用处。谢谢!
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;
}
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!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可能想要:
如果您定期执行此操作,那么您应该只执行前三个步骤一次,重复
BitBlt< /code> 和
GetDIBits
,以及程序完成时的最后三个。You're probably wanting:
If you're doing this periodically, then you should do the first three steps only once, repeat
BitBlt
andGetDIBits
, and the last three when your program finishes.