转 一个简单实用的 生成验证码的 代码

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

 

posted @ 2015-09-10 20:05  逆流而下  阅读(273)  评论(0编辑  收藏  举报