C#图像镜像

来源:互联网 发布:阿里云上传api 编辑:程序博客网 时间:2024/06/11 08:08

C#水平垂直图像镜像---C#数字图像处理算法典型实例.赵春江

原理:

变换前

           灰度图像--->像素序号=c+r*w(当前列+当前行*列数)

            彩色图像--->像素序号=0+c*3+r*w*3

                                  像素序号=1+c*3+r*w*3

                                  像素序号=2+c*3+r*w*3

变换后参考代码

private void start_Click(object sender, EventArgs e)        {            Rectangle rect= new Rectangle(0,0,_bmp.Width,_bmp.Height);            BitmapData bmpdata = _bmp.LockBits(rect, ImageLockMode.ReadWrite, _bmp.PixelFormat);            IntPtr ptr = bmpdata.Scan0;            int bytes=0;            if(_bmp.PixelFormat==PixelFormat.Format8bppIndexed)//判断是灰度色图像还是彩色图像,给相应的大小            {                bytes=_bmp.Width*_bmp.Height;            }            else if(_bmp.PixelFormat==PixelFormat.Format24bppRgb)            {                bytes = _bmp.Width * _bmp.Height * 3;            }            byte[] pixelValues=new byte[bytes];            System.Runtime.InteropServices.Marshal.Copy(ptr, pixelValues, 0, bytes);//内存法,从内存中将像素复制到pixelValues数组            int halfWidth = _bmp.Width / 2;            int halfheigth= _bmp.Height/2;            byte temp;            byte temp1;            byte temp2;            //灰度图像            if (_bmp.PixelFormat==PixelFormat.Format8bppIndexed)            {                if (HorV)                {                    for (int r = 0; r < _bmp.Height; r++)                        for (int c = 0; c < halfWidth; c++)                        {                            temp = pixelValues[r * _bmp.Width + c];                            pixelValues[r * _bmp.Width + c] = pixelValues[(r + 1) * _bmp.Width - c - 1];                            pixelValues[(r + 1) * _bmp.Width - c - 1] = temp;                        }                                    }                else                {                    for (int c = 0; c < _bmp.Width; c++)                    {                        for (int r = 0; r < halfheigth; r++)                        {                            temp = pixelValues[r * _bmp.Width + c];                            pixelValues[r * _bmp.Width + c] = pixelValues[(_bmp.Height - r - 1) * _bmp.Width + c];                            pixelValues[(_bmp.Height - r - 1) * _bmp.Width + c] = temp;                        }                    }                }            }            else if (_bmp.PixelFormat == PixelFormat.Format24bppRgb)//彩色图像            {                if (HorV)                {                    for (int r = 0; r < _bmp.Height; r++)                        for (int c = 0; c < halfWidth; c++)                        {                            temp  = pixelValues[0 + r * _bmp.Width * 3 + c * 3];                            temp1 = pixelValues[1 + r * _bmp.Width * 3 + c * 3];                            temp2 = pixelValues[2 + r * _bmp.Width * 3 + c * 3];                            pixelValues[0 + r * _bmp.Width * 3 + c * 3] = pixelValues[0 + (r + 1) * _bmp.Width * 3 - (c + 1) * 3];                            pixelValues[1 + r * _bmp.Width * 3 + c * 3] = pixelValues[1 + (r + 1) * _bmp.Width * 3 - (c + 1) * 3];                            pixelValues[2 + r * _bmp.Width * 3 + c * 3] = pixelValues[2 + (r + 1) * _bmp.Width * 3 - (c + 1) * 3];                            pixelValues[0 + (r + 1) * _bmp.Width * 3 - (c + 1) * 3]=temp;                            pixelValues[1 + (r + 1) * _bmp.Width * 3 - (c + 1) * 3]=temp1;                            pixelValues[2 + (r + 1) * _bmp.Width * 3 - (c + 1) * 3] = temp2;                        }                }                else                {                    for (int c = 0; c < _bmp.Width; c++)                    {                        for (int r = 0; r < halfheigth; r++)                        {                            temp  = pixelValues[0 + r * _bmp.Width * 3 + c * 3];                            temp1 = pixelValues[1 + r * _bmp.Width * 3 + c * 3];                            temp2 = pixelValues[2 + r * _bmp.Width * 3 + c * 3];                            pixelValues[0 + r * _bmp.Width * 3 + c * 3] = pixelValues[0 + (_bmp.Height - r - 1) * _bmp.Width * 3 + c * 3];                            pixelValues[1 + r * _bmp.Width * 3 + c * 3] = pixelValues[1 + (_bmp.Height - r - 1) * _bmp.Width * 3 + c * 3];                            pixelValues[2 + r * _bmp.Width * 3 + c * 3] = pixelValues[2 + (_bmp.Height - r - 1) * _bmp.Width * 3 + c * 3];                            pixelValues[0 + (_bmp.Height - r - 1) * _bmp.Width * 3 + c * 3] = temp;                            pixelValues[1 + (_bmp.Height - r - 1) * _bmp.Width * 3 + c * 3] = temp1;                            pixelValues[2 + (_bmp.Height - r - 1) * _bmp.Width * 3 + c * 3] = temp2;                        }                    }                }            }            System.Runtime.InteropServices.Marshal.Copy(pixelValues, 0, ptr, bytes);            _bmp.UnlockBits(bmpdata);            _pictureBox.Image = _bmp;
水平镜像效果

垂直镜像效果


解决方案已上传:http://download.csdn.net/download/saw009/10169694点击打开链接

原创粉丝点击