C# 剪切图片空余的白边

  1. 原文出自:https://blog.csdn.net/rovecat/article/details/30230841
  2. 自己亲自试了可以
  3. /// <summary>  
  4.   /// 2014.6.12 剪去图片空余白边  
  5.   /// </summary>  
  6.   /// <param name="FilePath">源文件</param>  
  7.   /// <param name="WhiteBarRate">保留空白边比例</param>  
  8.   public static void CutImageWhitePart(string FilePath, int WhiteBarRate)  
  9.   {  
  10.       Bitmap bmp = new Bitmap(FilePath);  
  11.       int top = 0, left = 0;  
  12.       int right = bmp.Width, bottom = bmp.Height;  
  13.       Color white = Color.White;  
  14.       //寻找最上面的标线,从左(0)到右,从上(0)到下  
  15.       for (int i = 0; i < bmp.Height; i++)//行  
  16.       {  
  17.           bool find = false;  
  18.           for (int j = 0; j < bmp.Width; j++)//列  
  19.           {  
  20.               Color c = bmp.GetPixel(j, i);  
  21.               if (IsWhite(c))  
  22.               {  
  23.                   top = i;  
  24.                   find = true;  
  25.                   break;  
  26.               }  
  27.           }  
  28.           if (find) break;  
  29.       }  
  30.       //寻找最左边的标线,从上(top位)到下,从左到右  
  31.       for (int i = 0; i < bmp.Width; i++)//列  
  32.       {  
  33.           bool find = false;  
  34.           for (int j = top; j < bmp.Height; j++)//行  
  35.           {  
  36.               Color c = bmp.GetPixel(i, j);  
  37.               if (IsWhite(c))  
  38.               {  
  39.                   left = i;  
  40.                   find = true;  
  41.                   break;  
  42.               }  
  43.           }  
  44.           if (find) break; ;  
  45.       }  
  46.       ////寻找最下边标线,从下到上,从左到右  
  47.       for (int i = bmp.Height - 1; i >= 0; i--)//行  
  48.       {  
  49.           bool find = false;  
  50.           for (int j = left; j < bmp.Width; j++)//列  
  51.           {  
  52.               Color c = bmp.GetPixel(j, i);  
  53.               if (IsWhite(c))  
  54.               {  
  55.                   bottom = i;  
  56.                   find = true;  
  57.                   break;  
  58.               }  
  59.           }  
  60.           if (find) break;  
  61.       }  
  62.       //寻找最右边的标线,从上到下,从右往左  
  63.       for (int i = bmp.Width - 1; i >= 0; i--)//列  
  64.       {  
  65.           bool find = false;  
  66.           for (int j = 0; j <= bottom; j++)//行  
  67.           {  
  68.               Color c = bmp.GetPixel(i, j);  
  69.               if (IsWhite(c))  
  70.               {  
  71.                   right = i;  
  72.                   find = true;  
  73.                   break;  
  74.               }  
  75.           }  
  76.           if (find) break;  
  77.       }  
  78.       int iWidth = right - left;  
  79.       int iHeight = bottom - left;  
  80.       int blockWidth = Convert.ToInt32(iWidth * WhiteBarRate / 100);  
  81.       bmp = Cut(bmp, left - blockWidth, top - blockWidth, right - left + 2 * blockWidth, bottom - top + 2 * blockWidth);  
  82.       if (bmp != null)  
  83.       {  
  84.           bmp.Save(@"d:\" + bmp.GetHashCode().ToString() + ".jpg", ImageFormat.Jpeg);  
  85.       }  
  86.   }  
  87.   
  88.   
  89.   /// <summary>  
  90.   /// 2014.6.13 来源于网络的一个函数  
  91.   /// </summary>  
  92.   /// <param name="b"></param>  
  93.   /// <param name="StartX"></param>  
  94.   /// <param name="StartY"></param>  
  95.   /// <param name="iWidth"></param>  
  96.   /// <param name="iHeight"></param>  
  97.   /// <returns></returns>  
  98.   public static Bitmap Cut(Bitmap b, int StartX, int StartY, int iWidth, int iHeight)  
  99.   {  
  100.       if (b == null)  
  101.       {  
  102.           return null;  
  103.       }  
  104.       int w = b.Width;  
  105.       int h = b.Height;  
  106.       if (StartX >= w || StartY >= h)  
  107.       {  
  108.           return null;  
  109.       }  
  110.       if (StartX + iWidth > w)  
  111.       {  
  112.           iWidth = w - StartX;  
  113.       }  
  114.       if (StartY + iHeight > h)  
  115.       {  
  116.           iHeight = h - StartY;  
  117.       }  
  118.       try  
  119.       {  
  120.           Bitmap bmpOut = new Bitmap(iWidth, iHeight, PixelFormat.Format24bppRgb);  
  121.           Graphics g = Graphics.FromImage(bmpOut);  
  122.           g.DrawImage(b, new Rectangle(0, 0, iWidth, iHeight), new Rectangle(StartX, StartY, iWidth, iHeight), GraphicsUnit.Pixel);  
  123.           g.Dispose();  
  124.           return bmpOut;  
  125.       }  
  126.       catch  
  127.       {  
  128.           return null;  
  129.       }  
  130.   }  
  131.   
  132.   /// <summary>  
  133.   /// 2014.6.12 判断白色与否,非纯白色  
  134.   /// </summary>  
  135.   /// <param name="c"></param>  
  136.   /// <returns></returns>  
  137.   public static bool IsWhite(Color c)  
  138.   {  
  139.       if (c.R < 245 || c.G < 245 || c.B < 245)   
  140.           return true;  
  141.       else return false;  
  142.   }

猜你喜欢

转载自blog.csdn.net/xujingcheng123/article/details/79936922