C# winform登陆框验证码的实现方法
本文实例为大家分享了C#winform登陆框验证码的具体代码,供大家参考,具体内容如下
1、 新建一个简单的windows应用项目
在默认的Form1中添加如下控件:
1) Label:text=“输入验证码:”
2) TextBox:name=”txtValidCode”输入验证码的文本框
3) Image:name=”picValidCode”显示验证码的图片控件
4) Button:单击事件进行验证码验证
2、 新建一个产生验证码的核心类,并构建其一个实例:
ValidCodevalidCode=newValidCode(5,ValidCode.CodeType.Numbers)
usingSystem; usingSystem.Collections.Generic; usingSystem.Text; usingSystem.IO; usingSystem.Drawing; usingSystem.Drawing.Drawing2D; namespaceBigHorseLib.Security { publicclassValidCode { #regionPrivateFields privateconstdoublePI=3.1415926535897932384626433832795; privateconstdoublePI2=6.283185307179586476925286766559; //privatereadonlyint_wordsLen=4; privateint_len; privateCodeType_codetype; privatereadonlySingle_jianju=(float)18.0; privatereadonlySingle_height=(float)24.0; privatestring_checkCode; #endregion #regionPublicProperty publicstringCheckCode { get { return_checkCode; } } #endregion #regionConstructors //////publicconstructors /// ///验证码长度 /// 验证码类型:字母、数字、字母+数字 publicValidCode(intlen,CodeTypectype) { this._len=len; this._codetype=ctype; } #endregion #regionPublicField publicenumCodeType{Words,Numbers,Characters,Alphas} #endregion #regionPrivateMethods privatestringGenerateNumbers() { stringstrOut=""; System.Randomrandom=newRandom(); for(inti=0;i<_len;i++) { stringnum=Convert.ToString(random.Next(10000)%10); strOut+=num; } returnstrOut.Trim(); } privatestringGenerateCharacters() { stringstrOut=""; System.Randomrandom=newRandom(); for(inti=0;i<_len;i++) { stringnum=Convert.ToString((char)(65+random.Next(10000)%26)); strOut+=num; } returnstrOut.Trim(); } // privatestringGenerateAlphas() { stringstrOut=""; stringnum=""; System.Randomrandom=newRandom(); for(inti=0;i<_len;i++) { if(random.Next(500)%2==0) { num=Convert.ToString(random.Next(10000)%10); } else { num=Convert.ToString((char)(65+random.Next(10000)%26)); } strOut+=num; } returnstrOut.Trim(); } privateSystem.Drawing.BitmapTwistImage(BitmapsrcBmp,boolbXDir,doubledMultValue,doubledPhase) { System.Drawing.BitmapdestBmp=newBitmap(srcBmp.Width,srcBmp.Height); //将位图背景填充为白色 System.Drawing.Graphicsgraph=System.Drawing.Graphics.FromImage(destBmp); graph.FillRectangle(newSolidBrush(System.Drawing.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 3、 图片加载验证码、验证码验证
//图片加载验证码 picValidCode.Image=Bitmap.FromStream(validCode.CreateCheckCodeImage()) //验证 if(!this.txtValidCode.Text.Equals(_validCode.CheckCode)) { MessageBox.Show("请输入正确的验证码!",this.Text); this.txtValidCode.Focus(); return; }以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持毛票票。