C#图像镜像

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点击打开链接

  • 4
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值