首页 > 编程 > .NET > 正文

asp.net实现验证码

2024-07-10 13:06:26
字体:
来源:转载
供稿:网友

#region 实现一个验证码的类

public class validatecode
{
private bitmap validateimage;
private graphics g;
public validatecode()
{
validateimage = new bitmap(120, 30, pixelformat.format24bpprgb);
g = graphics.fromimage(validateimage);
}

public void drawvalidatecode(page e, string i)
{
g.drawstring(i, new font("黑体",16,fontstyle.bold),new solidbrush(color.white),new pointf(2,4));
g.fillrectangle(new lineargradientbrush(new point(0,0), new point(120,30), color.fromargb(0,0,0,0),color.fromargb(255,255,255,255)),0,0,120,30);
//validateimage.save(e.outputstream, imageformat.jpeg);
g.save();
memorystream ms=new memorystream();
validateimage.save(ms,system.drawing.imaging.imageformat.gif);
e.response.clearcontent();
e.response.contenttype="image/gif";
e.response.binarywrite(ms.toarray());
e.response.end();
//validateimage.save(e.mappath("numimage/validateimage.gif"), imageformat.gif);
//e.end();
}
}

#endregion


private void makevalidatecode()
{
char[] s = new char[]{'0','1', '2','3','4','5','6','7','8','9','a'
,'b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q'
,'r','s','t','u','v','w','x','y','z','a','b','c','d','e','f','g'
,'h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w'
,'x','y','z'};
string num = "";
random r = new random();
for(int i = 0; i < 5; i++)
{
num += s[r.next(0, s.length)].tostring();
}
((loginuserinfo)session["loginuserinfo"]).validatenum = num;
textbox3.text = "";
}

也综合了别人的一部分代码。:)

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表