[ASP.NET] 验证码生成
类:
![]()
CheckCode.cs
1
//----------------------------------------------------------------------
2
// Ryan Wei
3
// Date:2008.05.08
4
//----------------------------------------------------------------------
5
// Description:
6
//----------------------------------------------------------------------
7
using System;
8
using System.Collections.Generic;
9
using System.Text;
10
using System.Drawing;
11![]()
12
namespace HouseProgram.Common
13![]()
![]()
{
14
public class CheckCode
15![]()
{
16![]()
/**///// <summary>
17
/// 生成随机数
18
/// </summary>
19
/// <param name="codeCount">要生成的随机数位数</param>
20
/// <returns></returns>
21
public static string CreateRandomCode(int codeCount)
22![]()
{
23
string allChar = "0,1,2,3,4,5,6,7,8,9";
24
string[] allCharArray = allChar.Split(',');
25
string randomCode = "";
26
int temp = -1;
27![]()
28
Random rand = new Random();
29
for(int i = 0; i < codeCount; i++)
30![]()
{
31
if(temp != -1)
32![]()
{
33
rand = new Random(i*temp*((int)DateTime.Now.Ticks));
34
}
35
int t = rand.Next(9);
36
if(temp == t)
37![]()
{
38
return CreateRandomCode(codeCount);
39
}
40
temp = t;
41
randomCode += allCharArray[t];
42
}
43
return randomCode;
44
}
45![]()
/**///// <summary>
46
/// 生成验证码图片
47
/// </summary>
48
/// <param name="checkCode"></param>
49
public static void CreateImage(string checkCode)
50![]()
{
51
int iwidth = (int)(checkCode.Length * 11.5);
52
System.Drawing.Bitmap image = new System.Drawing.Bitmap(iwidth, 20);
53
Graphics g = Graphics.FromImage(image);
54
Font f = new System.Drawing.Font("Arial", 10, System.Drawing.FontStyle.Bold);
55
Brush b = new System.Drawing.SolidBrush(Color.White);
56
g.Clear(Color.Blue);
57
g.DrawString(checkCode, f, b, 3, 3);
58![]()
59
Pen blackPen = new Pen(Color.Brown,0);
60
Random rand = new Random();
61
for (int i=0;i<5;i++)
62![]()
{
63
int y = rand.Next(image.Height);
64
}
65
66
System.IO.MemoryStream ms = new System.IO.MemoryStream();
67
for(int i=0; i<90; i++)
68![]()
{
69
int x = rand.Next(image.Width);
70
int y = rand.Next(image.Height);
71
image.SetPixel(x, y, Color.FromArgb(rand.Next()));
72
}
73
image.Save(ms,System.Drawing.Imaging.ImageFormat.Jpeg);
74
System.Web.HttpContext.Current.Response.ClearContent();
75
System.Web.HttpContext.Current.Response.ContentType = "image/Jpeg";
76
System.Web.HttpContext.Current.Response.BinaryWrite(ms.ToArray());
77
g.Dispose();
78
image.Dispose();
79
}
80
}
81
}
82![]()
页面后台代码:
![]()
checkCode.aspx.cs
1
//----------------------------------------------------------------------
2
// Ryan Wei
3
// Date:2008.05.08
4
//----------------------------------------------------------------------
5
// Description: CheckCode Display Page
6
//----------------------------------------------------------------------
7
using System;
8
using System.Data;
9
using System.Configuration;
10
using System.Collections;
11
using System.Web;
12
using System.Web.Security;
13
using System.Web.UI;
14
using System.Web.UI.WebControls;
15
using System.Web.UI.WebControls.WebParts;
16
using System.Web.UI.HtmlControls;
17
using HouseProgram.Common;
18![]()
19
public partial class checkCode : System.Web.UI.Page
20![]()
![]()
{
21![]()
/**//// <summary>
22
///
23
/// </summary>
24
/// <param name="sender"></param>
25
/// <param name="e"></param>
26
protected void Page_Load(object sender, EventArgs e)
27![]()
{
28
//Generate 4 bit CheckCode
29
string checkCode = CheckCode.CreateRandomCode(4);
30
//Cache CheckCode
31
Session[PublicConstString.session_CheckCode] = checkCode;
32
//Create Image
33
CheckCode.CreateImage(checkCode);
34
}
35
}
36![]()
在登录页面添加ImageButton, ImageURL指定为CheckCode.aspx页面。
1
//----------------------------------------------------------------------2
// Ryan Wei3
// Date:2008.05.084
//----------------------------------------------------------------------5
// Description: 6
//----------------------------------------------------------------------7
using System;8
using System.Collections.Generic;9
using System.Text;10
using System.Drawing;11

