创建数字验证码

  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        //======================在webform中的写法============================
 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                 Random random = new Random();
 77                 //清空图片背景色
 78                 g.Clear(Color.White);
 79                 //画图片的干扰线
 80                 for (int i = 0; i < 25; i++)
 81                 {
 82                     int x1 = random.Next(image.Width);
 83                     int x2 = random.Next(image.Width);
 84                     int y1 = random.Next(image.Height);
 85                     int y2 = random.Next(image.Height);
 86                     g.DrawLine(new Pen(Color.Silver), x1, y1, x2, y2);
 87                 }
 88                 Font font = new Font("Arial", 12, (FontStyle.Bold | FontStyle.Italic));
 89                 LinearGradientBrush brush = new LinearGradientBrush(new Rectangle(0, 0, image.Width, image.Height),
 90                  Color.Blue, Color.DarkRed, 1.2f, true);
 91                 g.DrawString(validateCode, font, brush, 3, 2);
 92                 //画图片的前景干扰点
 93                 for (int i = 0; i < 100; i++)
 94                 {
 95                     int x = random.Next(image.Width);
 96                     int y = random.Next(image.Height);
 97                     image.SetPixel(x, y, Color.FromArgb(random.Next()));
 98                 }
 99                 //画图片的边框线
100                 g.DrawRectangle(new Pen(Color.Silver), 0, 0, image.Width - 1, image.Height - 1);
101                 //保存图片数据
102                 MemoryStream stream = new MemoryStream();
103                 image.Save(stream, ImageFormat.Jpeg);
104                 //输出图片流
105                 context.Response.Clear();
106                 context.Response.ContentType = "image/jpeg";
107                 context.Response.BinaryWrite(stream.ToArray());
108             }
109             finally
110             {
111                 g.Dispose();
112                 image.Dispose();
113             }
114         }
115         /// <summary>
116         /// 得到验证码图片的长度
117         /// </summary>
118         /// <param name="validateNumLength">验证码的长度</param>
119         /// <returns></returns>
120         public static int GetImageWidth(int validateNumLength)
121         {
122             return (int)(validateNumLength * 12.0);
123         }
124         /// <summary>
125         /// 得到验证码的高度
126         /// </summary>
127         /// <returns></returns>
128         public static double GetImageHeight()
129         {
130             return 22.5;
131         }
132 
133         //===============在mvc中的写法=========================
134         //C# MVC 升级版
135         /// <summary>
136         /// 创建验证码的图片
137         /// </summary>
138         /// <param name="containsPage">要输出到的page对象</param>
139         /// <param name="validateNum">验证码</param>
140         public byte[] CreateValidateGraphic(string validateCode)
141         {
142             Bitmap image = new Bitmap((int)Math.Ceiling(validateCode.Length * 12.0), 22);
143             Graphics g = Graphics.FromImage(image);
144             try
145             {
146                 //生成随机生成器
147                 Random random = new Random();
148                 //清空图片背景色
149                 g.Clear(Color.White);
150                 //画图片的干扰线
151                 for (int i = 0; i < 25; i++)
152                 {
153                     int x1 = random.Next(image.Width);
154                     int x2 = random.Next(image.Width);
155                     int y1 = random.Next(image.Height);
156                     int y2 = random.Next(image.Height);
157                     g.DrawLine(new Pen(Color.Silver), x1, y1, x2, y2);
158                 }
159                 Font font = new Font("Arial", 12, (FontStyle.Bold | FontStyle.Italic));
160                 LinearGradientBrush brush = new LinearGradientBrush(new Rectangle(0, 0, image.Width, image.Height),
161                  Color.Blue, Color.DarkRed, 1.2f, true);
162                 g.DrawString(validateCode, font, brush, 3, 2);
163                 //画图片的前景干扰点
164                 for (int i = 0; i < 100; i++)
165                 {
166                     int x = random.Next(image.Width);
167                     int y = random.Next(image.Height);
168                     image.SetPixel(x, y, Color.FromArgb(random.Next()));
169                 }
170                 //画图片的边框线
171                 g.DrawRectangle(new Pen(Color.Silver), 0, 0, image.Width - 1, image.Height - 1);
172                 //保存图片数据
173                 MemoryStream stream = new MemoryStream();
174                 image.Save(stream, ImageFormat.Jpeg);
175                 //输出图片流
176                 return stream.ToArray();
177             }
178             finally
179             {
180                 g.Dispose();
181                 image.Dispose();
182             }
183         }
184     }

上面是生成验证码以及验证码图片

下面是怎么使用:

1   string code=vCode.CreateValidateCode(4);//得到生成的验证码
2   byte[] bcode=vCode.CreateValidateGraphic(code);//将验证码创建成图片的二进制格式

 

posted @ 2016-06-16 22:41  黄树强  阅读(606)  评论(0编辑  收藏  举报