Color

This algorithm adds/subtracts a value to each color.

   



									public static void ApplyColor(ref Bitmap bmp, System.Drawing.Color c)
{
	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;

		int RetVal;

		while ((int)ptr != stopAddress)
		{
			RetVal = ptr[2] + c.R;
			if (RetVal < 0) RetVal = 0;
			else if (RetVal > 255) RetVal = 255;
			ptr[2] = (byte)RetVal;

			RetVal = ptr[1] + c.G;
			if (RetVal < 0) RetVal = 0;
			else if (RetVal > 255) RetVal = 255;
			ptr[1] = (byte)RetVal;

			RetVal = ptr[0] + c.B;
			if (RetVal < 0) RetVal = 0;
			else if (RetVal > 255) RetVal = 255;
			ptr[0] = (byte)RetVal;

			ptr += 3;
		}
	}

	bmp.UnlockBits(bmpData);
}
								


Example

									Bitmap b = (Bitmap)Image.FromFile("rose.jpg");
ApplyColor(ref b, Color.Red);