C # Net remove the white border pictures

The codes for rewriting strongman, better

Use direct copy

Namespace:

using System.Drawing;

 

Code:

///  <Summary> 
        /// cropped pictures (one hundred removed edge)
         ///  </ Summary> 
        ///  <param name = "FilePath"> </ param> 
        public  static Bitmap CutImageWhitePart ( String FilePath) 
        { 
            Bitmap BMP = new new Bitmap (FilePath);
             // at the right and left 
            int top = 0 , left = 0 , = bmp.Width right, bottom = bmp.Height; 

            // find the top reticle, left (0) to the right, from (0) to the next 
            for ( int I = 0 ; I <bmp.Height; I ++) // row
            {
                bool find = false;
                for (int j = 0; j < bmp.Width; j++)//
                {
                    Color c = bmp.GetPixel(j, i);
                    if (!IsWhite(c))
                    {
                        top = i;
                        find = true;
                        break;
                    }
                }
                if (find)
                    break; 
            } 
            // Find the leftmost reticle, from (Top position) to the lower, left to right 
            for ( int I = 0 ; I <bmp.Width; I ++) // row 
            {
                 BOOL Find = to false ;
                 for ( int J = Top; J <bmp.Height; J ++) // row 
                { 
                    Color C = bmp.GetPixel (I, J);
                     IF (! iswhite (C)) 
                    { 
                        left = I; 
                        Find =to true ;
                         BREAK ; 
                    } 
                } 
                IF (Find)
                     BREAK ; 
            } 
            // find the lowermost marking, from bottom to top, from left to right 
            for ( int I = bmp.Height - . 1 ; I> = 0 ; i--) // row 
            {
                 BOOL Find = to false ;
                 for ( int J = left; J <bmp.Width; J ++) // row 
                { 
                    Color C =bmp.GetPixel (J, I);
                     IF (! iswhite (C)) 
                    { 
                        bottom = I; 
                        Find = to true ;
                         BREAK ; 
                    } 
                } 
                IF (Find)
                     BREAK ; 
            } 
            // find the marking most right, top to bottom , from right to left 
            for ( int I = bmp.Width - . 1 ; I> = 0 ; i--) // column 
            {
                 BOOL Find =to false ;
                 for ( int J = 0 ; J <= bottom; J ++) // row 
                { 
                    Color C = bmp.GetPixel (I, J);
                     IF (! iswhite (C)) 
                    { 
                        right = I; 
                        Find = to true ;
                         BREAK ; 
                    } 
                } 
                IF (Find)
                     BREAK ; 
            } 

            // portion cloning bitmap object.
            = CloneRect of the Rectangle new new of the Rectangle (left, Top, right - left, bottom - Top); 
            Bitmap cloneBitmap = bmp.Clone (cloneRect, bmp.PixelFormat); 
            bmp.Dispose (); 
            // cloneBitmap.Save (@ "d: \ 123.png ", ImageFormat.Png); 
            return cloneBitmap; 
        } 

        ///  <Summary> 
        /// determines whether pure white and transparent color (tolerance 10 points)
         ///  </ Summary> 
        public  static  BOOL iswhite (color C) 
        { 
            // pure white is transparent, RGB are pure white 255 
            IF (cA < 10 || (cR> 245 && cG> 245 && c.B > 245))
                return true;

            return false;
        }

transfer:

            Bitmap bitmap = CutImageWhitePart(@"C:\Users\Ping\Desktop\2.png");
            bitmap.Save(@"d:\123.png", ImageFormat.Png);

 

 

I have a question, please contact 971 931 543

Guess you like

Origin www.cnblogs.com/ping9719/p/11416176.html