c#数字图像处理(九)图像镜像

 

 

 

 

 

 

        private void mirror_Click(object sender, EventArgs e)
        {
            if (curBitmap!=null)
            {
                mirror mirForm = new mirror();
                if (mirForm.ShowDialog()==DialogResult.OK)
                {
                    Rectangle rect = new Rectangle(0, 0, curBitmap.Width, curBitmap.Height);
                    BitmapData bmpData = curBitmap.LockBits(rect, ImageLockMode.ReadWrite, curBitmap.PixelFormat);
                    IntPtr ptr = bmpData.Scan0;
                    int bytes = 0;
                    ////判断是灰度色图像还是彩色图像,给相应的大小
                    if (curBitmap.PixelFormat==PixelFormat.Format8bppIndexed)
                    {
                        bytes= curBitmap.Width * curBitmap.Height;
                    }
                    else if (curBitmap.PixelFormat == PixelFormat.Format24bppRgb)
                    {
                        bytes = curBitmap.Width * curBitmap.Height * 3;
                    }
                    byte[] pixelValues = new byte[bytes];
                    Marshal.Copy(ptr, pixelValues, 0, bytes);

                    //水平中轴
                    int halfWidth = curBitmap.Width / 2;
                    //垂直中轴
                    int halfHeight = curBitmap.Height / 2;
                    byte temp;
                    byte temp1;
                    byte temp2;
                    byte temp3;
                    if (curBitmap.PixelFormat == PixelFormat.Format8bppIndexed)
                    {
                        if (mirForm.GetMirror)
                        {
                            for (int i = 0; i < curBitmap.Height; i++)
                                for (int j = 0; j < halfWidth; j++)
                                {
                                    temp = pixelValues[i * curBitmap.Width + j];
                                    pixelValues[i * curBitmap.Width + j] = pixelValues[(i + 1) * curBitmap.Width - j - 1];
                                    pixelValues[(i + 1) * curBitmap.Width - j - 1] = temp;
                                }
                        }
                        else
                        {
                            for (int j = 0; j < curBitmap.Width; j++)
                            {
                                for (int i = 0; i < halfHeight; i++)
                                {
                                    temp = pixelValues[i * curBitmap.Width + j];
                                    pixelValues[i * curBitmap.Width + j] = pixelValues[(curBitmap.Height - i - 1) * curBitmap.Width + j];
                                    pixelValues[(curBitmap.Height - i - 1) * curBitmap.Width + j] = temp;
                                }
                            }
                        }
                    }
                    else if (curBitmap.PixelFormat == PixelFormat.Format24bppRgb)
                    {
                        if (mirForm.GetMirror)
                        {
                            //水平镜像处理  
                            for (int i = 0; i < curBitmap.Height; i++)
                            {
                                //每个像素的三个字节在水平镜像时顺序不能变,所以这个方法不能用
                                //for (int j = 0; j < halfWidth; j++)
                                //{
                                //    //以水平中轴线为对称轴,两边像素值交换  
                                //    temp = pixelValues[i * curBitmap.Width * 3 + j * 3];
                                //    pixelValues[i * curBitmap.Width * 3 + j * 3] = pixelValues[(i + 1) * curBitmap.Width * 3 - 1 - j * 3];
                                //    pixelValues[(i + 1) * curBitmap.Width * 3 - 1 - j * 3] = temp;
                                //}
                                for (int j = 0; j < halfWidth; j++)
                                {//每三个字节组成一个像素,顺序不能乱
                                    temp = pixelValues[0 + i * curBitmap.Width * 3 + j * 3];
                                    temp1 = pixelValues[1 + i * curBitmap.Width * 3 + j * 3];
                                    temp2 = pixelValues[2 + i * curBitmap.Width * 3 + j * 3];
                                    pixelValues[0 + i * curBitmap.Width * 3 + j * 3] = pixelValues[0 + (i + 1) * curBitmap.Width * 3 - (j + 1) * 3];
                                    pixelValues[1 + i * curBitmap.Width * 3 + j * 3] = pixelValues[1 + (i + 1) * curBitmap.Width * 3 - (j + 1) * 3];
                                    pixelValues[2 + i * curBitmap.Width * 3 + j * 3] = pixelValues[2 + (i + 1) * curBitmap.Width * 3 - (j + 1) * 3];
                                    pixelValues[0 + (i + 1) * curBitmap.Width * 3 - (j + 1) * 3] = temp;
                                    pixelValues[1 + (i + 1) * curBitmap.Width * 3 - (j + 1) * 3] = temp1;
                                    pixelValues[2 + (i + 1) * curBitmap.Width * 3 - (j + 1) * 3] = temp2;
                                }
                            }
                        }
                        else
                        {
                            //垂直镜像处理  
                            for (int i = 0; i < curBitmap.Width * 3; i++)
                            {
                                for (int j = 0; j < halfHeight; j++)
                                {
                                    //以垂直中轴线为对称轴。两边像素值互换  
                                    temp = pixelValues[j * curBitmap.Width * 3 + i];
                                    pixelValues[j * curBitmap.Width * 3 + i] = pixelValues[(curBitmap.Height - j - 1) * curBitmap.Width * 3 + i];
                                    pixelValues[(curBitmap.Height - j - 1) * curBitmap.Width * 3 + i] = temp;
                                }
                            }
                        }
                    }

                        Marshal.Copy(pixelValues, 0, ptr, bytes);
                    curBitmap.UnlockBits(bmpData);
                }
                Invalidate();
            }
        }

 原图:

 水平镜像:

 垂直镜像:

 

posted @ 2018-03-27 11:05  微光-倾城  阅读(2965)  评论(0编辑  收藏  举报