码迷,mamicode.com
首页 > 其他好文 > 详细

验证码类

时间:2015-07-26 00:19:41      阅读:147      评论:0      收藏:0      [点我收藏+]

标签:

  1    public class ValidateCode
  2     {
  3         public ValidateCode()
  4         {
  5         }
  6         /// <summary>
  7         /// 验证码的最大长度
  8         /// </summary>
  9         public int MaxLength
 10         {
 11             get { return 10; }
 12         }
 13         /// <summary>
 14         /// 验证码的最小长度
 15         /// </summary>
 16         public int MinLength
 17         {
 18             get { return 1; }
 19         }
 20         /// <summary>
 21         /// 生成验证码
 22         /// </summary>
 23         /// <param name="length">指定验证码的长度</param>
 24         /// <returns></returns>
 25         public string CreateValidateCode(int length)
 26         {
 27             int[] randMembers = new int[length];
 28             int[] validateNums = new int[length];
 29             string validateNumberStr = "";
 30             //生成起始序列值
 31             int seekSeek = unchecked((int)DateTime.Now.Ticks);
 32             Random seekRand = new Random(seekSeek);
 33             int beginSeek = (int)seekRand.Next(0, Int32.MaxValue - length * 10000);
 34             int[] seeks = new int[length];
 35             for (int i = 0; i < length; i++)
 36             {
 37                 beginSeek += 10000;
 38                 seeks[i] = beginSeek;
 39             }
 40             //生成随机数字
 41             for (int i = 0; i < length; i++)
 42             {
 43                 Random rand = new Random(seeks[i]);
 44                 int pownum = 1 * (int)Math.Pow(10, length);
 45                 randMembers[i] = rand.Next(pownum, Int32.MaxValue);
 46             }
 47             //抽取随机数字
 48             for (int i = 0; i < length; i++)
 49             {
 50                 string numStr = randMembers[i].ToString();
 51                 int numLength = numStr.Length;
 52                 Random rand = new Random();
 53                 int numPosition = rand.Next(0, numLength - 1);
 54                 validateNums[i] = Int32.Parse(numStr.Substring(numPosition, 1));
 55             }
 56             //生成验证码
 57             for (int i = 0; i < length; i++)
 58             {
 59                 validateNumberStr += validateNums[i].ToString();
 60             }
 61             return validateNumberStr;
 62         }
 63 
 64         /// <summary>
 65         /// 创建验证码的图片
 66         /// </summary>
 67         /// <param name="containsPage">要输出到的page对象</param>
 68         /// <param name="validateNum">验证码</param>
 69         public void CreateValidateGraphic(string validateCode, HttpContext context)
 70         {
 71             Bitmap image = new Bitmap((int)Math.Ceiling(validateCode.Length * 12.0), 22);
 72             Graphics g = Graphics.FromImage(image);
 73             try
 74             {
 75                
 76                 //生成随机生成器
 77                 Random random = new Random();
 78                 //清空图片背景色
 79                 g.Clear(Color.White);
 80                 //画图片的干扰线
 81                 for (int i = 0; i < 25; i++)
 82                 {
 83                     int x1 = random.Next(image.Width);
 84                     int x2 = random.Next(image.Width);
 85                     int y1 = random.Next(image.Height);
 86                     int y2 = random.Next(image.Height);
 87                     g.DrawLine(new Pen(Color.Silver), x1, y1, x2, y2);
 88                 }
 89                 Font font = new Font("Arial", 12, (FontStyle.Bold | FontStyle.Italic));
 90                 LinearGradientBrush brush = new LinearGradientBrush(new Rectangle(0, 0, image.Width, image.Height),//渐变.
 91                  Color.Blue, Color.DarkRed, 1.2f, true);
 92                 g.DrawString(validateCode, font, brush, 3, 2);
 93                 //画图片的前景干扰点
 94                 for (int i = 0; i < 100; i++)
 95                 {
 96                     int x = random.Next(image.Width);
 97                     int y = random.Next(image.Height);
 98                     image.SetPixel(x, y, Color.FromArgb(random.Next()));
 99                 }
100                 //画图片的边框线
101                 g.DrawRectangle(new Pen(Color.Silver), 0, 0, image.Width - 1, image.Height - 1);
102                 //保存图片数据
103                 MemoryStream stream = new MemoryStream();
104                 image.Save(stream, ImageFormat.Jpeg);
105                 //输出图片流
106                 context.Response.Clear();
107                 context.Response.ContentType = "image/jpeg";
108                 context.Response.BinaryWrite(stream.ToArray());
109             }
110             finally
111             {
112                 g.Dispose();
113                 image.Dispose();
114             }
115         }
116         /// <summary>
117         /// 得到验证码图片的长度
118         /// </summary>
119         /// <param name="validateNumLength">验证码的长度</param>
120         /// <returns></returns>
121         public static int GetImageWidth(int validateNumLength)
122         {
123             return (int)(validateNumLength * 12.0);
124         }
125         /// <summary>
126         /// 得到验证码的高度
127         /// </summary>
128         /// <returns></returns>
129         public static double GetImageHeight()
130         {
131             return 22.5;
132         }
133     }

 

验证码类

标签:

原文地址:http://www.cnblogs.com/lansehaiyang/p/4676817.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!