第一步生成验证码
新增一个validatecode.aspx页面
在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;
public partial class validatecode : system.web.ui.page
{ //该页面将用于生成验证码图片
protected void page_load(object sender, eventargs e)
{ //调用函数将验证码生成图片
this.createcheckcodeimage(generatecheckcode());
}
private string generatecheckcode()
{ //产生五位的随机字符串
int number;
char code;
string checkcode = string.empty;
system.random random = new random();
for (int i = 0; i < 5; 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("checkcode", checkcode));
session["checkcode"] = checkcode;//用于客户端校验码比较
return checkcode;
}
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();
}
}
}
第二步测试验证码
建立test.aspx
form id="form1" runat="server">
<div>
<asp:textbox id="textbox1" runat="server"></asp:textbox>
<asp:button id="button1" runat="server" text="button" onclick="button1_click" /><br />
<asp:image id="image1" runat="server" imageurl="~/validatecode.aspx" />//该图片将用于显示验证码
</div>
</form>
test.aspx.cs代码
protected void page_load(object sender, eventargs e)
{
}
protected void button1_click(object sender, eventargs e)
{
if (this.textbox1.text.tostring().trim() == session["checkcode"].tostring()) { response.write("<script lauguage='javascript'>alert('验证成功');</script>"); }
}
新闻热点
疑难解答
图片精选