12
namespace HouseProgram.Common13


{14
public class CheckCode15

{16

/**///// <summary>17
/// 生成随机数18
/// </summary>19
/// <param name="codeCount">要生成的随机数位数</param>20
/// <returns></returns>21
public static string CreateRandomCode(int codeCount)22

{23
string allChar = "0,1,2,3,4,5,6,7,8,9";24
string[] allCharArray = allChar.Split(',');25
string randomCode = "";26
int temp = -1;27

28
Random rand = new Random();29
for(int i = 0; i < codeCount; i++)30

{31
if(temp != -1)32

{33
rand = new Random(i*temp*((int)DateTime.Now.Ticks));34
}35
int t = rand.Next(9);36
if(temp == t)37

{38
return CreateRandomCode(codeCount);39
}40
temp = t;41
randomCode += allCharArray[t];42
}43
return randomCode;44
}45

/**///// <summary>46
/// 生成验证码图片47
/// </summary>48
/// <param name="checkCode"></param>49
public static void CreateImage(string checkCode)50

{51
int iwidth = (int)(checkCode.Length * 11.5);52
System.Drawing.Bitmap image = new System.Drawing.Bitmap(iwidth, 20);53
Graphics g = Graphics.FromImage(image);54
Font f = new System.Drawing.Font("Arial", 10, System.Drawing.FontStyle.Bold);55
Brush b = new System.Drawing.SolidBrush(Color.White);56
g.Clear(Color.Blue);57
g.DrawString(checkCode, f, b, 3, 3);58

59
Pen blackPen = new Pen(Color.Brown,0);60
Random rand = new Random();61
for (int i=0;i<5;i++)62

{63
int y = rand.Next(image.Height);64
}65
66
System.IO.MemoryStream ms = new System.IO.MemoryStream();67
for(int i=0; i<90; i++)68

{69
int x = rand.Next(image.Width);70
int y = rand.Next(image.Height);71
image.SetPixel(x, y, Color.FromArgb(rand.Next()));72
}73
image.Save(ms,System.Drawing.Imaging.ImageFormat.Jpeg);74
System.Web.HttpContext.Current.Response.ClearContent();75
System.Web.HttpContext.Current.Response.ContentType = "image/Jpeg";76
System.Web.HttpContext.Current.Response.BinaryWrite(ms.ToArray());77
g.Dispose();78
image.Dispose();79
}80
}81
}82

页面后台代码:
1
//----------------------------------------------------------------------2
// Ryan Wei3
// Date:2008.05.084
//----------------------------------------------------------------------5
// Description: CheckCode Display Page6
//----------------------------------------------------------------------7
using System;8
using System.Data;9
using System.Configuration;10
using System.Collections;11
using System.Web;12
using System.Web.Security;13
using System.Web.UI;14
using System.Web.UI.WebControls;15
using System.Web.UI.WebControls.WebParts;16
using System.Web.UI.HtmlControls;17
using HouseProgram.Common;18

19
public partial class checkCode : System.Web.UI.Page20


{21

/**//// <summary>22
/// 23
/// </summary>24
/// <param name="sender"></param>25
/// <param name="e"></param>26
protected void Page_Load(object sender, EventArgs e)27

{28
//Generate 4 bit CheckCode29
string checkCode = CheckCode.CreateRandomCode(4);30
//Cache CheckCode31
Session[PublicConstString.session_CheckCode] = checkCode;32
//Create Image33
CheckCode.CreateImage(checkCode);34
}35
}36

在登录页面添加ImageButton, ImageURL指定为CheckCode.aspx页面。

浙公网安备 33010602011771号