/// <summary> /// 水平镜像 /// </summary> /// <param name="bmp"></param> /// <returns></returns> public static Bitmap MirrorH(Bitmap bmp) { if (bmp == null) return bmp; Rectangle bmpRect = new Rectangle(0, 0, bmp.Width, bmp.Height); BitmapData bmpData = bmp.LockBits(bmpRect, ImageLockMode.ReadWrite, bmp.PixelFormat); int bytes = bmpData.Stride * bmp.Height; byte[] rgbValues = new byte[bytes]; IntPtr ptr = bmpData.Scan0; Marshal.Copy(ptr, rgbValues, 0, bytes); byte[] newRgbValues = new byte[bytes]; for (int y = 0; y < bmp.Height; y++) { for (int x = 0; x < bmp.Width * 3; x += 3) { newRgbValues[y * bmpData.Stride + (bmp.Width - 1) * 3 - x] = rgbValues[y * bmpData.Stride + x]; newRgbValues[y * bmpData.Stride + (bmp.Width - 1) * 3 - x + 1] = rgbValues[y * bmpData.Stride + x + 1]; newRgbValues[y * bmpData.Stride + (bmp.Width - 1) * 3 - x + 2] = rgbValues[y * bmpData.Stride + x + 2]; } } Marshal.Copy(newRgbValues, 0, ptr, bytes); bmp.UnlockBits(bmpData); return bmp; } /// <summary> /// 垂直镜像 /// </summary> /// <param name="bmp"></param> /// <returns></returns> public static Bitmap MirrorV(Bitmap bmp) { if (bmp == null) return bmp; Rectangle bmpRect = new Rectangle(0, 0, bmp.Width, bmp.Height); BitmapData bmpData = bmp.LockBits(bmpRect, ImageLockMode.ReadWrite, bmp.PixelFormat); int bytes = bmpData.Stride * bmp.Height; byte[] rgbValues = new byte[bytes]; IntPtr ptr = bmpData.Scan0; Marshal.Copy(ptr, rgbValues, 0, bytes); byte[] newRgbValues = new byte[bytes]; for (int y = 0; y < bmp.Height; y++) { for (int x = 0; x < bmp.Width * 3; x++) { newRgbValues[(bmp.Height - y - 1) * bmpData.Stride + x] = rgbValues[y * bmpData.Stride + x]; } } Marshal.Copy(newRgbValues, 0, ptr, bytes); bmp.UnlockBits(bmpData); return bmp; }