在图形设备上下文中搜索特定颜色的像素

发布于 2024-08-01 21:14:46 字数 321 浏览 1 评论 0原文

我正在编写一些自动化代码,但在寻找一种有效的方法来检查显示器上当前存在的像素时遇到了问题。

我已经熟悉了gdi32库中存在的GetPixel函数,但是它太慢了(实际上扫描一行约1900像素的线就需要大约10秒)。

我已经提出了从图形设备上下文的片段中制作位图并测试这是否更有效的想法(我无法想象它会是这样,考虑到需要大量的读/写才能保持合理的状态)当前的屏幕图像),但在我这样做之前,我想知道是否有人有更好的想法。

我真正喜欢的是一种在部分或全部当前(或相当接近当前)图形上下文中收集像素颜色的二维数组的有效方法,希望使用 .NET。

I'm working on writing some automation code and I'm running into a problem finding an efficient way to examine the pixels that presently exist on the display.

I've familiarized myself with the GetPixel function that exists in the gdi32 library, but it's too slow (in fact it takes about 10 seconds just to scan a line of about 1900 pixels).

I've kicked the idea around of making a bitmap out of segments of the graphics device context and testing if this is more efficient (which I can't imagine it would be, considering it would take a lot of r/w to stay reasonably current with the screen image), but before I do that I'd like to know if anyone has any better ideas.

What I'd really love is an efficient way to gather a 2 dimensional array of pixel colors in some or all of the present (or reasonably close to present) graphics context, hopefully using .NET.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

花间憩 2024-08-08 21:14:46

您必须锁定图像并直接使用内存。 此处有一篇关于如何执行此操作的好文章。

You have to lock the image and work with the memory directly. There is a good article about how to do this here.

浅沫记忆 2024-08-08 21:14:46

我最终所做的是捕获整个图形上下文的屏幕截图,并在生成的位图中搜索我在静态上下文中正在寻找的内容。 然后,因为我关心的图形输出区域只有大约 70x70 像素,所以我只是每 10 毫秒捕获这一小部分,并检查它是否有有趣的变化。

这不需要任何不安全的代码或图形上下文的锁定,并且相当高效。

What I ended up doing was capturing a screenshot of the entire graphics context and searching the resulting bitmap for exactly what I was looking for in a static context. Then, because the area of the graphics output I was concerned with was only about 70x70 pixels, I just captured this small portion every 10 milliseconds and examined it for interesting changes.

This didn't require any unsafe code or locking of the graphics context and was reasonably efficient.

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