ValidateImage.cs
public class ValidateImage
{
public string RndChars { get; set; } //生成的随机验证码
Random Rnd = new Random();
#region 生成随机验证码
public void CreateRnd()
{
this.RndChars = "";
char code;
for (int i = 1; i <= 4; i++)
{
if (Rnd.Next() % 2 == 0) //偶数生成数字
{
code = (char)('0' + Rnd.Next(0, 10));
}
else //奇数生成字母
{
code = (char)('A' + Rnd.Next(0, 26));
}
RndChars = RndChars + code.ToString();
}
}
#endregion
#region 将验证码生成图片
public byte[] CreateImage()
{
CreateRnd();//生成随机字符串
System.Drawing.Bitmap bitmap = new System.Drawing.Bitmap(RndChars.Length * 15, 26);
Graphics g = Graphics.FromImage(bitmap);
g.Clear(Color.Gray); //清空图片,设置背景为灰色
//画图片噪线
for (int i = 1; i <=Rnd.Next(10,30); i++)
{
int x1 = Rnd.Next(1, bitmap.Width);
int x2 = Rnd.Next(1, bitmap.Width);
int y1 = Rnd.Next(1, bitmap.Height);
int y2 = Rnd.Next(1, bitmap.Height);
g.DrawLine(new Pen(Color.Green), x1, y1, x2, y2);
}
//画验证码字符串
Font font = new Font("微软雅黑", 12, (FontStyle.Bold | FontStyle.Italic));
System.Drawing.Drawing2D.LinearGradientBrush brush = new System.Drawing.Drawing2D.LinearGradientBrush(new RectangleF(0, 0, bitmap.Width, bitmap.Height), Color.Red, Color.Blue,1.2f,true);
g.DrawString(this.RndChars, font, brush, 2, 2);
//画图片噪点
for (int i = 0; i <= Rnd.Next(50,100); i++)
{
int x = Rnd.Next(1, bitmap.Width);
int y = Rnd.Next(1, bitmap.Height);
bitmap.SetPixel(x, y, Color.Red);
}
//画图片边框线
g.DrawRectangle(new Pen(Color.Black), 0, 0, bitmap.Width-1, bitmap.Height-1);
MemoryStream ms = new MemoryStream();
bitmap.Save(ms, System.Drawing.Imaging.ImageFormat.Gif);
return ms.ToArray();
}
#endregion
}
验证码ASPX文件删除所有的HTML代码,保留如下:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Demo04_01.aspx.cs" Inherits="Demo04_01" %>
验证码ASPX对应的C#代码:
public partial class Demo04_01 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
ValidateImage validate = new ValidateImage();
byte[] bytes = validate.CreateImage();
Session["validate"] = validate.RndChars;
Response.ContentType = "image/gif";
Response.BinaryWrite(bytes);
}
}
验证码的使用:
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>实现验证码</title>
<style type="text/css">
#container{ text-align:center;}
.righttd
{
width: 460px;
}
.lefted
{
width: 196px;
}
.mytitle{ font-size:18px; font-weight:bold;}
</style>
<script type="text/javascript" src="js/jquery.js"></script>
<script type="text/javascript">
//alert(new Date());
$(function () {
$("#Refresh").click(function () {
$("#imgValidate").prop("src","Demo04_01.aspx?date=" + new Date());
})
})
</script>
</head>
<body>
<form id="form1" runat="server">
<div id="container">
<table>
<tr>
<td align="center" height="30" class="mytitle" colspan="2">用户登录</td>
</tr>
<tr>
<td align="right" class="lefted" height="30">用户名:</td>
<td align="left" class="righttd" height="30">
<asp:TextBox ID="txtAccount" runat="server"></asp:TextBox>
</td>
</tr>
<tr>
<td align="right" class="lefted" height="30">密码:</td>
<td align="left" class="righttd" height="30">
<asp:TextBox ID="txtPwd" runat="server" TextMode="Password"></asp:TextBox>
</td>
</tr>
<tr>
<td align="right" class="lefted" height="30">验证码:</td>
<td align="left" class="righttd" height="30">
<asp:TextBox ID="txtValidate" runat="server" Width="72px"></asp:TextBox>
<asp:Image ID="imgValidate" runat="server" />
<a href="javascript:void(0);" id="Refresh">刷新</a>
</td>
</tr>
<tr>
<td align="right" class="lefted" height="30"></td>
<td align="left" class="righttd" height="30">
<asp:Button ID="btLogin" runat="server" onclick="btLogin_Click" Text="登 录" />
</td>
</tr>
<tr>
<td align="right" class="lefted" height="30"></td>
<td align="left" class="righttd" height="30">
<asp:Label ID="lblErrInfo" runat="server" ForeColor="Red"></asp:Label>
</td>
</tr>
</table>
</div>
</form>
</body>
</html>
public partial class Demo04 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
CreateValidateImg();
}
private void CreateValidateImg()
{
//方案一:写文件,然后在读取
//ValidateImage validate = new ValidateImage();
//byte[] bytes = validate.CreateImage();
//Session["validate"] = validate.RndChars;
//FileStream fs = new FileStream(Server.MapPath("img/valiate.jpg"), FileMode.Create);
//BinaryWriter bw = new BinaryWriter(fs);
////开始写入
//bw.Write(bytes, 0, bytes.Length);
////关闭流
//bw.Close();
//fs.Close();
//this.imgValidate.ImageUrl = "img/valiate.jpg";
//方案二:接受页面Response.write的结果
this.imgValidate.ImageUrl = "Demo04_01.aspx";
}
protected void btLogin_Click(object sender, EventArgs e)
{
if (Session["validate"] == null)
{
this.lblErrInfo.Text = "验证码过期";
this.imgValidate.ImageUrl = "Demo04_01.aspx"; //加载验证码
return;
}
if (this.txtValidate.Text.Equals(Session["validate"].ToString()) == false)
{
this.lblErrInfo.Text = "验证码错误!";
}
else
{
this.lblErrInfo.Text = "验证码正确";
}
}
}