. NET picture auto crop white edge function case

Source: Internet
Author: User

1. The project requires uploading white-bottomed images to be trimmed with white edges, so colleagues thanks a function that feels very useful.

2.

  #regionCut white edges/// <summary>        ///cut White edges/// </summary>        /// <param name= "p" ></param>        /// <returns></returns>         Public Staticimage Crop (image p) {intx, y;//For use of x, y coordinates of pixelsBitmap B =NewBitmap (P);//image needed to cropColor C =NewColor ();//pixel color for use of identifying if background            intintleft=0;//furthest left X coordinate            intintright=0;//furthest right X coordinate            intIntbottom =0;//furthest to the bottom Y coordinate            intInttop =0; Y=0;  while(Y <b.height) {x=0;  while(x < B.width)//loop through pixels on X axis until end of image width{C= B.getpixel (x, y);//Get The color of the pixel                    if(C.R! =255&&c.r!=0&& C.G! =255&&c.g!=0&& C.B! =255&&c.b!=0)                    {                        if(C.R < -|| C.G < -|| C.b < -)                        {                            //determine if Pixel is further left than the value we already has                            if(Intleft = =0|| Intleft >x) {intleft=x; }                            //determine if pixel is further to the top than the value we already has                            if(Inttop = =0|| Inttop >y) {inttop=y; }                            //determine if pixel is further right than the value we already has                            if(Intright <= b.width && Intright <x) {intright=x; }                            //determine if pixel is further to the bottom than the value we already has                            if(Intbottom <= b.height && Intbottom <y) {intbottom=y; } }} X+=1; } y+=1; }            intIntnewwidth = Intright;//establish width of new cropped image            intIntnewheight = Intbottom;//establish height of new cropped imageBitmap imgcropped =NewBitmap (Intnewwidth-intleft +2, Intnewheight-inttop +2); Graphics Objgraphics=graphics.fromimage (imgcropped); //set the background color to white (you can chooseobjgraphics.clear (System.Drawing.Color.Transparent); intIntstarttop =1-Inttop;///+ 5            intIntstartleft =1-Intleft;///+ 5            //Draw The original image to your new cropped sized imageObjgraphics.drawimage (b, Intstartleft, intstarttop);            B.dispose ();            Objgraphics.dispose (); //return the cropped image to the caller            returnimgcropped; }

. NET picture auto crop white edge function case

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.