asp.net mvc验证码类使用
验证码类
namespaceQJW.VerifyCode { //用法: //publicFileContentResultCreateValidate() //{ //ValidateCodevCode=newValidateCode(); //stringcode=vCode.CreateValidateCode(5); //Session["ValidateCode"]=code; //byte[]bytes=vCode.CreateValidateGraphic(code); //returnFile(bytes,"image/JPEG"); //} publicclassValidateCode { publicValidateCode() { } ///<summary> ///验证码的最大长度 ///</summary> publicintMaxLength { get{return10;} } ///<summary> ///验证码的最小长度 ///</summary> publicintMinLength { get{return1;} } ///<summary> ///生成验证码 ///</summary> ///<paramname="length">指定验证码的长度</param> ///<returns></returns> publicstringCreateValidateCode(intlength) { int[]randMembers=newint[length]; int[]validateNums=newint[length]; stringvalidateNumberStr=""; //生成起始序列值 intseekSeek=unchecked((int)DateTime.Now.Ticks); RandomseekRand=newRandom(seekSeek); intbeginSeek=(int)seekRand.Next(0,Int32.MaxValue-length*10000); int[]seeks=newint[length]; for(inti=0;i<length;i++) { beginSeek+=10000; seeks[i]=beginSeek; } //生成随机数字 for(inti=0;i<length;i++) { Randomrand=newRandom(seeks[i]); intpownum=1*(int)Math.Pow(10,length); randMembers[i]=rand.Next(pownum,Int32.MaxValue); } //抽取随机数字 for(inti=0;i<length;i++) { stringnumStr=randMembers[i].ToString(); intnumLength=numStr.Length; Randomrand=newRandom(); intnumPosition=rand.Next(0,numLength-1); validateNums[i]=Int32.Parse(numStr.Substring(numPosition,1)); } //生成验证码 for(inti=0;i<length;i++) { validateNumberStr+=validateNums[i].ToString(); } returnvalidateNumberStr; } ///<summary> ///创建验证码的图片 ///</summary> ///<paramname="containsPage">要输出到的page对象</param> ///<paramname="validateNum">验证码</param> publicbyte[]CreateValidateGraphic(stringvalidateCode) { Bitmapimage=newBitmap((int)Math.Ceiling(validateCode.Length*12.0),22); Graphicsg=Graphics.FromImage(image); try { //生成随机生成器 Randomrandom=newRandom(); //清空图片背景色 g.Clear(Color.White); //画图片的干扰线 for(inti=0;i<25;i++) { intx1=random.Next(image.Width); intx2=random.Next(image.Width); inty1=random.Next(image.Height); inty2=random.Next(image.Height); g.DrawLine(newPen(Color.Silver),x1,y1,x2,y2); } Fontfont=newFont("Arial",12,(FontStyle.Bold|FontStyle.Italic)); LinearGradientBrushbrush=newLinearGradientBrush(newRectangle(0,0,image.Width,image.Height), Color.Blue,Color.DarkRed,1.2f,true); g.DrawString(validateCode,font,brush,3,2); //画图片的前景干扰点 for(inti=0;i<100;i++) { intx=random.Next(image.Width); inty=random.Next(image.Height); image.SetPixel(x,y,Color.FromArgb(random.Next())); } //画图片的边框线 g.DrawRectangle(newPen(Color.Silver),0,0,image.Width-1,image.Height-1); //保存图片数据 MemoryStreamstream=newMemoryStream(); image.Save(stream,ImageFormat.Jpeg); //输出图片流 returnstream.ToArray(); } finally { g.Dispose(); image.Dispose(); } } ///<summary> ///得到验证码图片的长度 ///</summary> ///<paramname="validateNumLength">验证码的长度</param> ///<returns></returns> publicstaticintGetImageWidth(intvalidateNumLength) { return(int)(validateNumLength*12.0); } ///<summary> ///得到验证码的高度 ///</summary> ///<returns></returns> publicstaticdoubleGetImageHeight() { return22.5; } } }
使用方法
publicFileContentResultCreateValidate() { ValidateCodevCode=newValidateCode(); stringcode=vCode.CreateValidateCode(5); Session["ValidateCode"]=code; byte[]bytes=vCode.CreateValidateGraphic(code); returnFile(bytes,"image/JPEG"); }
HTML
<ahref="javascript:;"onclick="reloadcode();"><imgid="safecode"src="/common/CreateValidate"alt=""/></a>
视图JS
functionreloadcode(){ varverify=document.getElementById('safecode'); verify.setAttribute('src','/common/CreateValidate?'+Math.random()); }
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持毛票票。