C# WinForm 登录界面的图片验证码(区分大小写+不区分大小写)
一、功能界面
二、创建一个产生验证码的类Class1
(1)生成随机验证码字符串,用的是Random随机函数
(2)创建验证码图片,将该字符串画在PictureBox控件中
Class1.cs:
usingSystem; usingSystem.Collections.Generic; usingSystem.Linq; usingSystem.Text; usingSystem.Threading.Tasks; usingSystem.Drawing;//图片 usingSystem.Windows.Forms; namespaceValidCodeTest { publicclassClass1 { #region验证码功能 //////生成随机验证码字符串 /// publicstaticstringCreateRandomCode(intCodeLength) { intrand; charcode; stringrandomCode=String.Empty;//随机验证码 //生成一定长度的随机验证码 //Randomrandom=newRandom();//生成随机数对象 for(inti=0;i///创建验证码图片 /// publicstaticvoidCreateImage(stringstrValidCode,PictureBoxpbox) { try { intRandAngle=45;//随机转动角度 intMapWidth=(int)(strValidCode.Length*21); Bitmapmap=newBitmap(MapWidth,28);//验证码图片—长和宽 //创建绘图对象Graphics Graphicsgraph=Graphics.FromImage(map); graph.Clear(Color.AliceBlue);//清除绘画面,填充背景色 graph.DrawRectangle(newPen(Color.Black,0),0,0,map.Width-1,map.Height-1);//画一个边框 graph.SmoothingMode=System.Drawing.Drawing2D.SmoothingMode.AntiAlias;//模式 Randomrand=newRandom(); //背景噪点生成 PenblackPen=newPen(Color.LightGray,0); for(inti=0;i<50;i++) { intx=rand.Next(0,map.Width); inty=rand.Next(0,map.Height); graph.DrawRectangle(blackPen,x,y,1,1); } //验证码旋转,防止机器识别 char[]chars=strValidCode.ToCharArray();//拆散字符串成单字符数组 //文字居中 StringFormatformat=newStringFormat(StringFormatFlags.NoClip); format.Alignment=StringAlignment.Center; format.LineAlignment=StringAlignment.Center; //定义颜色 Color[]c={Color.Black,Color.Red,Color.DarkBlue,Color.Green,Color.Orange,Color.Brown,Color.DarkCyan,Color.Purple}; //定义字体 string[]font={"Verdana","MicrosoftSansSerif","ComicSansMS","Arial","宋体"}; for(inti=0;i 三、调用
(1)更新验证码
(2)验证(区分大小写)
(3)验证(不区分大小写)Form1.cs:
usingSystem; usingSystem.Collections.Generic; usingSystem.ComponentModel; usingSystem.Data; usingSystem.Drawing; usingSystem.Linq; usingSystem.Text; usingSystem.Threading.Tasks; usingSystem.Windows.Forms; usingValidCodeTest; namespaceValidCode { publicpartialclassForm1:Form { publicForm1() { InitializeComponent(); } #region验证码 privateconstintValidCodeLength=4;//验证码长度 privateStringstrValidCode="";//验证码 //调用自定义函数,更新验证码 privatevoidUpdateValidCode() { strValidCode=Class1.CreateRandomCode(ValidCodeLength);//生成随机验证码 if(strValidCode=="")return; Class1.CreateImage(strValidCode,pbox1);//创建验证码图片 } #endregion privatevoidpbox1_Click(objectsender,EventArgse) { UpdateValidCode();//点击更新验证码 } privatevoidForm1_Load(objectsender,EventArgse) { UpdateValidCode();//加载更新验证码 } //////验证(区分大小写) /// privatevoidbtn1_Click(objectsender,EventArgse) { stringvalidcode=txtValidCode.Text.Trim(); char[]ch1=validcode.ToCharArray(); char[]ch2=strValidCode.ToCharArray(); intCount1=0;//字母个数 intCount2=0;//数字个数 if(String.IsNullOrEmpty(validcode)!=true)//验证码不为空 { for(inti=0;i='a'&&ch1[i]<='z')||(ch1[i]>='A'&&ch1[i]<='Z'))//字母 { if(ch1[i]==ch2[i]) { Count1++; } } else//数字 { if(ch1[i]==ch2[i]) { Count2++; } } } intCountSum=Count1+Count2; if(CountSum==strValidCode.Length) { MessageBox.Show("验证通过","提示",MessageBoxButtons.OK,MessageBoxIcon.Information); UpdateValidCode(); txtValidCode.Text=""; txtValidCode.Focus(); } else { MessageBox.Show("验证失败","警告",MessageBoxButtons.OK,MessageBoxIcon.Exclamation); UpdateValidCode();//更新验证码 txtValidCode.Text=""; txtValidCode.Focus(); } } else//验证码为空 { MessageBox.Show("请输入验证码","提示",MessageBoxButtons.OK,MessageBoxIcon.Information); UpdateValidCode();//更新验证码 txtValidCode.Text=""; txtValidCode.Focus(); } } /// ///验证(不区分大小写) /// privatevoidbtn2_Click(objectsender,EventArgse) { stringvalidcode=txtValidCode.Text.Trim(); if(String.IsNullOrEmpty(validcode)!=true)//验证码不为空 { if(validcode.ToLower()==strValidCode.ToLower()) { MessageBox.Show("验证通过","提示",MessageBoxButtons.OK,MessageBoxIcon.Information); UpdateValidCode(); txtValidCode.Text=""; txtValidCode.Focus(); } else { MessageBox.Show("验证失败","警告",MessageBoxButtons.OK,MessageBoxIcon.Exclamation); UpdateValidCode();//更新验证码 txtValidCode.Text=""; txtValidCode.Focus(); } } else//验证码为空 { MessageBox.Show("请输入验证码","提示",MessageBoxButtons.OK,MessageBoxIcon.Information); UpdateValidCode();//更新验证码 txtValidCode.Text=""; txtValidCode.Focus(); } } } }.exe测试文件下载:ValidCode_jb51.zip
参考文章:
https://www.jianshu.com/p/d89f22cf51bf以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持毛票票。
声明:本文内容来源于网络,版权归原作者所有,内容由互联网用户自发贡献自行上传,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任。如果您发现有涉嫌版权的内容,欢迎发送邮件至:czq8825#qq.com(发邮件时,请将#更换为@)进行举报,并提供相关证据,一经查实,本站将立刻删除涉嫌侵权内容。