C # Digital Image processing (ix) Image mirroring

Source: Internet
Author: User

        Private voidMirror_click (Objectsender, EventArgs e) {            if(curbitmap!=NULL) {Mirror Mirform=NewMirror (); if(Mirform.showdialog () = =DialogResult.OK) {Rectangle rect=NewRectangle (0,0, Curbitmap.width, curbitmap.height); BitmapData Bmpdata=curbitmap.lockbits (Rect, Imagelockmode.readwrite, Curbitmap.pixelformat); IntPtr ptr=Bmpdata.scan0; intbytes =0; ////Determine whether the grayscale image or color image, to the corresponding size                    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); //Horizontal Middle Axis                    intHalfwidth = curbitmap.width/2; //Vertical Middle Axis                    intHalfheight = curbitmap.height/2; bytetemp; byteTemp1; byteTemp2; byteTemp3; if(Curbitmap.pixelformat = =pixelformat.format8bppindexed) {if(mirform.getmirror) { for(inti =0; i < curbitmap.height; i++)                                 for(intj =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(intj =0; J < Curbitmap.width; J + +)                            {                                 for(inti =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.heightI1) * Curbitmap.width + j] =temp; }                            }                        }                    }                    Else if(Curbitmap.pixelformat = =Pixelformat.format24bpprgb) {if(mirform.getmirror) {//Horizontal Mirroring processing                             for(inti =0; i < curbitmap.height; i++)                            {                                //The order of three bytes per pixel cannot be changed when mirroring horizontally, so this method cannot be used//For (int j = 0; J < Halfwidth; J + +)//{                                //    //The horizontal axis is symmetrical and the pixel value is exchanged on both sides.//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(intj =0; J < Halfwidth; J + +)                                {//every three bytes to form a pixel, order can not be chaotictemp = 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                        {                            //Vertical Mirroring processing                             for(inti =0; I < Curbitmap.width *3; i++)                            {                                 for(intj =0; J < Halfheight; J + +)                                {                                    //The axis of symmetry is perpendicular to the axes. Interchangeable pixel values on both sidestemp = 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 (Pixelvalu ES,0, ptr, bytes);                Curbitmap.unlockbits (Bmpdata);            } Invalidate (); }        }

Original:

Horizontal mirroring:

Vertical mirroring:

C # Digital Image processing (ix) Image mirroring

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.