首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > .NET > C# >

【C#图像处理】关于水平镜像有关问题。初学者

2013-03-17 
【C#图像处理】关于水平镜像问题。菜鸟求助!先上代码,在选择窗体里,如果选择水平镜像,点击确定后就进行水平镜

【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,能够让同一个像素点里的三个数值,按顺便转移。


但是代码实现在我想要的功能,希望能够有人可以指导下我 十分感谢  C# 图像 图像处理 水平镜像
[解决办法]
楼主参考下这个
[解决办法]
你去查了没有,这个像素格式就是一个像素占多少空间,根据这个来进行你的内存处理,尤其是行对齐,否则当然就乱掉了。
[解决办法]
可能是我没看明白。。。图片直接就能翻转和旋转了,RotateFlipType有好多枚举的;比如旋转90度:
Image i = Image.FromStream(ms);
i.RotateFlip(RotateFlipType.Rotate90FlipXY);

[解决办法]
System.Drawing.Imaging.BitmapData?bmpData?=?curBitmap.LockBits(rect,?System.Drawing.Imaging.ImageLockMode.ReadWrite,?curBitmap.PixelFormat);



既然是3位3位下去,那么这里最后一个参数要用24位RGB。
[解决办法]
帮忙顶一下 论坛上关于图像处理的帖子不多
[解决办法]

引用:
先上代码,在选择窗体里,如果选择水平镜像,点击确定后就进行水平镜像处理
  if (curBitmap != null)
            {
               //一个选择是否用水平镜像的窗体
                mirror mirForm = new mirror();
               
           ……
这个太简单了,给你代码,你直接用吧,速度绝对没问题!方法1http://dongtingyueh.blog.163.com/blog/static/461945320117992835689/
方法2:http://dongtingyueh.blog.163.com/blog/static/461945320126169522784/
http://dongtingyueh.blog.163.com/blog/static/461945320126178162240/

热点排行