Flip Vertical

This algorithm flips specified image in a vertical direction.

   



									public static void ApplyVerticalFlip(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 YOffset = 0;

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

			YOffset = BmpHeight - (Y + 1);

			if (YOffset < 0 && YOffset >= BmpHeight)
				YOffset = 0;

			Val = (YOffset * Stride) + (X * 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");
ApplyVerticalFlip(ref b);