简体   繁体   中英

What is the fastest method to lookup pixel RGB data in C++?

I want to look at and store in an array the RGB data of each pixel in a 100 X 100 square of my screen. I got this concept working using GetPixel() in a for loop, and then converting the output to hexadecimal and storing the RGB data in an array, but this method takes much longer than I'd like. What is the fastest way of reading pixel RGB data with c++?

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 (eg, the screen).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM