private uint CountPixels(Image img)
{
var s = DateTime.Now;
uint result = 0;
int col = colorDialog1.Color.ToArgb();
Bitmap bmp = new Bitmap(img);
Rectangle rect = new Rectangle(0, 0, bmp.Width, bmp.Height);
System.Drawing.Imaging.BitmapData bmpData =
bmp.LockBits(rect, System.Drawing.Imaging.ImageLockMode.ReadWrite,
bmp.PixelFormat);
bmpData.PixelFormat = System.Drawing.Imaging.PixelFormat.Format32bppArgb;
// Get the address of the first line.
IntPtr ptr = bmpData.Scan0;
// Declare an array to hold the bytes of the bitmap.
int pixelcount = bmp.Width * bmp.Height;
int[] rgbValues = new int[pixelcount];
// Copy the
RGB values into the array.
System.Runtime.InteropServices.Marshal.Copy(ptr, rgbValues, 0, pixelcount);
for (int i = 0; i < rgbValues.Length; i++)
{
if (rgbValues[i] == col)
result++;
}
// Unlock the bits.
bmp.UnlockBits(bmpData);
bmpData = null;
bmp.Dispose();
rgbValues = null;
var e = DateTime.Now;
System.Console.WriteLine((e - s).TotalMilliseconds);
return result;
}