图像镜像翻转
来源:互联网 发布:毕向东java教程百度云 编辑:程序博客网 时间:2024/05/09 08:19
-
-
-
-
-
- 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;
- }
-
-
-
-
-
-
- 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;
- }