首页 > 代码库 > C#彩色扭曲验证码

C#彩色扭曲验证码

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

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

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

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

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

            private int fontMinSize = 20;
            /// <summary>
            /// 验证码字体的最小值,默认为15,建议不小于15像素
            /// </summary>
            public Int32 FontMinSize
            {
                get { return fontMinSize; }
                set { fontMinSize = value; }
            }
            private Int32 fontMaxSize = 25;
            /// <summary>
            /// 验证码字体的最大值,默认为20
            /// </summary>
            public Int32 FontMaxSize
            {
                get { return fontMaxSize; }
                set { fontMaxSize = value; }
            }
            private Color[] fontColor = { };
            /// <summary>
            /// 验证码字体的颜色,默认为系统自动生成字体颜色
            /// </summary>
            public Color[] FontColor
            {
                get { return fontColor; }
                set { fontColor = value; }
            }
            private Color backColor = Color.FromArgb(243, 255, 255);
            /// <summary>
            /// 验证码的背景色,默认为Color.FromArgb(243, 251, 254)
            /// </summary>
            public Color BackgroundColor
            {
                get { return backColor; }
                set { backColor = value; }
            }
            private Int32 bezierCount = 3;
            /// <summary>
            /// 贝塞尔曲线的条数,默认为3条
            /// </summary>
            public Int32 BezierCount
            {
                get { return bezierCount; }
                set { bezierCount = value; }
            }
            private Int32 lineCount = 3;
            /// <summary>
            /// 直线条数,默认为3条
            /// </summary>
            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;  
            /// <summary>
            /// 随机字符串列表,请使用英文状态下的逗号分隔。
            /// </summary>
            public String CharCollection
            {
                get { return charCollection; }
                set { charCollection = value; }
            }
            private Int32 intCount = 4;
            /// <summary>
            /// 验证码字符串个数,默认为4个字符
            /// </summary>
            public Int32 IntCount
            {
                get { return intCount; }
                set { intCount = value; }
            }
            private Boolean isPixel = true;
            /// <summary>
            /// 是否添加噪点,默认添加,噪点颜色为系统随机生成。
            /// </summary>
            public Boolean IsPixel
            {
                get { return isPixel; }
                set { isPixel = value; }
            }
            private Boolean isRandString = true;
            /// <summary>
            /// 是否添加随机噪点字符串,默认添加
            /// </summary>
            public Boolean IsRandString
            {
                get { return isRandString; }
                set { isRandString = value; }
            }
            /// <summary>
            /// 随机背景字符串的个数
            /// </summary>
            public Int32 RandomStringCount
            {
                get;
                set;
            }
            private Int32 randomStringFontSize = 9;
            /// <summary>
            /// 随机背景字符串的大小
            /// </summary>
            public Int32 RandomStringFontSize
            {
                get { return randomStringFontSize; }
                set { randomStringFontSize = value; }
            }
            /// <summary>
            /// 是否对图片进行扭曲
            /// </summary>
            public Boolean IsTwist
            {
                get;
                set;
            }
            /// <summary>
            /// 边框样式
            /// </summary>
            public enum BorderStyle
            {
                /// <summary>
                /// 无边框
                /// </summary>
                None,
                /// <summary>
                /// 矩形边框
                /// </summary>
                Rectangle,
                /// <summary>
                /// 圆角边框
                /// </summary>
                RoundRectangle
            }
            private Int32 rotationAngle = 40;
            /// <summary>
            /// 验证码字符串随机转动的角度的最大值
            /// </summary>
            public Int32 RotationAngle
            {
                get { return rotationAngle; }
                set { rotationAngle = value; }
            }
            /// <summary>
            /// 设置或获取边框样式
            /// </summary>
            public BorderStyle Border
            {
                get;
                set;
            }
            private Point[] strPoint = null;


            private Double gaussianDeviation = 0;
            /// <summary>
            /// 对验证码图片进行高斯模糊的阀值,如果设置为0,则不对图片进行高斯模糊,该设置可能会对图片处理的性能有较大影响
            /// </summary>
            public Double GaussianDeviation
            {
                get { return gaussianDeviation; }
                set { gaussianDeviation = value; }
            }
            private Int32 brightnessValue = http://www.mamicode.com/0;>
关于如何调用这个类,我举一个ASP.Net MVC中的例子:

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

这样就可以了:





作者:胡孝林
出处:http://www.cnblogs.com/yiwuxun

本页版权归作者和CSDN博客所有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文链接,否则保留追究法律责任的权利

C#彩色扭曲验证码