asp.net验证码简单示例,可以是数字或英文或混合

发布时间:2007年09月13日      浏览次数:1676 次
=====================================================================
index.aspx源码
=====================================================================
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>asp.net验证码</title>
</head>
<body>
<form id="form1" runat="server">
<div>
验证码:<asp:TextBox ID="txtVail" runat="server"></asp:TextBox>
<img src="ValidateCode.aspx" />
 
<asp:Button ID="btnLogin" runat="server" OnClick="btnLogin_Click" Text="确定" /></div>
</form>
</body>
</html>
=====================================================================
index.aspx源码
=====================================================================
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void btnLogin_Click(object sender, EventArgs e)
{
if (this.txtVail.Text.Trim() == Session["validateCode"].ToString().Trim())
{
Response.Write("<font color=green>验证码输入正确!</font>");
}
else
{
Response.Write("<font color=red>验证出错!</font>");
}
}
}
=====================================================================
ValidateCode.aspx.cs源码
=====================================================================
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Drawing;
using System.Drawing.Imaging;
using Security;
namespace Security
{
public partial class ValidateCode : System.Web.UI.Page
{
// 验证码长度
private int codeLen = 2;
// 图片清晰度
private int fineness = 80;
// 图片宽度
private int imgWidth = 60;
// 图片高度
private int imgHeight = 24;
// 字体家族名称
private string fontFamily = "Times New Roman";
// 字体大小
private int fontSize = 15;
// 字体样式
private int fontStyle = 0;
// 绘制起始坐标 X
private int posX = 0;
// 绘制起始坐标 Y
private int posY = 0;
protected void Page_Load(object sender, EventArgs e)
{
string validateCode = CreateValidateCode();//生成验证码
Bitmap bitmap = new Bitmap(imgWidth, imgHeight);// 生成BITMAP图像
DisturbBitmap(bitmap);// 图像背景
DrawValidateCode(bitmap, validateCode);// 绘制验证码图像
bitmap.Save(Response.OutputStream,ImageFormat.Gif);// 保存验证码图像,等待输出
}
// 生成验证码
private string CreateValidateCode()
{
string validateCode = "";
Random random = new Random();// 随机数对象
for (int i = 0; i < codeLen; i++)
{
int n = random.Next(26); // 26: A - Z 字符
validateCode += (char)(n + 65); // 将数字转换成大写字母
int nn = random.Next(10); //数字
validateCode += nn.ToString();
//int nnn = random.Next(26); //26: a - z 字符
//validateCode += (char)(nnn + 97); // 将数字转换成小写字母
}
Session["validateCode"] = validateCode;
return validateCode;
}
// 图像背景
private void DisturbBitmap(Bitmap bitmap)
{
Random random = new Random();// 通过随机数生成
for (int i = 0; i < bitmap.Width; i++)
{
for (int j = 0; j < bitmap.Height; j++)
{
if (random.Next(90) <= this.fineness)
bitmap.SetPixel(i, j, Color.White);
}
}
}
// 绘制验证码图像,bitmap 图板,validateCode 验证码值
private void DrawValidateCode(Bitmap bitmap, string validateCode)
{
Graphics g = Graphics.FromImage(bitmap);// 获取绘制器对象
Font font = new Font(fontFamily, fontSize, FontStyle.Bold);// 设置绘制字体
g.DrawString(validateCode, font, Brushes.Black, posX, posY);// 绘制验证码图像
}
}
}
文章来源:桂林唯创网络
免责声明:本站相关技术文章信息部分来自网络,目的主要是传播更多信息,如果您认为本站的某些信息侵犯了您的版权,请与我们联系,我们会即时妥善的处理,谢谢合作!