zoukankan      html  css  js  c++  java
  • .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;
            }
  • 相关阅读:
    2020 CCPC Wannafly Winter Camp Day2 K题 破忒头的匿名信(AC自动机+dp)
    CF1446C Xor Tree(01Trie)
    day04---系统重要文件
    day03--vi和vim快捷方式及操作系统目录介绍
    day02---虚拟机上网模式
    day01---操作系统安装环境准备
    django中一些快捷函数
    django中的一些装饰器用法
    auth模块的一些方法
    celery中异步延迟执行任务apply_anysc的用法
  • 原文地址:https://www.cnblogs.com/guozefeng/p/4178250.html
Copyright © 2011-2022 走看看