Flip Both
This algorithm flips specified image in both vertical and horizontal directions.
Public Shared Sub ApplyBothFlip(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 XOffset As Integer = 0
Dim YOffset As Integer = 0
While CInt(ptr) <> stopAddress
X = i Mod BmpWidth
Y = i \ BmpWidth
XOffset = BmpWidth - (X + 1)
YOffset = BmpHeight - (Y + 1)
If XOffset < 0 AndAlso XOffset >= BmpWidth Then
XOffset = 0
End If
If YOffset < 0 AndAlso YOffset >= BmpHeight Then
YOffset = 0
End If
Val = (YOffset * Stride) + (XOffset * 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)
ApplyBothFlip(ref b)