【C#图像处理】关于水平镜像问题。菜鸟求助!
先上代码,在选择窗体里,如果选择水平镜像,点击确定后就进行水平镜像处理
if (curBitmap != null)
{
//一个选择是否用水平镜像的窗体
mirror mirForm = new mirror();
if (mirForm.ShowDialog() == DialogResult.OK)
{
Rectangle rect = new Rectangle(0, 0, curBitmap.Width, curBitmap.Height);
System.Drawing.Imaging.BitmapData bmpData = curBitmap.LockBits(rect, System.Drawing.Imaging.ImageLockMode.ReadWrite, curBitmap.PixelFormat);
IntPtr ptr = bmpData.Scan0;
int bytes = curBitmap.Width * curBitmap.Height*3;
byte[] grayValues = new byte[bytes];
System.Runtime.InteropServices.Marshal.Copy(ptr, grayValues, 0, bytes);
int halfWidth = curBitmap.Width / 2;
int halfHeight = curBitmap.Height / 2;
byte temp;
//以上是获取图像,然后接着用内存法处理了
//主要的水平镜像处理就是在这里面了
if (mirForm.GetMirror)
{
for (int i = 0; i < curBitmap.Height; i++)
{
//这里减3是因为我下面K还多出2个
for (int j = 0; j <=halfWidth*3-2; j=j+3)
{
for (int k = 0; k < 3; k++)
{
temp = grayValues[i * curBitmap.Width * 3 + j + k];
grayValues[i * curBitmap.Width * 3 + j + k] =
grayValues[i * curBitmap.Width * 3 + (curBitmap.Width - (j / 3 + 1)) * 3 + k];
grayValues[i * curBitmap.Width * 3 + (curBitmap.Width - (j / 3 + 1)) * 3 + k] = temp;
}
}
}
}
实际上编译时候,没有出过,但是效果确十分的夸张,连纯色的图进行这段代码,都会出现其他颜色。
先说说我对水平镜像的粗浅看法,希望有人指正,首先对面处理图像,一个像素点,需要3个数值去储存。
水平镜像的原理就是 x'=width-x
y=y
但是比如第一个像素点,转移到最后一个像素点,要把数组里面存的值前3个都拿最后并且按顺序。这里我是用(j / 3 + 1)(j为X坐标)来判断现在这个点是第几个点,所以x'=width-x=width-(j/3+1).
其中还让k从0到2,能够让同一个像素点里的三个数值,按顺便转移。
但是代码实现在我想要的功能,希望能够有人可以指导下我 十分感谢