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

 原圖:

 水平鏡像:

 垂直鏡像:

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM