Flip Vertical

This algorithm flips specified image in a vertical direction.

   



									Public Shared Sub ApplyVerticalFlip(ByRef bmp As Bitmap)
	Dim TempBmp As Bitmap = DirectCast(bmp.Clone(), Bitmap)

	Dim bmpData As BitmapData = bmp.LockBits(New Rectangle(0, 0, bmp.Width, bmp.Height), ImageLockMode.ReadWrite, PixelFormat.Format24bppRgb)
	Dim TempBmpData As BitmapData = TempBmp.LockBits(New Rectangle(0, 0, TempBmp.Width, TempBmp.Height), ImageLockMode.[ReadOnly], PixelFormat.Format24bppRgb)

	Dim BmpWidth As Integer = bmp.Width
	Dim BmpHeight As Integer = bmp.Height
	Dim Stride As Integer = bmpData.Stride

	Dim ptr As IntPtr = bmpData.Scan0
	Dim TempPtr As IntPtr = TempBmpData.Scan0

	Dim stopAddress As Integer = CInt(ptr) + bmpData.Stride * bmpData.Height
	Dim i As Integer = 0, X As Integer, Y As Integer
	Dim Val As Integer = 0
	Dim YOffset As Integer = 0

	While CInt(ptr) <> stopAddress
		X = i Mod BmpWidth
		Y = i \ BmpWidth

		YOffset = BmpHeight - (Y + 1)

		If YOffset < 0 AndAlso YOffset >= BmpHeight Then
			YOffset = 0
		End If

		Val = (YOffset * Stride) + (X * 3)

		Marshal.WriteByte(ptr, Marshal.ReadByte(TempPtr + Val))
		Marshal.WriteByte(ptr + 1, Marshal.ReadByte(TempPtr + Val + 1))
		Marshal.WriteByte(ptr + 2, Marshal.ReadByte(TempPtr + Val + 2))

		ptr += 3
		i += 1
	End While

	bmp.UnlockBits(bmpData)
	TempBmp.UnlockBits(TempBmpData)
End Sub
								


Example

									DIm b As Bitmap = CType(Image.FromFile("rose.jpg"), Bitmap)
ApplyVerticalFlip(b)