Flip Horizontal

This algorithm flips specified image in a horizontal direction.

   



									public static void ApplyHorizontalFlip(ref Bitmap bmp)
{
	Bitmap TempBmp = (Bitmap)bmp.Clone();

	BitmapData bmpData = bmp.LockBits(new Rectangle(0, 0, bmp.Width, bmp.Height), ImageLockMode.ReadWrite, PixelFormat.Format24bppRgb);
	BitmapData TempBmpData = TempBmp.LockBits(new Rectangle(0, 0, TempBmp.Width, TempBmp.Height), ImageLockMode.ReadOnly, PixelFormat.Format24bppRgb);

	unsafe
	{
		int BmpWidth = bmp.Width;
		int BmpHeight = bmp.Height;
		int Stride = bmpData.Stride;

		byte* ptr = (byte*)bmpData.Scan0.ToPointer();
		byte* TempPtr = (byte*)TempBmpData.Scan0.ToPointer();

		int stopAddress = (int)ptr + bmpData.Stride * bmpData.Height;
		int i = 0, X, Y;
		int Val = 0;
		int XOffset = 0;

		while ((int)ptr != stopAddress)
		{
			X = i % BmpWidth;
			Y = i / BmpWidth;

			XOffset = BmpWidth - (X + 1);

			if (XOffset < 0 && XOffset >= BmpWidth)
				XOffset = 0;

			Val = (Y * Stride) + (XOffset * 3);

			ptr[0] = TempPtr[Val];
			ptr[1] = TempPtr[Val + 1];
			ptr[2] = TempPtr[Val + 2];

			ptr += 3;
			i++;
		}
	}

	bmp.UnlockBits(bmpData);
	TempBmp.UnlockBits(TempBmpData);
}
								


Example

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