在 C++ 中查找像素 RGB 數據的最快方法是什麼? (What is the fastest method to lookup pixel RGB data in C++?)


問題描述

在 C++ 中查找像素 RGB 數據的最快方法是什麼? (What is the fastest method to lookup pixel RGB data in C++?)

我想查看 100 X 100 正方形屏幕中每個像素的 RGB 數據並將其存儲在一個數組中。我在 for 循環中使用 GetPixel() 得到了這個概念,然後將輸出轉換為十六進制並將 RGB 數據存儲在一個數組中,但是這種方法比我想要的要長得多。用c++讀取像素RGB數據最快的方法是什麼?


參考解法

方法 1:

Yes, GetPixel and SetPixel are relatively slow API calls. Manipulating individual pixels requires copying the contents of the DC (Device Context) into a temporary bitmap, mapping and locating the pixel, retrieving (or setting) its color value, and then (if the pixel's color is being set) blitting the temporary bitmap back to the device context. Once you understand all the work that is going on behind the scenes for such an apparently simple operation, you can see why it is slow. Worse, the overhead must be paid for each call to GetPixel and/or SetPixel, so you can see why these are not commonly used for painting operations.

The replacement approach involves the use of GetDIBits, which is the fastest possible way of determining the values of multiple pixels. This function essentially copies the contents of the device context (DC) into a temporary device‑independent bitmap (DIB), which you can then view or manipulate as desired. Determining the color value of an individual pixel becomes as simple as indexing into that pixel's location in memory. With this approach, you can literally process millions of pixels per second. The overhead of retrieving the pixel color values is paid only once, at the very beginning.

To simulate SetPixel, you'd manipulate the desired pixel values, then call SetDIBits to update the device context (e.g., the screen).

(by NO_GUICody Gray)

參考文件

  1. What is the fastest method to lookup pixel RGB data in C++? (CC BY‑SA 2.5/3.0/4.0)

#gdi #C++ #Windows






相關問題

繪製鼠標光標 (Draw mouse cursor)

C 和 Windows GDI 中的雙緩衝 *framework* (Double-buffering *framework* in C and Windows GDI)

一個進程的 GDI 洩漏會影響其他進程嗎? (Can GDI leaks from one process affect other processes?)

BeginPath Textout EndPath 繪製反轉文本 (BeginPath Textout EndPath draws inverted text)

監控GDI通話 (Monitoring GDI calls)

如何捕獲(並希望修復)GDI 資源洩漏 (How to catch (and hopefully fix) a GDI resource leak)

如何使用 GDI(+) 在內存中渲染漸變 (How to render a gradient in memory using GDI(+))

使用 BITMAP::bmBits 與 GetDIBits 有什麼區別? (What is the difference between using BITMAP::bmBits vs GetDIBits?)

獲取背景窗口的縮略圖 (Get Thumbnail of background window)

AlphaBlend 返回“假”的原因可能是什麼 (What could be the reason for AlphaBlend to return 'false')

在 C++ 中查找像素 RGB 數據的最快方法是什麼? (What is the fastest method to lookup pixel RGB data in C++?)

逐行讀取文本框並將其視為單獨的文本 (Read a textbox line by line and treat it as a separate text)







留言討論