C#生成漂亮验证码完整代码类
话不多说,请看代码:
usingSystem; usingSystem.Web; usingSystem.Drawing; usingSystem.Security.Cryptography; namespaceDotNet.Utilities { //////验证码类 /// publicclassRand { #region生成随机数字 //////生成随机数字 /// ///生成长度 publicstaticstringNumber(intLength) { returnNumber(Length,false); } /// ///生成随机数字 /// ///生成长度 /// 是否要在生成前将当前线程阻止以避免重复 publicstaticstringNumber(intLength,boolSleep) { if(Sleep)System.Threading.Thread.Sleep(3); stringresult=""; System.Randomrandom=newRandom(); for(inti=0;i ///生成随机字母与数字 /// /// 生成长度 publicstaticstringStr(intLength) { returnStr(Length,false); } /// ///生成随机字母与数字 /// ///生成长度 /// 是否要在生成前将当前线程阻止以避免重复 publicstaticstringStr(intLength,boolSleep) { if(Sleep)System.Threading.Thread.Sleep(3); char[]Pattern=newchar[]{'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'}; stringresult=""; intn=Pattern.Length; System.Randomrandom=newRandom(~unchecked((int)DateTime.Now.Ticks)); for(inti=0;i ///生成随机纯字母随机数 /// /// 生成长度 publicstaticstringStr_char(intLength) { returnStr_char(Length,false); } /// ///生成随机纯字母随机数 /// ///生成长度 /// 是否要在生成前将当前线程阻止以避免重复 publicstaticstringStr_char(intLength,boolSleep) { if(Sleep)System.Threading.Thread.Sleep(3); char[]Pattern=newchar[]{'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'}; stringresult=""; intn=Pattern.Length; System.Randomrandom=newRandom(~unchecked((int)DateTime.Now.Ticks)); for(inti=0;i ///验证图片类 /// publicclassYZMHelper { #region私有字段 privatestringtext; privateBitmapimage; privateintletterCount=4;//验证码位数 privateintletterWidth=16;//单个字体的宽度范围 privateintletterHeight=20;//单个字体的高度范围 privatestaticbyte[]randb=newbyte[4]; privatestaticRNGCryptoServiceProviderrand=newRNGCryptoServiceProvider(); privateFont[]fonts= { newFont(newFontFamily("TimesNewRoman"),10+Next(1),System.Drawing.FontStyle.Regular), newFont(newFontFamily("Georgia"),10+Next(1),System.Drawing.FontStyle.Regular), newFont(newFontFamily("Arial"),10+Next(1),System.Drawing.FontStyle.Regular), newFont(newFontFamily("ComicSansMS"),10+Next(1),System.Drawing.FontStyle.Regular) }; #endregion #region公有属性 /// ///验证码 /// publicstringText { get{returnthis.text;} } //////验证码图片 /// publicBitmapImage { get{returnthis.image;} } #endregion #region构造函数 publicYZMHelper() { HttpContext.Current.Response.Expires=0; HttpContext.Current.Response.Buffer=true; HttpContext.Current.Response.ExpiresAbsolute=DateTime.Now.AddSeconds(-1); HttpContext.Current.Response.AddHeader("pragma","no-cache"); HttpContext.Current.Response.CacheControl="no-cache"; this.text=Rand.Number(4); CreateImage(); } #endregion #region私有方法 //////获得下一个随机数 /// ///最大值 privatestaticintNext(intmax) { rand.GetBytes(randb); intvalue=BitConverter.ToInt32(randb,0); value=value%(max+1); if(value<0)value=-value; returnvalue; } /// ///获得下一个随机数 /// ///最小值 /// 最大值 privatestaticintNext(intmin,intmax) { intvalue=Next(max-min)+min; returnvalue; } #endregion #region公共方法 /// ///绘制验证码 /// publicvoidCreateImage() { intint_ImageWidth=this.text.Length*letterWidth; Bitmapimage=newBitmap(int_ImageWidth,letterHeight); Graphicsg=Graphics.FromImage(image); g.Clear(Color.White); for(inti=0;i<2;i++) { intx1=Next(image.Width-1); intx2=Next(image.Width-1); inty1=Next(image.Height-1); inty2=Next(image.Height-1); g.DrawLine(newPen(Color.Silver),x1,y1,x2,y2); } int_x=-12,_y=0; for(intint_index=0;int_index///字体随机颜色 /// publicColorGetRandomColor() { RandomRandomNum_First=newRandom((int)DateTime.Now.Ticks); System.Threading.Thread.Sleep(RandomNum_First.Next(50)); RandomRandomNum_Sencond=newRandom((int)DateTime.Now.Ticks); intint_Red=RandomNum_First.Next(180); intint_Green=RandomNum_Sencond.Next(180); intint_Blue=(int_Red+int_Green>300)?0:400-int_Red-int_Green; int_Blue=(int_Blue>255)?255:int_Blue; returnColor.FromArgb(int_Red,int_Green,int_Blue); } /// ///正弦曲线Wave扭曲图片 /// ///图片路径 /// 如果扭曲则选择为True /// 波形的幅度倍数,越大扭曲的程度越高,一般为3 /// 波形的起始相位,取值区间[0-2*PI) publicSystem.Drawing.BitmapTwistImage(BitmapsrcBmp,boolbXDir,doubledMultValue,doubledPhase) { doublePI=6.283185307179586476925286766559; BitmapdestBmp=newBitmap(srcBmp.Width,srcBmp.Height); Graphicsgraph=Graphics.FromImage(destBmp); graph.FillRectangle(newSolidBrush(Color.White),0,0,destBmp.Width,destBmp.Height); graph.Dispose(); doubledBaseAxisLen=bXDir?(double)destBmp.Height:(double)destBmp.Width; for(inti=0;i =0&&nOldX =0&&nOldY 以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,同时也希望多多支持毛票票!