C#实现验证码功能
作者:Just Do Its 时间:2021-11-28 22:20:27
本文实例为大家分享了C#实现验证码功能的具体代码,供大家参考,具体内容如下
分析
需要四个字符(字母(大小写)+数字)
将四个字符连接成字符串
将连接的字符串写入label中,在label中显示验证码
解决办法
1、创建随机数生成器(Random)
2、使用for循环4次(验证码四个字符)
3、将4个随机字母随机数字连接成一个字符串
4、将链接好的字符串写入label中
所用到的属性,循环以及数据类型的转换
用到Random随机生成器类(先实例化)Random s = new Random();
FromArgb方法label1.ForeColor = Color.FromArgb(s.Next(0, 255), s.Next(0, 255), s.Next(0, 255));字体随机颜色
用到for循环(验证码为四位所以循环四次)
将数字转化为字符(大写字母) (char)s.Next(65,91)显式转换
将字符转化为字符串(ToString方法)字符后面直接点ToString((char)s.Next(65,91)).Tostring
将数字转化为字符串(ToString)
具体代码如下:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace Test_Security_Code
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
//分析:验证码为4个字符,产生数字大小写字母随机,将四个字符连接成一个字符串,将字符串写入Label中
string c = "";//初始化验证码
Random s = new Random();//随机数生成器
for (int i = 0; i < 4; i++)//组成(随机四位0~9数字、大小写字母)
{
int b = s.Next(0,3);//b表示验证码类型,此随机数为0、1、2
//验证码是数字是字母随机(0表示大写字母,1表示数字,2表示小写字母)
if (b==0)//如果类型是大写字母
{
//此处将数字转化为字符型,将字符转化为字符串
c += ((char)s.Next(65,91)).ToString();
}
if (b==1)//如果是数字
{
//将数字转化为字符串
c += s.Next(0, 10).ToString();
}
if (b==2)//如果是小写字母
{
c += ((char)s.Next(97,122)).ToString();
}
label1.Text = c;//将c中的值赋给label文本
//设置label中字体颜色
label1.ForeColor = Color.FromArgb(s.Next(0, 255), s.Next(0, 255), s.Next(0, 255));
//设置label中字体大小
label1.Font = new Font("黑体",s.Next(10,30));
}
}
private void Form1_Load(object sender, EventArgs e)
{
button1.Text = "看不清换一张";
}
}
}
效果图
来源:https://blog.csdn.net/liu991029/article/details/105463438
标签:C#,验证码
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
java Long类型转为String类型的两种方式及区别说明
2022-11-12 18:03:38
完美解决springboot项目出现”java: 错误: 无效的源发行版:17“问题(图文详解)
2023-08-29 02:15:32
![](https://img.aspxhome.com/file/2023/2/108402_0s.png)
Java进程间通信之消息队列
2023-05-24 01:44:27
![](https://img.aspxhome.com/file/2023/3/97953_0s.png)
Java 数据结构与算法系列精讲之KMP算法
2023-05-06 14:55:55
![](https://img.aspxhome.com/file/2023/7/64907_0s.jpg)
SpringSceurity实现短信验证码登陆
2023-06-23 00:37:35
![](https://img.aspxhome.com/file/2023/2/77102_0s.jpg)
解决JAVA遍历List集合,删除数据时出现的问题
2021-12-25 15:38:03
C#用户定义类型转换详解
2022-06-07 11:44:32
Java之SSM中bean相关知识汇总案例讲解
2021-11-10 06:16:26
![](https://img.aspxhome.com/file/2023/9/68689_0s.jpg)
java封装前端查询条件通用版
2023-06-24 12:06:15
IDEA项目maven project没有出现plugins和Dependencies问题
2021-08-08 10:59:53
![](https://img.aspxhome.com/file/2023/1/61871_0s.png)
SpringCloud Function SpEL注入漏洞分析及环境搭建
2022-07-03 05:18:21
![](https://img.aspxhome.com/file/2023/9/70949_0s.png)
详解Android的网络数据存储
2022-08-22 01:39:39
C#实现AddRange为数组添加多个元素的方法
2023-06-26 13:34:25
Android 下的 QuickJS Binding 库特性使用详解
2022-08-08 00:24:24
详解SpringMVC验证框架Validation特殊用法
2023-06-08 22:23:29
SpringMVC Interceptor拦截器使用教程
2022-05-11 02:17:22
![](https://img.aspxhome.com/file/2023/8/67098_0s.png)
Android RecyclerView的刷新分页的实现
2022-10-31 15:40:34
![](https://img.aspxhome.com/file/2023/8/90148_0s.gif)
Java并发编程之阻塞队列详解
2022-06-20 02:08:19
![](https://img.aspxhome.com/file/2023/1/78381_0s.png)
C# Directory.GetFiles()函数案例详解
2022-12-10 04:51:02
解决mybatis plus 驼峰式命名规则问题
2022-09-29 11:24:34
![](https://img.aspxhome.com/file/2023/2/62712_0s.png)