生成验证码:
ValidateCode.aspx.cs文件
1 private void Page_Load(object sender, System.EventArgs e)
2 {
3 string VNum=RndNum(4);//这里的数字4代表显示的是4位的验证字符串!
4 Session["VNum"]=VNum; //创建验证码的Session
5 GetValidateCode(VNum);//生成图像
6 }
7
8
9 /// <summary>
10 /// 生成图像
11 /// </summary>
12 /// <param name="VNum"></param>
13 private void GetValidateCode(string VNum)
14 {
15 int Gwidth=(int)(VNum.Length * 13);
16 //Gwidth为图片宽度,根据字符长度自动更改图片宽度
17 System.Drawing.Bitmap Img = new System.Drawing.Bitmap(Gwidth,25);
18 Graphics g = Graphics.FromImage(Img);
19 g.DrawString(VNum,new System.Drawing.Font("Arial",12),new System.Drawing.SolidBrush(Color.Blue),3,3);
20 //在矩形内绘制字串(字串,字体,画笔颜色,左上x.左上y)
21 System.IO.MemoryStream ms=new System.IO.MemoryStream();
22 Img.Save(ms,System.Drawing.Imaging.ImageFormat.Png);
23 Response.ClearContent(); //需要输出图象信息 要修改HTTP头
24 Response.ContentType="image/Png";
25 Response.BinaryWrite(ms.ToArray());
26 g.Dispose();
27 Img.Dispose();
28 Response.End();
29 }
30
31
32 /// <summary>
33 /// 生成随机数,从Vchar数组中随机抽取字符.
34 /// 字母区分大小写
35 /// </summary>
36 /// <param name="VcodeNum"></param>
37 /// <returns></returns>
38 public string RndNum(int VcodeNum)
39 {
40 string Vchar = "0,1,2,3,4,5,6,7,8,9,A,B,C,D,E,F,G,H,I,J,K,L,M,N,O,P,Q,R,S,T,U,W,X,Y,Z" ;
41
42 string[] VcArray = Vchar.Split(',') ;
43 string VNum = "" ;
44 int temp = -1 ;//记录上次随机数值,尽量避免生产几个一样的随机数
45
46 //采用一个简单的算法以保证生成随机数的不同
47 Random rand =new Random();
48 for ( int i = 1 ; i < VcodeNum+1 ; i++ )
49 {
50 if ( temp != -1)
51 {
52 rand =new Random(i*temp*unchecked((int)DateTime.Now.Ticks));
53 }
54 int t=rand.Next(35);
55 if (temp != -1 && temp == t)
56 {
57 return RndNum( VcodeNum );
58 }
59 temp = t ;
60 VNum += VcArray[t];
61 }
62 return VNum ;
63 }
2 {
3 string VNum=RndNum(4);//这里的数字4代表显示的是4位的验证字符串!
4 Session["VNum"]=VNum; //创建验证码的Session
5 GetValidateCode(VNum);//生成图像
6 }
7
8
9 /// <summary>
10 /// 生成图像
11 /// </summary>
12 /// <param name="VNum"></param>
13 private void GetValidateCode(string VNum)
14 {
15 int Gwidth=(int)(VNum.Length * 13);
16 //Gwidth为图片宽度,根据字符长度自动更改图片宽度
17 System.Drawing.Bitmap Img = new System.Drawing.Bitmap(Gwidth,25);
18 Graphics g = Graphics.FromImage(Img);
19 g.DrawString(VNum,new System.Drawing.Font("Arial",12),new System.Drawing.SolidBrush(Color.Blue),3,3);
20 //在矩形内绘制字串(字串,字体,画笔颜色,左上x.左上y)
21 System.IO.MemoryStream ms=new System.IO.MemoryStream();
22 Img.Save(ms,System.Drawing.Imaging.ImageFormat.Png);
23 Response.ClearContent(); //需要输出图象信息 要修改HTTP头
24 Response.ContentType="image/Png";
25 Response.BinaryWrite(ms.ToArray());
26 g.Dispose();
27 Img.Dispose();
28 Response.End();
29 }
30
31
32 /// <summary>
33 /// 生成随机数,从Vchar数组中随机抽取字符.
34 /// 字母区分大小写
35 /// </summary>
36 /// <param name="VcodeNum"></param>
37 /// <returns></returns>
38 public string RndNum(int VcodeNum)
39 {
40 string Vchar = "0,1,2,3,4,5,6,7,8,9,A,B,C,D,E,F,G,H,I,J,K,L,M,N,O,P,Q,R,S,T,U,W,X,Y,Z" ;
41
42 string[] VcArray = Vchar.Split(',') ;
43 string VNum = "" ;
44 int temp = -1 ;//记录上次随机数值,尽量避免生产几个一样的随机数
45
46 //采用一个简单的算法以保证生成随机数的不同
47 Random rand =new Random();
48 for ( int i = 1 ; i < VcodeNum+1 ; i++ )
49 {
50 if ( temp != -1)
51 {
52 rand =new Random(i*temp*unchecked((int)DateTime.Now.Ticks));
53 }
54 int t=rand.Next(35);
55 if (temp != -1 && temp == t)
56 {
57 return RndNum( VcodeNum );
58 }
59 temp = t ;
60 VNum += VcArray[t];
61 }
62 return VNum ;
63 }
可以如下方法引用:
ValidateTest.aspx.cs文件
1 private void Page_Load(object sender, System.EventArgs e)
2 {
3 if(!IsPostBack)
4 {
5 //设置Image控件的ImageUrl属性(ValidateCode.aspx生成验证码的页名)
6 this.Image1.ImageUrl = "ValidateCode.aspx";
7 }
8 }
9
10 //按钮事件
11 private void Button1_Click(object sender, System.EventArgs e)
12 {
13 if(this.TextBox1.Text == Session["VNum"].ToString())
14 {
15 Response.Write("验证码正确!");
16 }
17 else
18 {
19 Response.Write("验证码错误!");
20 }
21 }
22
2 {
3 if(!IsPostBack)
4 {
5 //设置Image控件的ImageUrl属性(ValidateCode.aspx生成验证码的页名)
6 this.Image1.ImageUrl = "ValidateCode.aspx";
7 }
8 }
9
10 //按钮事件
11 private void Button1_Click(object sender, System.EventArgs e)
12 {
13 if(this.TextBox1.Text == Session["VNum"].ToString())
14 {
15 Response.Write("验证码正确!");
16 }
17 else
18 {
19 Response.Write("验证码错误!");
20 }
21 }
22