C# Net 去除图片白边

代码根据强人的进行改写,效果更好

直接拷贝使用

名称空间:

using System.Drawing;

代码:

/// <summary>
        /// 裁剪图片(去掉百边)
        /// </summary>
        /// <param name="FilePath"></param>
        public static Bitmap CutImageWhitePart(string FilePath)
        {
            Bitmap bmp = new Bitmap(FilePath);
            //上左右下
            int top = 0, left = 0, right = bmp.Width, bottom = bmp.Height;

            //寻找最上面的标线,从左(0)到右,从上(0)到下
            for (int i = 0; i < bmp.Height; i++)//
            {
                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;
            }
            //寻找最左边的标线,从上(top位)到下,从左到右
            for (int i = 0; i < bmp.Width; i++)//
            {
                bool find = false;
                for (int j = top; j < bmp.Height; j++)//
                {
                    Color c = bmp.GetPixel(i, j);
                    if (!IsWhite(c))
                    {
                        left = i;
                        find = true;
                        break;
                    }
                }
                if (find)
                    break;
            }
            //寻找最下边标线,从下到上,从左到右
            for (int i = bmp.Height - 1; i >= 0; i--)//
            {
                bool find = false;
                for (int j = left; j < bmp.Width; j++)//
                {
                    Color c = bmp.GetPixel(j, i);
                    if (!IsWhite(c))
                    {
                        bottom = i;
                        find = true;
                        break;
                    }
                }
                if (find)
                    break;
            }
            //寻找最右边的标线,从上到下,从右往左
            for (int i = bmp.Width - 1; i >= 0; i--)//
            {
                bool find = false;
                for (int j = 0; j <= bottom; j++)//
                {
                    Color c = bmp.GetPixel(i, j);
                    if (!IsWhite(c))
                    {
                        right = i;
                        find = true;
                        break;
                    }
                }
                if (find)
                    break;
            }

            //克隆位图对象的一部分。
            Rectangle cloneRect = new 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>
        /// 判断是否白色和纯透明色(10点的容差)
        /// </summary>
        public static bool IsWhite(Color c)
        {
            //纯透明也是白色,RGB都为255为纯白
            if (c.A < 10 || (c.R > 245 && c.G > 245 && c.B > 245))
                return true;

            return false;
        }

调用:

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

有问题可联系我 971931543

猜你喜欢

转载自www.cnblogs.com/ping9719/p/11416176.html