12306网站推出“彩色动态验证码机制”,新版验证码不但经常出现字符叠压,还不停抖动,不少人大呼“看不清”,称“那个验证码,是毕加索的抽象画么!”铁总客服则表示:为了能正常购票只能这样。而多家抢票软件接近“报废”,引发不少网友不满的吐槽称“太抽象太艺术了”。
以前做项目有时候也会用到验证码,但基本都是静态的,这次也想凑凑12306的热闹。闲言少续,切入正题,先上代码。
实现方法:
public void showcode()
{
//对象实例化
validate gifvalidate = new validate();
#region 对验证码进行设置(不进行设置时,将以默认值生成)
//验证码位数,不小于4位
gifvalidate.validatecodecount = 4;
//验证码字体型号(默认13)
gifvalidate.validatecodesize = 13;
//验证码图片高度,高度越大,字符的上下偏移量就越明显
gifvalidate.imageheight = 23;
//验证码字符及线条颜色(需要参考颜色类)
gifvalidate.drawcolor = system.drawing.color.blueviolet;
//验证码字体(需要填写服务器安装的字体)
gifvalidate.validatecodefont = "arial";
//验证码字符是否消除锯齿
gifvalidate.fonttextrenderinghint = false;
//定义验证码中所有的字符(","分离),似乎暂时不支持中文
gifvalidate.allchar = "1,2,3,4,5,6,7,8,9,0,a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,w,x,y,z";
#endregion
//输出图像(session名称)
gifvalidate.outputvalidate("getcode");
}
调用主要方法:
public class validate
{
public string allchar = "1,2,3,4,5,6,7,8,9,0,a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,w,x,y,z";
public color drawcolor = color.blueviolet;
public bool fonttextrenderinghint = false;
public int imageheight = 0x17;
private byte truevalidatecodecount = 4;
protected string validatecode = "";
public string validatecodefont = "arial";
public float validatecodesize = 13f;
private void createimagebmp(out bitmap imageframe)
{
char[] charray = this.validatecode.tochararray(0, this.validatecodecount);
int width = (int) (((this.truevalidatecodecount * this.validatecodesize) * 1.3) + 4.0);
imageframe = new bitmap(width, this.imageheight);
graphics graphics = graphics.fromimage(imageframe);
graphics.clear(color.white);
font font = new font(this.validatecodefont, this.validatecodesize, fontstyle.bold);
brush brush = new solidbrush(this.drawcolor);
int maxvalue = (int) math.max((float) ((this.imageheight - this.validatecodesize) - 3f), (float) 2f);
random random = new random();
for (int i = 0; i < this.truevalidatecodecount; i++)
{
int[] numarray = new int[] { (((int) (i * this.validatecodesize)) + random.next(1)) + 3, random.next(maxvalue) };
point point = new point(numarray[0], numarray[1]);
if (this.fonttextrenderinghint)
{
graphics.textrenderinghint = textrenderinghint.singlebitperpixel;
}
else
{
graphics.textrenderinghint = textrenderinghint.antialias;
}
graphics.drawstring(charray[i].tostring(), font, brush, (pointf) point);
}
graphics.dispose();
}
private void createimagegif()
{
animatedgifencoder encoder = new animatedgifencoder();
memorystream stream = new memorystream();
encoder.start();
encoder.setdelay(5);
encoder.setrepeat(0);
for (int i = 0; i < 10; i++)
{
bitmap bitmap;
this.createimagebmp(out bitmap);
this.disposeimagebmp(ref bitmap);
bitmap.save(stream, imageformat.png);
encoder.addframe(image.fromstream(stream));
stream = new memorystream();
}
encoder.output(ref stream);
httpcontext.current.response.clearcontent();
httpcontext.current.response.contenttype = "image/gif";
httpcontext.current.response.binarywrite(stream.toarray());
stream.close();
stream.dispose();
}
private void createvalidate()
{
this.validatecode = "";
string[] strarray = this.allchar.split(new char[] { ',' });
int index = -1;
random random = new random();
for (int i = 0; i < this.validatecodecount; i++)
{
if (index != -1)
{
random = new random((i * index) * ((int) datetime.now.ticks));
}
int num3 = random.next(0x23);
if (index == num3)
{
this.createvalidate();
}
index = num3;
this.validatecode = this.validatecode + strarray[index];
}
if (this.validatecode.length > this.truevalidatecodecount)
{
this.validatecode = this.validatecode.remove(this.truevalidatecodecount);
}
}
private void disposeimagebmp(ref bitmap imageframe)
{
graphics graphics = graphics.fromimage(imageframe);
pen pen = new pen(this.drawcolor, 1f);
random random = new random();
point[] pointarray = new point[2];
for (int i = 0; i < 15; i++)
{
pointarray[0] = new point(random.next(imageframe.width), random.next(imageframe.height));
pointarray[1] = new point(random.next(imageframe.width), random.next(imageframe.height));
graphics.drawline(pen, pointarray[0], pointarray[1]);
}
graphics.dispose();
}
public void outputvalidate(string validatecodesession)
{
this.createvalidate();
this.createimagegif();
httpcontext.current.session[validatecodesession] = this.validatecode;
}
public byte validatecodecount
{
get
{
return this.truevalidatecodecount;
}
set
{
if (value > 4)
{
this.truevalidatecodecount = value;
}
}
}
}
以上就是实现asp.net的全部过程,还附有源码,希望可以帮到大家更好地了解asp.net验证码的生成方法。
更多12306动态验证码启发之asp.net实现动态gif验证码。