Flip Both
This algorithm flips specified image in both vertical and horizontal directions.
public static void ApplyBothFlip(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;
int YOffset = 0;
while ((int)ptr != stopAddress)
{
X = i % BmpWidth;
Y = i / BmpWidth;
XOffset = BmpWidth - (X + 1);
YOffset = BmpHeight - (Y + 1);
if (XOffset < 0 && XOffset >= BmpWidth)
XOffset = 0;
if (YOffset < 0 && YOffset >= BmpHeight)
YOffset = 0;
Val = (YOffset * 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");
ApplyBothFlip(ref b);