使用 BitBlt 捕获桌面像素颜色

发布于 2024-10-09 14:08:55 字数 879 浏览 3 评论 0原文

现在我正在使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

难理解 2024-10-16 14:08:55

您可能想要:

  • CreateCompatibleDC
  • CreateCompatibleBitmap
  • SelectObject,保存原始位图
  • BitBlt
  • GetDIBits
  • SelectObject,将原始位图放回
  • DeleteBitmap
  • DeleteDC

如果您定期执行此操作,那么您应该只执行前三个步骤一次,重复 BitBlt< /code> 和 GetDIBits,以及程序完成时的最后三个。

You're probably wanting:

  • CreateCompatibleDC
  • CreateCompatibleBitmap
  • SelectObject, saving the original bitmap
  • BitBlt
  • GetDIBits
  • SelectObject, putting the original bitmap back
  • DeleteBitmap
  • DeleteDC

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文