欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

asp.net下生成英文字符数字验证码的代码

程序员文章站 2024-03-09 11:00:11
复制代码 代码如下:using system; using system.data; using system.configuration; using system.we...

复制代码 代码如下:

using system;
using system.data;
using system.configuration;
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.drawing2d;

public partial class _default : system.web.ui.page
{
protected void page_load(object sender, eventargs e)
{

bitmap bmp = new bitmap(50, 25);
graphics g = graphics.fromimage(bmp);
solidbrush sb = new solidbrush(getcolor());
g.drawstring(checknumber(), new font("宋体", 16), sb, 0, 0);
bmp.save(response.outputstream, system.drawing.imaging.imageformat.gif);

}
public static string checknumber()
{
string checkcode = "abcdefghijklmnopqrstuvwxyz0123456789abcdefghijklmnopqrstuvwxyz";
int len = 4;
string tmpstr = "";
int irandnum;
random rnd = new random();
for (int i = 0; i < len; i++)
{
irandnum = rnd.next(checkcode.length);
tmpstr += checkcode[irandnum];
}
return tmpstr;
}
private color getcolor()
{
random r = new random();
return color.fromargb(r.next(256), r.next(256), r.next(256));
}
}