ASP.NET 实现验证码以及刷新验证码的小例子
实现代码
/// <summary>
/// 生成验证码图片,保存session名称verificationcode
/// </summary>
public static void createverificationcode()
{
int number;
string checkcode = string.empty;
//随机数种子
random randoms = new random();
for (int i = 0; i < 4; i++) //校验码长度为4
{
//随机的整数
number = randoms.next();
//字符从0-9,a-z中随机产生,对应的ascii码分别为
//48-57,65-90
number = number % 36;
if (number < 10)
{
number += 48;
}
else
{
number += 55;
}
checkcode += ((char)number).tostring();
}
//在session中保存校验码
system.web.httpcontext.current.session["verificationcode"] = checkcode;
//若校验码为空,则直接返回
if (checkcode == null || checkcode.trim() == string.empty)
{
return;
}
//根据校验码的长度确定输出图片的长度
system.drawing.bitmap image = new system.drawing.bitmap(55, 20);//(int)math.ceiling(convert.todouble(checkcode.length * 15))
//创建graphics对象
graphics g = graphics.fromimage(image);
try
{
//生成随机数种子
random random = new random();
//清空图片背景色
g.clear(color.white);
//画图片的背景噪音线 10条
//---------------------------------------------------
for (int i = 0; i < 10; i++)
{
//噪音线起点坐标(x1,y1),终点坐标(x2,y2)
int x1 = random.next(image.width);
int x2 = random.next(image.width);
int y1 = random.next(image.height);
int y2 = random.next(image.height);
//用银色画出噪音线
g.drawline(new pen(color.silver), x1, y1, x2, y2);
}
//---------------------------------------------------
//brush b = brushes.silver;
//g.fillrectangle(b, 0, 0, image.width, image.height);
//---------------------以上两种任选其一------------------------------
//输出图片中校验码的字体: 12号arial,粗斜体
font font = new font("arial", 12, (fontstyle.bold | fontstyle.italic));
//线性渐变画刷
lineargradientbrush brush = new lineargradientbrush(new rectangle(0, 0, image.width, image.height), color.blue, color.purple, 1.2f, true);
g.drawstring(checkcode, font, brush, 2, 2);
//画图片的前景噪音点 50个
for (int i = 0; i < 50; i++)
{
int x = random.next(image.width);
int y = random.next(image.height);
image.setpixel(x, y, color.fromargb(random.next()));
}
//画图片的边框线
g.drawrectangle(new pen(color.peru), 0, 0, image.width - 1, image.height - 1);
//创建内存流用于输出图片
using (memorystream ms = new memorystream())
{
//图片格式指定为png
image.save(ms, imageformat.jpeg);
//清除缓冲区流中的所有输出
system.web.httpcontext.current.response.clearcontent();
//输出流的http mime类型设置为"image/png"
system.web.httpcontext.current.response.contenttype = "image/jpeg";
//输出图片的二进制流
system.web.httpcontext.current.response.binarywrite(ms.toarray());
}
}
finally
{
//释放bitmap对象和graphics对象
g.dispose();
image.dispose();
}
}
创建一个aspx页面
<%@ page language="c#" autoeventwireup="true" codefile="authcode.aspx.cs" inherits="authcode" %>
<%help.createverificationcode(); %>
添加html代码,引用
<div class="positionr">
<label>验证码:</label>
<span class="style1"> *</span>
<input type="text" class="yanzm" runat="server" reg="^.+$" id="txtauthcode" tip="请输入验证码!" />
<img class="yanzm_img" src="authcode.aspx" alt="" id="imgauthcode" />
</div>
如何实现刷新?
<script type="text/javascript">
$("#imgauthcode").click(function () {
$(this).attr("src", "authcode.aspx?code=" + (new date()).gettime());
});
</script>
效果图