#region"自定义"//验证码
private void createcheckcodeimage()
{
session["checkcode"]=null;
string checkcode=this.createcheckcode(4).trim();
if(checkcode==null || checkcode==string.empty)
{
return;
}
else
session["checkcode"]=checkcode;
system.drawing.bitmap image = new system.drawing.bitmap((int)math.ceiling(((double)checkcode.length * 13)), 24);
graphics g = graphics.fromimage(image);//http://localhost/dx/global.asax
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", 13, (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();
}
}
private string createcheckcode(int length)
{
int number;
char code=(char)0;
string checkcode = string.empty;
system.random random = new random();
for(int i=0; i<length;i++)
{
number = random.next();
//if(number % 2 == 0)
code = (char)('0' + (char)(number % 10));
// else
// code = (char)('a' + (char)(number % 26));
checkcode += code.tostring();
}
return checkcode;
}
#endregion
新闻热点
疑难解答