C#彩色扭曲验证码

2020-12-13 14:56

阅读:455

标签:des   style   blog   http   io   color   os   ar   使用   

该验证码生成类集合了网上大部分的验证码生成类的精华,博采众长并多次改进,现在已经形成了可在生产环节中使用的验证码。

该验证码加入了背景噪点,背景噪点曲线和直线,背景噪点文字以及扭曲,调暗,模糊等。完全可以实现防识别。

按照国际惯例先贴张效果图吧:

soscw.com,搜素材

 

 #region 验证码生成类
        /// 
        /// 验证码生成类
        /// 
        public class ValidateCode
        {
            #region 定义和初始化配置字段
            //用户存取验证码字符串
            public string validationCode = String.Empty;
            //生成的验证码字符串
            public char[] chars = null;
            /// 
            /// 获取系统生成的随机验证码
            /// 
            public String ValidationCode
            {
                get { return validationCode; }
            }
            private Int32 validationCodeCount = 4;
            /// 
            /// 获取和设置验证码字符串的长度
            /// 
            public Int32 ValidationCodeCount
            {
                get { return validationCodeCount; }
                set { validationCodeCount = value; }
            }
            Graphics dc = null;
            private int bgWidth = 130;
            /// 
            /// 验证码的宽度,默认为80
            /// 
            public Int32 Width
            {
                get { return bgWidth; }
                set { bgWidth = value; }
            }

            private int bgHeight = 40;
            /// 
            /// 验证码的高度,默认为40
            /// 
            public Int32 Height
            {
                get { return bgHeight; }
                set { bgHeight = value; }
            }
            /* private string[] fontFace = { "Verdana", "Microsoft Sans Serif", "Comic Sans MS", "Arial", "宋体" };
             /// 
             /// 验证码字体列表,默认为{ "Verdana", "Microsoft Sans Serif", "Comic Sans MS", "Arial", "宋体" }
             /// 
             public String[] FontFace
             {
                 get { return fontFace; }
                 set { fontFace = value; }
             }*/

            private int fontMinSize = 20;
            /// 
            /// 验证码字体的最小值,默认为15,建议不小于15像素
            /// 
            public Int32 FontMinSize
            {
                get { return fontMinSize; }
                set { fontMinSize = value; }
            }
            private Int32 fontMaxSize = 25;
            /// 
            /// 验证码字体的最大值,默认为20
            /// 
            public Int32 FontMaxSize
            {
                get { return fontMaxSize; }
                set { fontMaxSize = value; }
            }
            private Color[] fontColor = { };
            /// 
            /// 验证码字体的颜色,默认为系统自动生成字体颜色
            /// 
            public Color[] FontColor
            {
                get { return fontColor; }
                set { fontColor = value; }
            }
            private Color backColor = Color.FromArgb(243, 255, 255);
            /// 
            /// 验证码的背景色,默认为Color.FromArgb(243, 251, 254)
            /// 
            public Color BackgroundColor
            {
                get { return backColor; }
                set { backColor = value; }
            }
            private Int32 bezierCount = 3;
            /// 
            /// 贝塞尔曲线的条数,默认为3条
            /// 
            public Int32 BezierCount
            {
                get { return bezierCount; }
                set { bezierCount = value; }
            }
            private Int32 lineCount = 3;
            /// 
            /// 直线条数,默认为3条
            /// 
            public Int32 LineCount
            {
                get { return lineCount; }
                set { lineCount = value; }
            }
            Random random = new Random();

            private String charCollection = "2,3,4,5,6,7,8,9,a,s,d,f,g,h,z,c,v,b,n,m,k,q,w,e,r,t,y,u,p,A,S,D,F,G,H,Z,C,V,B,N,M,K,Q,W,E,R,T,Y,U,P"; //定义验证码字符及出现频次 ,避免出现0 o j i l 1 x;  
            /// 
            /// 随机字符串列表,请使用英文状态下的逗号分隔。
            /// 
            public String CharCollection
            {
                get { return charCollection; }
                set { charCollection = value; }
            }
            private Int32 intCount = 4;
            /// 
            /// 验证码字符串个数,默认为4个字符
            /// 
            public Int32 IntCount
            {
                get { return intCount; }
                set { intCount = value; }
            }
            private Boolean isPixel = true;
            /// 
            /// 是否添加噪点,默认添加,噪点颜色为系统随机生成。
            /// 
            public Boolean IsPixel
            {
                get { return isPixel; }
                set { isPixel = value; }
            }
            private Boolean isRandString = true;
            /// 
            /// 是否添加随机噪点字符串,默认添加
            /// 
            public Boolean IsRandString
            {
                get { return isRandString; }
                set { isRandString = value; }
            }
            /// 
            /// 随机背景字符串的个数
            /// 
            public Int32 RandomStringCount
            {
                get;
                set;
            }
            private Int32 randomStringFontSize = 9;
            /// 
            /// 随机背景字符串的大小
            /// 
            public Int32 RandomStringFontSize
            {
                get { return randomStringFontSize; }
                set { randomStringFontSize = value; }
            }
            /// 
            /// 是否对图片进行扭曲
            /// 
            public Boolean IsTwist
            {
                get;
                set;
            }
            /// 
            /// 边框样式
            /// 
            public enum BorderStyle
            {
                /// 
                /// 无边框
                /// 
                None,
                /// 
                /// 矩形边框
                /// 
                Rectangle,
                /// 
                /// 圆角边框
                /// 
                RoundRectangle
            }
            private Int32 rotationAngle = 40;
            /// 
            /// 验证码字符串随机转动的角度的最大值
            /// 
            public Int32 RotationAngle
            {
                get { return rotationAngle; }
                set { rotationAngle = value; }
            }
            /// 
            /// 设置或获取边框样式
            /// 
            public BorderStyle Border
            {
                get;
                set;
            }
            private Point[] strPoint = null;


            private Double gaussianDeviation = 0;
            /// 
            /// 对验证码图片进行高斯模糊的阀值,如果设置为0,则不对图片进行高斯模糊,该设置可能会对图片处理的性能有较大影响
            /// 
            public Double GaussianDeviation
            {
                get { return gaussianDeviation; }
                set { gaussianDeviation = value; }
            }
            private Int32 brightnessValue = 0;
            /// 
            /// 对图片进行暗度和亮度的调整,如果该值为0,则不调整。该设置会对图片处理性能有较大影响
            /// 
            public Int32 BrightnessValue
            {
                get { return brightnessValue; }
                set { brightnessValue = value; }
            }
            #endregion
            /// 
            /// 构造函数,用于初始化常用变量
            /// 
            public void DrawValidationCode()
            {
                random = new Random(Guid.NewGuid().GetHashCode());
                strPoint = new Point[validationCodeCount + 1];
                if (gaussianDeviation 
            /// 生成验证码
            /// 
            /// 用于存储图片的一般字节序列
            public byte[] CreateImage(string code)
            {
                MemoryStream target = new MemoryStream();
                Bitmap bit = new Bitmap(bgWidth + 1, bgHeight + 1);
                //写字符串
                dc = Graphics.FromImage(bit);
                dc.SmoothingMode = SmoothingMode.HighQuality;
                dc.TextRenderingHint = TextRenderingHint.ClearTypeGridFit; ;
                dc.InterpolationMode = InterpolationMode.HighQualityBilinear;
                dc.CompositingQuality = CompositingQuality.HighQuality;

                try
                {
                    dc.Clear(Color.White);
                    DrawValidationCode();
                    dc.DrawImageUnscaled(DrawBackground(), 0, 0);
                    dc.DrawImageUnscaled(DrawRandomString(code), 0, 0);
                    //对图片文字进行扭曲
                    bit = AdjustRippleEffect(bit, 5);
                    //对图片进行高斯模糊
                    if (gaussianDeviation > 0)
                    {
                        Gaussian gau = new Gaussian();
                        bit = gau.FilterProcessImage(gaussianDeviation, bit);
                    }
                    //进行暗度和亮度处理
                    if (brightnessValue != 0)
                    {
                        //对图片进行调暗处理
                        bit = AdjustBrightness(bit, brightnessValue);
                    }
                    bit.Save(target, ImageFormat.Jpeg);
                    //输出图片流
                    return target.ToArray();
                }
                finally
                {
                    //brush.Dispose();
                    bit.Dispose();
                    dc.Dispose();
                }
            }

            #region 画验证码背景,例如,增加早点,添加曲线和直线等
            /// 
            /// 画验证码背景,例如,增加早点,添加曲线和直线等
            /// 
            /// 
            private Bitmap DrawBackground()
            {
                Bitmap bit = new Bitmap(bgWidth + 1, bgHeight + 1);
                Graphics g = Graphics.FromImage(bit);
                g.SmoothingMode = SmoothingMode.HighQuality;

                g.Clear(Color.White);
                Rectangle rectangle = new Rectangle(0, 0, bgWidth, bgHeight);
                Brush brush = new SolidBrush(backColor);
                g.FillRectangle(brush, rectangle);

                //画噪点
                if (isPixel)
                {
                    g.DrawImageUnscaled(DrawRandomPixel(30), 0, 0);
                }
                g.DrawImageUnscaled(DrawRandBgString(), 0, 0);


                //画曲线
                //g.DrawImageUnscaled(DrawRandomBezier(bezierCount), 0, 0);
                ////画直线
                //g.DrawImageUnscaled(DrawRandomLine(lineCount), 0, 0);

                //dc.DrawImageUnscaled(DrawStringline(), 0, 0);
                if (Border == BorderStyle.Rectangle)
                {
                    //绘制边框
                    g.DrawRectangle(new Pen(Color.FromArgb(90, 87, 46)), 0, 0, bgWidth, bgHeight);
                }
                else if (Border == BorderStyle.RoundRectangle)
                {
                    //画圆角
                    DrawRoundRectangle(g, rectangle, Color.FromArgb(90, 87, 46), 1, 3);
                }

                return bit;

            }
            #endregion

            #region 画正弦曲线
            private Bitmap DrawTwist(Bitmap bmp, Int32 tWidth, Int32 tHeight, float angle, Color color)
            {
                //为了方便查看效果,在这里我定义了一个常量。
                //它在定义数组的长度和for循环中都要用到。
                int size = bgWidth;

                double[] x = new double[size];
                Bitmap b = new Bitmap(bmp.Width, bmp.Height);
                b.MakeTransparent();
                Graphics graphics = Graphics.FromImage(b);
                Pen pen = new Pen(color);

                //画正弦曲线的横轴间距参数。建议所用的值应该是 正数且是2的倍数。
                //在这里采用2。
                int val = 2;

                float temp = 0.0f;

                //把画布下移100。为什么要这样做,只要你把这一句给注释掉,运行一下代码,
                //你就会明白是为什么?
                graphics.TranslateTransform(0, 100);
                graphics.SmoothingMode = SmoothingMode.HighQuality;
                graphics.PixelOffsetMode = PixelOffsetMode.HighQuality;
                for (int i = 0; i 
            /// 正弦曲线Wave扭曲图片
            /// 
            /// 图片路径
            /// 如果扭曲则选择为True
            /// 波形的幅度倍数,越大扭曲的程度越高,一般为3
            /// 波形的起始相位,取值区间[0-2*PI)
            /// 
            public Bitmap TwistImage(Bitmap srcBmp, bool bXDir, double dMultValue, double dPhase)
            {
                System.Drawing.Bitmap destBmp = new Bitmap(srcBmp.Width, srcBmp.Height);
                double PI2 = 6.283185307179586476925286766559;
                // 将位图背景填充为白色
                System.Drawing.Graphics graph = System.Drawing.Graphics.FromImage(destBmp);
                graph.FillRectangle(new SolidBrush(System.Drawing.Color.White), 0, 0, destBmp.Width, destBmp.Height);
                graph.Dispose();

                double dBaseAxisLen = bXDir ? (double)destBmp.Height : (double)destBmp.Width;

                for (int i = 0; i = 0 && nOldX = 0 && nOldY 
            /// 图片任意角度旋转
            /// 
            /// 原始图Bitmap
            /// 旋转角度
            /// 背景色
            /// 输出Bitmap
            public static Bitmap KiRotate(Bitmap bmp, float angle, Color bkColor)
            {
                int w = bmp.Width;
                int h = bmp.Height;

                PixelFormat pf;

                if (bkColor == Color.Transparent)
                {
                    pf = PixelFormat.Format32bppArgb;
                }
                else
                {
                    pf = bmp.PixelFormat;
                }

                Bitmap tmp = new Bitmap(w, h, pf);
                Graphics g = Graphics.FromImage(tmp);
                g.Clear(bkColor);
                g.DrawImageUnscaled(bmp, 1, 1);
                g.Dispose();

                GraphicsPath path = new GraphicsPath();
                path.AddRectangle(new RectangleF(0f, 0f, w, h));
                Matrix mtrx = new Matrix();
                mtrx.Rotate(angle);
                RectangleF rct = path.GetBounds(mtrx);

                Bitmap dst = new Bitmap((int)rct.Width, (int)rct.Height, pf);
                g = Graphics.FromImage(dst);
                g.Clear(bkColor);
                g.TranslateTransform(-rct.X, -rct.Y);
                g.RotateTransform(angle);
                g.InterpolationMode = InterpolationMode.HighQualityBilinear;
                g.DrawImageUnscaled(tmp, 0, 0);
                g.Dispose();
                tmp.Dispose();

                return dst;
            }
            #endregion

            #region 随机生成贝塞尔曲线
            /// 
            /// 随机生成贝塞尔曲线
            /// 
            /// 一个图片的实例
            /// 线条数量
            /// 
            public Bitmap DrawRandomBezier(Int32 lineNum)
            {
                Bitmap b = new Bitmap(bgWidth, bgHeight);
                b.MakeTransparent();
                Graphics g = Graphics.FromImage(b);
                g.Clear(Color.Transparent);
                g.SmoothingMode = SmoothingMode.HighQuality;
                g.PixelOffsetMode = PixelOffsetMode.HighQuality;

                GraphicsPath gPath1 = new GraphicsPath();
                Int32 lineRandNum = random.Next(lineNum);

                for (int i = 0; i 
            /// 画直线
            /// 
            /// 一个bmp实例
            /// 线条个数
            /// 
            public Bitmap DrawRandomLine(Int32 lineNum)
            {
                if (lineNum 
            /// 画随机噪点
            /// 
            /// 噪点的百分比
            /// 
            public Bitmap DrawRandomPixel(Int32 pixNum)
            {
                Bitmap b = new Bitmap(bgWidth, bgHeight);
                b.MakeTransparent();
                Graphics graph = Graphics.FromImage(b);
                graph.SmoothingMode = SmoothingMode.HighQuality;
                graph.InterpolationMode = InterpolationMode.HighQualityBilinear;

                //画噪点 
                for (int i = 0; i 
            /// 画随机字符串中间连线
            /// 
            /// 
            private Bitmap DrawStringline()
            {
                Bitmap b = new Bitmap(bgWidth, bgHeight);
                b.MakeTransparent();
                Graphics g = Graphics.FromImage(b);
                g.SmoothingMode = SmoothingMode.AntiAlias;

                Point[] p = new Point[validationCodeCount];
                for (int i = 0; i 
            /// 写入验证码的字符串
            /// 
            private Bitmap DrawRandomString(string Code)
            {
                if (fontMaxSize >= (bgHeight / 5) * 4) throw new ArgumentException("字体最大值参数FontMaxSize与验证码高度相近,这会导致描绘验证码字符串时出错,请重新设置参数!");
                Bitmap b = new Bitmap(bgWidth, bgHeight);
                b.MakeTransparent();
                Graphics g = Graphics.FromImage(b);

                g.Clear(Color.Transparent);
                g.PixelOffsetMode = PixelOffsetMode.Half;
                g.SmoothingMode = SmoothingMode.HighQuality;
                g.TextRenderingHint = TextRenderingHint.SingleBitPerPixelGridFit;
                g.InterpolationMode = InterpolationMode.HighQualityBilinear;

                chars = Code.ToCharArray();//拆散字符串成单字符数组
                validationCode = chars.ToString();

                //设置字体显示格式
                StringFormat format = new StringFormat(StringFormatFlags.NoClip);
                format.Alignment = StringAlignment.Center;
                format.LineAlignment = StringAlignment.Center;
                FontFamily f = new FontFamily(GenericFontFamilies.Monospace);


                Int32 charNum = chars.Length;

                Point sPoint = new Point();
                Int32 fontSize = 12;
                for (int i = 0; i  0 && (point.X - sPoint.X  (bgWidth - textFontSize / 2))
                    {
                        point.X = bgWidth - textFontSize / 2;
                    }

                    sPoint = point;

                    float angle = random.Next(-rotationAngle, rotationAngle);//转动的度数
                    g.TranslateTransform(point.X, point.Y);//移动光标到指定位置
                    g.RotateTransform(angle);

                    //设置渐变画刷  
                    Rectangle myretang = new Rectangle(0, 1, Convert.ToInt32(textFont.Size), Convert.ToInt32(textFont.Size));
                    Color c = GetRandomDeepColor();
                    LinearGradientBrush mybrush2 = new LinearGradientBrush(myretang, c, GetLightColor(c, 120), random.Next(180));

                    g.DrawString(chars[i].ToString(), textFont, mybrush2, 1, 1, format);

                    g.RotateTransform(-angle);//转回去
                    g.TranslateTransform(-point.X, -point.Y);//移动光标到指定位置,每个字符紧凑显示,避免被软件识别

                    strPoint[i] = point;

                    textFont.Dispose();
                    mybrush2.Dispose();
                }
                return b;
            }
            #endregion

            #region 画干扰背景文字
            /// 
            /// 画背景干扰文字
            /// 
            /// 
            private Bitmap DrawRandBgString()
            {
                Bitmap b = new Bitmap(bgWidth, bgHeight);
                String[] randStr = { "a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z", "A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z" };
                b.MakeTransparent();
                Graphics g = Graphics.FromImage(b);

                g.Clear(Color.Transparent);
                g.PixelOffsetMode = PixelOffsetMode.HighQuality;
                g.SmoothingMode = SmoothingMode.HighQuality;
                g.TextRenderingHint = TextRenderingHint.AntiAlias;
                g.InterpolationMode = InterpolationMode.HighQualityBilinear;

                //设置字体显示格式
                StringFormat format = new StringFormat(StringFormatFlags.NoClip);
                format.Alignment = StringAlignment.Center;
                format.LineAlignment = StringAlignment.Center;

                FontFamily f = new FontFamily(GenericFontFamilies.Serif);
                Font textFont = new Font(f, randomStringFontSize, FontStyle.Underline);

                int randAngle = 60; //随机转动角度

                for (int i = 0; i 
            /// 生成随机字符串    
            /// 
            /// 
            public string GetRandomString(Int32 textLength)
            {
                string[] randomArray = charCollection.Split(‘,‘); //将字符串生成数组     
                int arrayLength = randomArray.Length;
                string randomString = "";
                for (int i = 0; i 
            /// 根据指定长度,返回随机验证码
            /// 
            /// 制定长度
            /// 随即验证码
            public string Next(int length)
            {
                this.validationCode = GetRandomCode(length);
                return this.validationCode;
            }
            #endregion

            #region 内部方法:返回指定长度的随机验证码字符串
            /// 
            /// 根据指定大小返回随机验证码
            /// 
            /// 字符串长度
            /// 随机字符串
            private string GetRandomCode(int length)
            {
                StringBuilder sb = new StringBuilder(6);

                for (int i = 0; i 
            /// 产生0-9A-Z的随机字符代码
            /// 
            /// 字符代码
            private int RandomAZ09()
            {
                int result = 48;
                Random ram = new Random();
                int i = ram.Next(2);

                switch (i)
                {
                    case 0:
                        result = ram.Next(48, 58);
                        break;
                    case 1:
                        result = ram.Next(65, 91);
                        break;
                }

                return result;
            }

            /// 
            /// 返回一个随机点,该随机点范围在验证码背景大小范围内
            /// 
            /// Point对象
            private Point RandomPoint()
            {
                Random ram = new Random();
                Point point = new Point(ram.Next(this.bgWidth), ram.Next(this.bgHeight));
                return point;
            }
            #endregion

            #region 随机生成颜色值
            /// 
            /// 生成随机深颜色
            /// 
            /// 
            public Color GetRandomDeepColor()
            {
                int nRed, nGreen, nBlue;    // nBlue,nRed  nGreen 相差大一点 nGreen 小一些
                //int high = 255;       
                int redLow = 160;
                int greenLow = 100;
                int blueLow = 160;
                nRed = random.Next(redLow);
                nGreen = random.Next(greenLow);
                nBlue = random.Next(blueLow);
                Color color = Color.FromArgb(nRed, nGreen, nBlue);
                return color;
            }

            /// 
            /// 生成随机浅颜色
            /// 
            /// randomColor
            public Color GetRandomLightColor()
            {
                int nRed, nGreen, nBlue;    //越大颜色越浅
                int low = 180;           //色彩的下限
                int high = 255;          //色彩的上限      
                nRed = random.Next(high) % (high - low) + low;
                nGreen = random.Next(high) % (high - low) + low;
                nBlue = random.Next(high) % (high - low) + low;
                Color color = Color.FromArgb(nRed, nGreen, nBlue);
                return color;
            }
            /// 
            /// 生成随机颜色值
            /// 
            /// 
            public Color GetRandomColor()
            {
                int nRed, nGreen, nBlue;    //越大颜色越浅
                int low = 10;           //色彩的下限
                int high = 255;          //色彩的上限    
                nRed = random.Next(high) % (high - low) + low;
                nGreen = random.Next(high) % (high - low) + low;
                nBlue = random.Next(high) % (high - low) + low;
                Color color = Color.FromArgb(nRed, nGreen, nBlue);
                return color;
            }
            /// 
            /// 获取与当前颜色值相加后的颜色
            /// 
            /// 
            /// 
            public Color GetLightColor(Color c, Int32 value)
            {
                int nRed = c.R, nGreen = c.G, nBlue = c.B;    //越大颜色越浅
                if (nRed + value  0)
                {
                    nRed = c.R + 40;
                }
                if (nGreen + value  0)
                {
                    nGreen = c.G + 40;
                }
                if (nBlue + value  0)
                {
                    nBlue = c.B + 40;
                }
                Color color = Color.FromArgb(nRed, nGreen, nBlue);
                return color;
            }
            #endregion

            #region 合并图片
            ///        
            /// 合并图片        
            ///         
            ///         
            ///         
            private Bitmap MergerImg(params Bitmap[] maps)
            {
                int i = maps.Length;
                if (i == 0)
                    throw new Exception("图片数不能够为0");
                //创建要显示的图片对象,根据参数的个数设置宽度            
                Bitmap backgroudImg = new Bitmap(i * 12, 16);
                Graphics g = Graphics.FromImage(backgroudImg);
                //清除画布,背景设置为白色            
                g.Clear(System.Drawing.Color.White);
                for (int j = 0; j 
            /// 生成不重复的随机数,该函数会消耗大量系统资源
            /// 
            /// 
            private static int GetRandomSeed()
            {
                byte[] bytes = new byte[4];
                System.Security.Cryptography.RNGCryptoServiceProvider rng = new System.Security.Cryptography.RNGCryptoServiceProvider();
                rng.GetBytes(bytes);
                return BitConverter.ToInt32(bytes, 0);
            }
            #endregion

            #region 缩放图片
            /// 
            /// 缩放图片
            /// 
            /// 原始Bitmap
            /// 新的宽度
            /// 新的高度
            /// 缩放质量
            /// 处理以后的图片
            public static Bitmap KiResizeImage(Bitmap bmp, int newW, int newH, InterpolationMode Mode)
            {
                try
                {
                    Bitmap b = new Bitmap(newW, newH);
                    Graphics g = Graphics.FromImage(b);
                    // 插值算法的质量
                    g.InterpolationMode = Mode;
                    g.DrawImage(bmp, new Rectangle(0, 0, newW, newH), new Rectangle(0, 0, bmp.Width, bmp.Height), GraphicsUnit.Pixel);
                    g.Dispose();
                    return b;
                }
                catch
                {
                    return null;
                }
            }
            #endregion

            #region 绘制圆角矩形
            /// 
            /// C# GDI+ 绘制圆角矩形
            /// 
            /// Graphics 对象
            /// Rectangle 对象,圆角矩形区域
            /// 边框颜色
            /// 边框宽度
            /// 圆角半径
            private static void DrawRoundRectangle(Graphics g, Rectangle rectangle, Color borderColor, float borderWidth, int r)
            {
                // 如要使边缘平滑,请取消下行的注释
                g.SmoothingMode = SmoothingMode.HighQuality;

                // 由于边框也需要一定宽度,需要对矩形进行修正
                //rectangle = new Rectangle(rectangle.X, rectangle.Y, rectangle.Width, rectangle.Height);
                Pen p = new Pen(borderColor, borderWidth);
                // 调用 getRoundRectangle 得到圆角矩形的路径,然后再进行绘制
                g.DrawPath(p, getRoundRectangle(rectangle, r));
            }
            #endregion

            #region 根据普通矩形得到圆角矩形的路径
            /// 
            /// 根据普通矩形得到圆角矩形的路径
            /// 
            /// 原始矩形
            /// 半径
            /// 图形路径
            private static GraphicsPath getRoundRectangle(Rectangle rectangle, int r)
            {
                int l = 2 * r;
                // 把圆角矩形分成八段直线、弧的组合,依次加到路径中
                GraphicsPath gp = new GraphicsPath();
                gp.AddLine(new Point(rectangle.X + r, rectangle.Y), new Point(rectangle.Right - r, rectangle.Y));
                gp.AddArc(new Rectangle(rectangle.Right - l, rectangle.Y, l, l), 270F, 90F);

                gp.AddLine(new Point(rectangle.Right, rectangle.Y + r), new Point(rectangle.Right, rectangle.Bottom - r));
                gp.AddArc(new Rectangle(rectangle.Right - l, rectangle.Bottom - l, l, l), 0F, 90F);

                gp.AddLine(new Point(rectangle.Right - r, rectangle.Bottom), new Point(rectangle.X + r, rectangle.Bottom));
                gp.AddArc(new Rectangle(rectangle.X, rectangle.Bottom - l, l, l), 90F, 90F);

                gp.AddLine(new Point(rectangle.X, rectangle.Bottom - r), new Point(rectangle.X, rectangle.Y + r));
                gp.AddArc(new Rectangle(rectangle.X, rectangle.Y, l, l), 180F, 90F);
                return gp;
            }
            #endregion

            #region 柔化
            ///
            /// 柔化
            /// 
            /// 原始图
            /// 输出图
            public static Bitmap KiBlur(Bitmap b)
            {

                if (b == null)
                {
                    return null;
                }

                int w = b.Width;
                int h = b.Height;

                try
                {

                    Bitmap bmpRtn = new Bitmap(w, h, PixelFormat.Format24bppRgb);

                    BitmapData srcData = b.LockBits(new Rectangle(0, 0, w, h), ImageLockMode.ReadOnly, PixelFormat.Format24bppRgb);
                    BitmapData dstData = bmpRtn.LockBits(new Rectangle(0, 0, w, h), ImageLockMode.WriteOnly, PixelFormat.Format24bppRgb);

                    unsafe
                    {
                        byte* pIn = (byte*)srcData.Scan0.ToPointer();
                        byte* pOut = (byte*)dstData.Scan0.ToPointer();
                        int stride = srcData.Stride;
                        byte* p;

                        for (int y = 0; y 
            /// 红色滤镜
            /// 
            /// Bitmap
            /// 阀值 -255~255
            /// 
            public System.Drawing.Bitmap AdjustToRed(System.Drawing.Bitmap bitmap, int threshold)
            {
                for (int y = 0; y 
            /// 绿色滤镜
            /// 
            /// 一个图片实例
            /// 阀值 -255~+255
            /// 
            public System.Drawing.Bitmap AdjustToGreen(System.Drawing.Bitmap bitmap, int threshold)
            {
                for (int y = 0; y  255) pG = 255;
                        if (pG 
            /// 蓝色滤镜
            /// 
            /// 一个图片实例
            /// 阀值 -255~255
            /// 
            public System.Drawing.Bitmap AdjustToBlue(System.Drawing.Bitmap bitmap, int threshold)
            {
                for (int y = 0; y  255) pB = 255;
                        if (pB 
            /// 调整 RGB 色调
            /// 
            /// 
            /// 红色阀值
            /// 蓝色阀值
            /// 绿色阀值
            /// 
            public System.Drawing.Bitmap AdjustToCustomColor(System.Drawing.Bitmap bitmap, int thresholdRed, int thresholdGreen, int thresholdBlue)
            {
                for (int y = 0; y  255) pG = 255;
                        if (pG  255) pR = 255;
                        if (pR  255) pB = 255;
                        if (pB 
            /// 图片去色(图片黑白化)
            /// 
            /// 一个需要处理的图片
            /// 
            public static Bitmap MakeGrayscale(Bitmap original)
            {
                //create a blank bitmap the same size as original
                Bitmap newBitmap = new Bitmap(original.Width, original.Height);

                //get a graphics object from the new image
                Graphics g = Graphics.FromImage(newBitmap);
                g.SmoothingMode = SmoothingMode.HighQuality;
                //create the grayscale ColorMatrix
                ColorMatrix colorMatrix = new ColorMatrix(new float[][] 
                              {
                                 new float[] {.3f, .3f, .3f, 0, 0},
                                 new float[] {.59f, .59f, .59f, 0, 0},
                                 new float[] {.11f, .11f, .11f, 0, 0},
                                 new float[] {0, 0, 0, 1, 0},
                                 new float[] {0, 0, 0, 0, 1}
                              });

                //create some image attributes
                ImageAttributes attributes = new ImageAttributes();

                //set the color matrix attribute
                attributes.SetColorMatrix(colorMatrix);

                //draw the original image on the new image
                //using the grayscale color matrix
                g.DrawImage(original, new Rectangle(0, 0, original.Width, original.Height),
                   0, 0, original.Width, original.Height, GraphicsUnit.Pixel, attributes);

                //dispose the Graphics object
                g.Dispose();
                return newBitmap;
            }
            #endregion

            #region 增加或減少亮度
            /// 
            /// 增加或減少亮度
            /// 
            /// System.Drawing.Image Source 
            /// 0~255
            /// 
            public System.Drawing.Bitmap AdjustBrightness(System.Drawing.Image img, int valBrightness)
            {
                // 讀入欲轉換的圖片並轉成為 Bitmap
                System.Drawing.Bitmap bitmap = new System.Drawing.Bitmap(img);

                for (int y = 0; y  255) ? 255 : pixel.R + valBrightness)  255) ? 255 : pixel.R + valBrightness);
                        var pG = ((pixel.G + valBrightness > 255) ? 255 : pixel.G + valBrightness)  255) ? 255 : pixel.G + valBrightness);
                        var pB = ((pixel.B + valBrightness > 255) ? 255 : pixel.B + valBrightness)  255) ? 255 : pixel.B + valBrightness);

                        // 將改過的 RGB 寫回
                        System.Drawing.Color newColor = System.Drawing.Color.FromArgb(pixel.A, pR, pG, pB);

                        bitmap.SetPixel(x, y, newColor);

                    }
                }
                // 回傳結果
                return bitmap;
            }
            #endregion

            #region 浮雕效果
            /// 
            /// 浮雕效果
            /// 
            /// 一个图片实例
            /// 
            public Bitmap AdjustToStone(Bitmap src)
            {
                // 依照 Format24bppRgb 每三个表示一 Pixel 0: 蓝 1: 绿 2: 红
                BitmapData bitmapData = src.LockBits(new Rectangle(0, 0, src.Width, src.Height), ImageLockMode.ReadWrite, PixelFormat.Format24bppRgb);

                unsafe
                {
                    // 抓住第一个 Pixel 第一个数值
                    byte* p = (byte*)(void*)bitmapData.Scan0;

                    // 跨步值 - 宽度 *3 可以算出畸零地 之后跳到下一行
                    int nOffset = bitmapData.Stride - src.Width * 3;

                    for (int y = 0; y  255 ? 255 : r;
                            p[2] = (byte)r;

                            g = Math.Abs(p[1] - q[1] + 128);
                            g = g  255 ? 255 : g;
                            p[1] = (byte)g;

                            b = Math.Abs(p[0] - q[0] + 128);
                            b = b  255 ? 255 : b;
                            p[0] = (byte)b;

                            // 跳去下一个 Pixel
                            p += 3;

                        }
                        // 跨越畸零地
                        p += nOffset;
                    }
                }
                src.UnlockBits(bitmapData);
                return src;
            }
            #endregion

            #region 水波纹效果
            /// 
            /// 水波纹效果
            /// 
            /// 
            /// 坡度
            /// www.it165.net
            /// 
            public Bitmap AdjustRippleEffect(Bitmap src, short nWave)
            {

                int nWidth = src.Width;
                int nHeight = src.Height;

                // 透过公式进行水波纹的採样

                PointF[,] fp = new PointF[nWidth, nHeight];
            
                Point[,] pt = new Point[nWidth, nHeight];

                Point mid = new Point();
                mid.X = nWidth / 2;
                mid.Y = nHeight / 2;

                double newX, newY;
                double xo, yo;

                //先取样将水波纹座标跟RGB取出
                for (int x = 0; x  0 && newX  0 && newY = 0 && yOffset = 0 && xOffset 
            /// 调整曝光度值
            /// 
            /// 原图
            /// 
            /// 
            /// 
            /// 
            public Bitmap AdjustGamma(Bitmap src, double r, double g, double b)
            {
                // 判断是不是在0.2~5 之间
                r = Math.Min(Math.Max(0.2, r), 5);
                g = Math.Min(Math.Max(0.2, g), 5);
                b = Math.Min(Math.Max(0.2, b), 5);

                // 依照 Format24bppRgb 每三个表示一 Pixel 0: 蓝 1: 绿 2: 红
                BitmapData bitmapData = src.LockBits(new Rectangle(0, 0, src.Width, src.Height), ImageLockMode.ReadWrite, PixelFormat.Format24bppRgb);

                unsafe
                {
                    // 抓住第一个 Pixel 第一个数值
                    byte* p = (byte*)(void*)bitmapData.Scan0;

                    // 跨步值 - 宽度 *3 可以算出畸零地 之后跳到下一行
                    int nOffset = bitmapData.Stride - src.Width * 3;

                    for (int y = 0; y 
            /// 高对比,对过深的颜色调浅,过浅的颜色调深。
            /// 
            /// 
            ///  高对比程度 -100~100
            /// 
            public Bitmap Contrast(Bitmap src, float effectThreshold)
            {

                // 依照 Format24bppRgb 每三个表示一 Pixel 0: 蓝 1: 绿 2: 红
                BitmapData bitmapData = src.LockBits(new Rectangle(0, 0, src.Width, src.Height), ImageLockMode.ReadWrite, PixelFormat.Format24bppRgb);

                // 判断是否在 -100~100
                effectThreshold = effectThreshold  100 ? 100 : effectThreshold;

                effectThreshold = (float)((100.0 + effectThreshold) / 100.0);
                effectThreshold *= effectThreshold;

                unsafe
                {
                    // 抓住第一个 Pixel 第一个数值 www.it165.net
                    byte* p = (byte*)(void*)bitmapData.Scan0;

                    // 跨步值 - 宽度 *3 可以算出畸零地 之后跳到下一行
                    int nOffset = bitmapData.Stride - src.Width * 3;



                    for (int y = 0; y  255 ? 255 : buffer;
                            buffer = buffer  255 ? 255 : buffer;
                            buffer = buffer  255 ? 255 : buffer;
                            buffer = buffer 
            /// 对图片进行雾化效果
            /// 
            /// 
            /// 
            public Bitmap Atomization(Bitmap bmp)
            {

                int Height = bmp.Height;
                int Width = bmp.Width;
                Bitmap newBitmap = new Bitmap(Width, Height);
                Bitmap oldBitmap = bmp;
                Color pixel;
                for (int x = 1; x = Width)
                            dx = Width - 1;
                        if (dy >= Height)
                            dy = Height - 1;
                        pixel = oldBitmap.GetPixel(dx, dy);
                        newBitmap.SetPixel(x, y, pixel);
                    }
                }
                return newBitmap;
            }
            #endregion

        } //END Class DrawValidationCode
        #endregion

    #region 高斯模糊算法
    /// 
    /// 高斯模糊算法
    /// 
    public class Gaussian
    {
        public static double[,] Calculate1DSampleKernel(double deviation, int size)
        {
            double[,] ret = new double[size, 1];
            double sum = 0;
            int half = size / 2;
            for (int i = 0; i = 0 && cox = 0 && coy 
        /// 对颜色值进行灰色处理
        /// 
        /// 
        /// 
        private Color grayscale(Color cr)
        {
            return Color.FromArgb(cr.A, (int)(cr.R * .3 + cr.G * .59 + cr.B * 0.11),
               (int)(cr.R * .3 + cr.G * .59 + cr.B * 0.11),
              (int)(cr.R * .3 + cr.G * .59 + cr.B * 0.11));
        }
        /// 
        /// 对图片进行高斯模糊
        /// 
        /// 模糊数值,数值越大模糊越很
        /// 一个需要处理的图片
        /// 
        public Bitmap FilterProcessImage(double d, Bitmap image)
        {
            Bitmap ret = new Bitmap(image.Width, image.Height);
            Double[,] matrixR = new Double[image.Width, image.Height];
            Double[,] matrixG = new Double[image.Width, image.Height];
            Double[,] matrixB = new Double[image.Width, image.Height];
            for (int i = 0; i 

  


关于如何调用这个类,我举一个ASP.Net MVC中的例子:

        

            //首先实例化验证码的类
            ValidateCode validateCode = new ValidateCode();
            //生成验证码指定的长度
            string code = validateCode.GetRandomString(4);
            //创建验证码的图片
            byte[] bytes = validateCode.CreateImage(code);
            //最后将验证码返回
            return File(bytes, @"image/jpeg");

C#彩色扭曲验证码

标签:des   style   blog   http   io   color   os   ar   使用   

原文地址:http://www.cnblogs.com/huxiaolin/p/4067587.html

(0)
(0)
   
举报
评论 一句话评论(0


评论


亲,登录后才可以留言!