Wednesday, March 20, 2013

Determining percentage of colors in a bitmap using C# without getPixel

To keep things simple, let's suppose we have a black and white bitmap image.
I will not use getPixel method here, Bitmap.getPixel() is a very expensive method, and it alone can turn your program into a lazy snail.

Please note that I am using a Format24bpprgb image format here, 24BPPRGB can be defined as:
Each pixel in the image is represented by 24 bits. First 8 bits represent red color, next 8 bits represent green color, and last 8 bits represent blue color in a bitmap.

Another popular format is 32BPPARGB, in this format the first 8 bits represent "Alpha" or the opacity of pixel, next 3 bytes or 24 bits describe the RGB components same like 24BPPRGB.

Given below is the C# language source code for checking pixel colors without Bitmap.getPixel() method.