首页 > 代码库 > .net图片自动裁剪白边函数案例

.net图片自动裁剪白边函数案例

1.项目要求上传白底的图片要进行裁剪白边,于是同事谢了个函数感觉很好用。

2.

  #region 剪切白边        /// <summary>        /// 剪切白边        /// </summary>        /// <param name="p"></param>        /// <returns></returns>        public static Image Crop(Image p)        {             int x, y ;//for use of X,Y Coordinates of pixels            Bitmap b = new Bitmap(p); //image needed to crop            Color c = new Color(); //pixel color for use of identifying if background            int intLeft=0;//furthest left X coordinate            int intRight=0;//furthest right X coordinate            int intBottom =0;//furthest to the bottom Y coordinate            int intTop =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 < 240 || c.G < 240 || c.B < 240)                        {                            //Determine if pixel is further left than the value we already have                            if (intLeft == 0 || intLeft > x )                            {                                intLeft = x;                            }                            //Determine if pixel is further to the top than the value we already have                            if (intTop == 0 || intTop > y )                            {                                intTop = y;                            }                            //Determine if pixel is further right than the value we already have                            if (intRight <= b.Width && intRight < x )                            {                                intRight = x;                            }                            //Determine if pixel is further to the bottom than the value we already have                            if (intBottom <= b.Height && intBottom < y )                            {                                intBottom = y;                            }                        }                    }                    x += 1;                }                y += 1;            }            int intNewWidth = intRight; //Establish width of new cropped image            int intNewHeight = intBottom; //Establish height of new cropped image            Bitmap imgCropped =new Bitmap(intNewWidth - intLeft + 2, intNewHeight - intTop + 2);            Graphics objGraphics = Graphics.FromImage(imgCropped);            //set the background color to white (you can choose what you like            objGraphics.Clear(System.Drawing.Color.Transparent);            int intStartTop = 1 - intTop; /// 40 + 5            int intStartLeft = 1 - intLeft; /// 40 + 5            //Draw the original image to your new cropped sized image            objGraphics.DrawImage(b, intStartLeft, intStartTop);            b.Dispose();            objGraphics.Dispose();            //return the Cropped image to the caller            return imgCropped;        }

 

.net图片自动裁剪白边函数案例