windows 有没有比 GDI GetPixel() 更快的替代方法?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4235731/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Is there a faster alternative to GDI GetPixel()?
提问by stucampbell
I'm using GetPixel()
from gdi32.dll in a .NET app to sample the colour of a pixel anywhere on the screen. It works ok but it is a major performance bottleneck for me.
我GetPixel()
在 .NET 应用程序中使用来自 gdi32.dll 来对屏幕上任意位置的像素颜色进行采样。它工作正常,但对我来说是一个主要的性能瓶颈。
Is there a faster way of doing it?
有没有更快的方法?
回答by Aliostad
Fast access to pixels are possible using LockBits()
method of the Bitmap
. This will return to you an object containing a pointer to the start of the pixel data and you can use unsafe code to access the memory.
使用 的LockBits()
方法可以快速访问像素Bitmap
。这将返回给您一个包含指向像素数据开头的指针的对象,您可以使用不安全的代码来访问内存。
回答by valdo
GetPixel
is slow for two reasons:
GetPixel
速度慢有两个原因:
Since you're polling the screen - every call to
GetPixel
leads to a transaction to the video driver, which in turn takes the pixel data from the video memory.In constrast using
GetPixel
on DIBs is very much faster.Anyway
GetPixel
does several things, including coordinates clipping/transformations and etc.
由于您正在轮询屏幕 - 每次调用都会
GetPixel
导致视频驱动程序的事务,而视频驱动程序又从视频内存中获取像素数据。相比之下,
GetPixel
在 DIB 上使用要快得多。无论如何
GetPixel
做了几件事,包括坐标裁剪/转换等。
So that if you're using to query many pixel values at once - you should try to arrange this in a single transaction to GDI / video driver.
因此,如果您使用一次查询多个像素值 - 您应该尝试将其安排在 GDI/视频驱动程序的单个事务中。
Using GDI you should create a DIB of the adequate size (see CreateDIBSection
). After creation you'll be given a direct pointer to the image bits data. Then copy the image part onto your DIB (see BitBlt
). Also don't forget to call GdiFlush
before you actually check the contents of the DIB (since video drivers may do asynchronous drawing).
使用 GDI,您应该创建一个足够大小的 DIB(请参阅 参考资料CreateDIBSection
)。创建后,您将获得一个指向图像位数据的直接指针。然后将图像部分复制到您的 DIB(请参阅 参考资料BitBlt
)。另外不要忘记GdiFlush
在实际检查 DIB 的内容之前调用(因为视频驱动程序可能会进行异步绘图)。
Using GD+ you may actually do the same, with a bit simpler syntax.
使用 GD+,您实际上可以使用更简单的语法来做同样的事情。