Invert

This algorithm inverts the colors of input image.

   



									public static void ApplyInvert(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[0] = (byte)(255 - ptr[0]);
			ptr[1] = (byte)(255 - ptr[1]);
			ptr[2] = (byte)(255 - ptr[2]);

			ptr += 3;
		}
	}

	bmp.UnlockBits(bmpData);
}
								


Example

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