Grayscale

This algorithm converts input image to grayscale (a.k.a greyscale) image.

   



									public static void ApplyGrayscale(ref Bitmap bmp)
{
	BitmapData bmpData = bmp.LockBits(new Rectangle(0, 0, bmp.Width, bmp.Height), ImageLockMode.ReadWrite, PixelFormat.Format24bppRgb);

	unsafe
	{
		byte* ptr = (byte*)bmpData.Scan0.ToPointer();
		int stopAddress = (int)ptr + bmpData.Stride * bmpData.Height;

		while ((int)ptr != stopAddress)
		{
			*ptr = (byte)((ptr[2] * .299) + (ptr[1] * .587) + (ptr[0] * .114));
			ptr[1] = *ptr;
			ptr[2] = *ptr;

			ptr += 3;
		}
	}

	bmp.UnlockBits(bmpData);
}
								


Example

									Bitmap b = (Bitmap)Image.FromFile("rose.jpg");
ApplyGrayscale(ref b);