First of all, you need a gray-scale bitmap. If it is colored, you need to work with if as with a gray scale, which would create some ambiguity: there can be several equally valid methods giving slightly different results.
You need to calculate color of all pixels and classify them into several groups of brightness. The number of desired groups is a free parameter; so you can have different results depending on it. I suggest you work with the arrahys of length of
2<sup>N</sup>
, where
N
is some integer > 1. If you need to show full non-normalized histogram (which is preferred), you should create and array of N elements and define the brightness range for each array element with zero array element accepting pixels starting from zero (black) and last element — pixels of the brightest (maximum value, typically 255, but can be something like
2<sup>12</sup>
to
2<sup>14</sup>
(usual range for photo cameras raw mode these days; there are also so called HDR cameras).
How to get all pixels? It you use something like
GetPixel
, it would be infinitely long. Here is what wrong with you answer: you should have specified if you want to use
System.Drawing
or WPF (or something else) — the techniques are similar but completely different.
In brief, for
System.Drawing
use
System.Drawing.Bitmap.LockBits
, see the code sample:
http://msdn.microsoft.com/en-us/library/5ey6h79d.aspx[
^]; for WPF, use
System.Windows.Media.Imaging.BitmapSource
and one of the methods
CopyPixels
, see
http://msdn.microsoft.com/en-us/library/system.windows.media.imaging.bitmapsource.aspx[
^]; you will find some code samples in the help pages on
CopyPixel
.
—SA