验证码类(转)

it2025-03-16  22

 

这些东西网上都泛滥了,随便拉了个过来,然后改改,加上自己的标示什么的,就是这样子了。 可以定义验证码的格式,包括字母、数字、特殊符号和汉字,指定生成的图片大小。 本来还想加上图片扭曲的,可怕以后用起来被人说“万恶的验证码”所以就算了。   1 using  System;   2 using  System.Collections;   3 using  System.Collections.Generic;   4 using  System.Drawing;   5 using  System.Text;   6 using  System.Threading;   7 using  System.Drawing.Drawing2D;   8   9 namespace  SHNK.Framework.Utility  10 { 11    /**//// <summary> 12    /// 验证码生成类 13    /// </summary> 14    public static class ValidateCodeHelper 15    { 16        /**//// <summary> 17        /// 生成长度为length、字符类型为codeType的验证码字符串 18        /// </summary> 19        /// <param name="codeType">验证码类型</param> 20        /// <param name="length">验证码长度</param> 21        /// <returns>验证码字符串</returns> 22        public static string GenerateCode(ValidateCodeType codeType, int length) 23        { 24 25            const string sNumber = "0123456789"; 26            const string sLetter = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"; 27            const string sSymbol = "!\"#$%&'()*+,-./:;<=>?@[\\]^_`{|}~"; 28 29            if ((codeType | ValidateCodeType.Chinese) == codeType) 30            { 31                return GetChinese(length); 32            } 33 34            StringBuilder blocks = new StringBuilder(); 35            StringBuilder builder = new StringBuilder(); 36 37            if ((codeType | ValidateCodeType.Number) == codeType) 38                blocks.Append(sNumber); 39 40            if ((codeType | ValidateCodeType.Letter) == codeType) 41                blocks.Append(sLetter); 42 43            if ((codeType | ValidateCodeType.Symbol) == codeType) 44                blocks.Append(sSymbol); 45 46            Random rand = new Random(); 47            for (int i = 0; i < length; i++) 48            { 49                builder.Append(blocks[rand.Next(blocks.Length)]); 50            } 51 52            return builder.ToString(); 53        } 54 55        /**//// <summary> 56        /// 获取num个中文字符 57        /// </summary> 58        /// <param name="num">字符串长度</param> 59        /// <returns>中文字符</returns> 60        private static string GetChinese(int num) 61        { 62            /**//* ********************************************* 63             * be in common use 64             *  65             * first byte       0xB0 - 0xD7 66             * second byte      0xA1 - 0xFE 67             *  68             *          except  0xD7FA - 0xD7FE (space) 69             * *********************************************/ 70            const int _MAX_SIZE = 3755//  (0xD7 - 0xB0 + 1) * (0xFE - 0xA1 + 1) - (0xD7FE - 0xD7FA + 1); 71            const int _Capacity = 94;   //  0xFE - 0xA1 + 1 72 73            Random rand = new Random(); 74            StringBuilder builder = new StringBuilder(); 75            for (int i = 0; i < num; i++) 76            { 77                int index = rand.Next(_MAX_SIZE); 78                int high = Math.Floor(index.ToDouble() / _Capacity.ToDouble()).ToInt32(); 79                int lower = index % _Capacity; 80                string code = Encoding.Default.GetString(new byte[] { (byte)(high + 0xB0), (byte)(lower + 0xA1) }); 81                builder.Append(code); 82            } 83            return builder.ToString(); 84        } 85 86        /**//// <summary> 87        /// 按照输入的字符串src和每个字符位图的宽度生成一张图片 88        /// </summary> 89        /// <param name="src">验证码</param> 90        /// <param name="height">图片高度</param> 91        /// <param name="wdpc">每个字符所占宽度</param> 92        /// <returns>验证图片</returns> 93        public static Image GenerateImage(string src, int height, int wdpc) 94        { 95            Bitmap img = new Bitmap(src.Length * wdpc, height); 96            Graphics g = Graphics.FromImage(img); 97            g.Clear(Color.White); 98 99            Random rand = new Random();100            for (int i = 0; i < 25; i++)101            {102                int x1 = rand.Next(img.Width);103                int x2 = rand.Next(img.Width);104                int y1 = rand.Next(img.Height);105                int y2 = rand.Next(img.Height);106107                g.DrawLine(new Pen(Color.Silver), x1, y1, x2, y2);108            }109            Font f = new Font("'Segoe UI' 微软雅黑 Verdana 宋体", 14f);110            Brush b = new LinearGradientBrush(new Rectangle(new Point(00), img.Size), Color.Blue, Color.DarkRed, LinearGradientMode.ForwardDiagonal);111            for (int i = 0; i < src.Length; i++)112                g.DrawString(src[i].ToString(), f, b, wdpc * i.ToSingle(), 0f);113114            g.Save();115            for (int i = 0; i < 100; i++)116            {117                int x = rand.Next(img.Width);118                int y = rand.Next(img.Height);119120                img.SetPixel(x, y, Color.FromArgb(rand.Next()));121            }122123            return img;124        }125    }126127    ValidateCodeType Enum#region ValidateCodeType Enum128    /**//// <summary>129    /// 验证码类型枚举130    /// </summary>131    public enum ValidateCodeType132    {133        /**//*  ValidateCodeType枚举成员                                     134         *  135         *      Number:136         *        ASCII Code 48 - 57137         *        138         *      Letter:139         *        Uppercase140         *          ASCII Code 65 - 90141         *        Lowercase142         *          ASCII Code 97 - 122143         *      144         *      Symbol:145         *          ASCII Code 33 - 126 except Number and Letter146         *          147         *      Chinese:148         *          Unicode 0xA1A1-0xF7FE149         */150        /**//// <summary>151        /// 数字152        /// </summary>153        Number = 1,154        /**//// <summary>155        /// 字母156        /// </summary>157        Letter = 2,158        /**//// <summary>159        /// 符号160        /// </summary>161        Symbol = 4,162        /**//// <summary>163        /// 汉字164        /// </summary>165        Chinese = 8166    }167    #endregion168169    public static partial class Extension170    {171        convert method#region convert method172        public static double ToDouble(this IConvertible o)173        {174            return Convert.ToDouble(o);175        }176177        public static int ToInt32(this IConvertible o)178        {179            return Convert.ToInt32(o);180        }181182        public static float ToSingle(this IConvertible o)183        {184            return Convert.ToSingle(o);185        }186        #endregion187    }188}

转载于:https://www.cnblogs.com/HappyQQ/archive/2008/04/16/1155945.html

最新回复(0)