• 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();
                }
            }

     原图:

     水平镜像:

     垂直镜像:

  • 相关阅读:
    (转)50道JavaScript基础面试题(附答案)
    (转)44 道 JavaScript 难题
    web本地存储(localStorage、sessionStorage)
    (转)在 vue-cli 脚手架中引用 jQuery、bootstrap 以及使用 sass、less 编写 css [vue-cli配置入门]
    (转)Vue 爬坑之路(四)—— 与 Vuex 的第一次接触
    vue 路由传参 params 与 query两种方式的区别
    (转)Vue 爬坑之路(三)—— 使用 vue-router 跳转页面
    【半原创】将js和css文件装入localStorage加速程序执行
    SqlAgent备份脚本
    读<css世界>笔记之img标签
  • 原文地址:https://www.cnblogs.com/dearzhoubi/p/8656365.html
Copyright © 2020-2023  润新知