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