一、 关于验证码
验证码是因为防止机器恶意注册而被广泛应用的技术。下面是使用ASP.NET在VB.NET和C#中实现的样例代码。
二、步骤
-
添加ASP.NET Web页面
- 首先,添加一个新的ASP.NET页面,指定网址,如“~/CheckCode.aspx” ,最好确保您设置为不得缓存页面。这个页面将会生成验证码的图片并直接输出。
-
创建验证码
- 使用Bitmap类处理验证码图片,使用Graphics类在Bitmap对象中创建图像。
-
增加允许字符
- 在生成验证码图片之前,您需要预定义一个包含所有允许字符的字符串常量,例如:“ABCDEFGHJKLMNPQRSTUVWXYZ23456789”。
- 注意:不包含“I”和“O”字符,因为它们容易与数字1和0混淆。
-
添加干扰线
- 这样可以使图片更复杂、更难以被机器识别。
- 使用坐标随机数生成代码,在Bitmap对象中创建干扰线。
-
添加点位置
- 往往需要将干扰点加入图片中。
-
创建一个随机字符
- 每次生成验证码,都需要使用一个随机字符,调用Random.Next()方法获取不重复的随机数。
-
清除其他信息
- 在输出图像之前,您需要清除其他信息。
-
输出验证码图片
- 最后,使用Response.OutputStream将您的验证码图像输出到客户端。
三、VB.NET示例代码
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
Dim strCodes As String = "ABCDEFGHJKLMNPQRSTUVWXYZ23456789" ' 验证码可能包含的字符
Dim strFont As String = "Verdana" ' 字体
Dim intWidth As Integer = 90 ' 图片宽度
Dim intHeight As Integer = 20 ' 图片高度
Dim intFontSize As Integer = 17 ' 字体大小
Dim intMaxLength As Integer = 4 ' 验证码长度 '
Dim strRandomCode As String = String.Empty
Dim rnd As New Random() ' 随机生成器
Dim img As New Bitmap(intWidth, intHeight)
Dim g As Graphics = Graphics.FromImage(img)
Dim font As New Font(strFont, intFontSize, FontStyle.Bold)
' 填充背景
g.Clear(Color.White)
' 排列验证码中的字符
For i As Integer = 0 To intMaxLength - 1
Dim strCurrChar As String = strCodes.Substring(rnd.Next(0, strCodes.Length - 1), 1)
strRandomCode = String.Concat(strRandomCode, strCurrChar)
Dim x As Integer = i * (intWidth \ intMaxLength)
Dim rndX As Integer = x + rnd.Next((intWidth \ intMaxLength) \ 2, (intWidth \ intMaxLength) - 10)
Dim rndY As Integer = rnd.Next(5, intHeight - intFontSize - 5)
' 在绘画画布上输出字符
g.DrawString(strCurrChar, font, New SolidBrush(Color.Black), rndX, rndY, New StringFormat())
Next
'添加干扰线
For i As Integer = 0 To 4
Dim x1 As Integer = rnd.Next(intWidth)
Dim x2 As Integer = rnd.Next(intWidth)
Dim y1 As Integer = rnd.Next(intHeight)
Dim y2 As Integer = rnd.Next(intHeight)
g.DrawLine(New Pen(Color.Black), x1, y1, x2, y2)
Next
' 添加干扰点位
For i As Integer = 0 To 50
Dim x As Integer = rnd.Next(intWidth)
Dim y As Integer = rnd.Next(intHeight)
img.SetPixel(x, y, Color.FromArgb(rnd.Next()))
Next
' 输出图片
Response.Cache.SetCacheability(HttpCacheability.NoCache)
Response.ContentType = "image/jpeg"
Dim ms As New MemoryStream()
img.Save(ms, ImageFormat.Jpeg)
Response.BinaryWrite(ms.ToArray())
g.Dispose()
img.Dispose()
End Sub
四、C#示例代码
protected void Page_Load(object sender, EventArgs e)
{
string strCodes = "ABCDEFGHJKLMNPQRSTUVWXYZ23456789";
string strFont = "Verdana";
int intWidth = 90;
int intHeight = 20;
int intFontSize = 17;
int intMaxLength = 4;
string strRandomCode = String.Empty;
Random rnd = new Random();
Bitmap img = new Bitmap(intWidth, intHeight);
Graphics gdi = Graphics.FromImage(img);
Font font = new Font(strFont, intFontSize, FontStyle.Bold);
//填充背景
gdi.Clear(Color.White);
//绘画验证码字符
for (int i = 0; i < intMaxLength; i++)
{
string strCurrChar = strCodes.Substring(rnd.Next(0, strCodes.Length - 1), 1);
strRandomCode = String.Concat(strRandomCode, strCurrChar);
int x = i * (intWidth / intMaxLength);
int rndX = x + rnd.Next((intWidth / intMaxLength) / 2, (intWidth / intMaxLength) - 10);
int rndY = rnd.Next(5, intHeight - intFontSize - 5);
// 在绘画画布上输出字符
gdi.DrawString(strCurrChar, font, new SolidBrush(Color.Black), rndX, rndY, new StringFormat());
}
//添加随机干扰线
for (int i = 0; i < 4; i++)
{
int x1 = rnd.Next(intWidth);
int x2 = rnd.Next(intWidth);
int y1 = rnd.Next(intHeight);
int y2 = rnd.Next(intHeight);
gdi.DrawLine(new Pen(Color.Black), x1, y1, x2, y2);
}
// 添加随机干扰点
for (int i = 0; i < 50; i++)
{
int x = rnd.Next(intWidth);
int y = rnd.Next(intHeight);
img.SetPixel(x, y, Color.FromArgb(rnd.Next()));
}
Response.Cache.SetCacheability(HttpCacheability.NoCache);
Response.ContentType = "image/jpeg";
MemoryStream ms = new MemoryStream();
img.Save(ms, ImageFormat.Jpeg);
Response.BinaryWrite(ms.ToArray());
gdi.Dispose();
img.Dispose();
}
本站文章如无特殊说明,均为本站原创,如若转载,请注明出处:asp.net 验证码的简单制作(vb.net+C#) - Python技术站