asp.net验证码图片生成示例
验证码是一张图片。我们需要在前台代码中写一段<img>,src指向一张页面(validateimage.aspx)。
<script language="javascript">
function changeimg() {
$("#imgcheckno").attr("src", "validateimage.aspx?r=" + getrandom(999));
}
function getrandom(n) { return math.floor(math.random() * n + 1) }
</script>
<div>
<img id="imgcheckno" src="validateimage.aspx" style="border-color: #000000; border-width: 1px;border-style: solid">
<span onclick="changeimg();">看不清?换一张</span>
</div>
<div>
<div class="labelcss">验证码:</div>
<div>
<asp:textbox id="tbxcheckno" runat="server" cssclass="tbxcss"></asp:textbox>
</div>
<div>
<asp:button id="btnsubmit" runat="server" text="立即注册" onclick="btnsubmit_click" />
</div>
validateimage.aspx 用来生产验证码图片,并将验证码的码值保存到cookie中。
代码如下:
public partial class validateimage : system.web.ui.page
{
protected void page_load(object sender, eventargs e)
{
this.createcheckcodeimage(rndnum());
}
/// <summary>生成验证码
/// </summary>
/// <returns></returns>
private string rndnum()
{
int number;
char code;
string checkcode = string.empty;
system.random random = new random();
for (int i = 0; i < 4; i++)
{
number = random.next();
if (number % 2 == 0)
code = (char)('0' + (char)(number % 10));
else
code = (char)('a' + (char)(number % 26));
checkcode += code.tostring();
}
response.cookies.add(new httpcookie("yzmcode", checkcode));
return checkcode;
}
/// <summary>向页面生成验证码gif图片
/// </summary>
/// <param name="checkcode"></param>
private void createcheckcodeimage(string checkcode)
{
if (checkcode == null || checkcode.trim() == string.empty)
return;
system.drawing.bitmap image = new system.drawing.bitmap((int)math.ceiling((checkcode.length * 12.5)), 22);
graphics g = graphics.fromimage(image);
try
{
//生成随机生成器
random random = new random();
//清空图片背景色
g.clear(color.white);
//画图片的背景噪音线
for (int i = 0; i < 25; i++)
{
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);
}
font font = new system.drawing.font("arial", 12, (system.drawing.fontstyle.bold | system.drawing.fontstyle.italic));
system.drawing.drawing2d.lineargradientbrush brush = new system.drawing.drawing2d.lineargradientbrush(new rectangle(0, 0, image.width, image.height), color.blue, color.darkred, 1.2f, true);
g.drawstring(checkcode, font, brush, 2, 2);
//画图片的前景噪音点
for (int i = 0; i < 100; 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.silver), 0, 0, image.width - 1, image.height - 1);
system.io.memorystream ms = new system.io.memorystream();
image.save(ms, system.drawing.imaging.imageformat.gif);
response.clearcontent();
response.contenttype = "image/gif";
response.binarywrite(ms.toarray());
}
finally
{
g.dispose();
image.dispose();
}
}
}
最后就是后台事件,判断当前验证码是否正确。
代码如下:
#region 页面控件事件
protected void btnsubmit_click(object sender, eventargs e)
{
if (string.compare(request.cookies["yzmcode"].value, tbxcheckno.text, true) != 0)
{
response.write("<script>alert('验证码错误!')</script>");
}
else
{
// response.write("<script>alert('验证吗正确!!!')</script>");
}
}
#endregion