C#自定义控件-验证码
效果如图
功能
1、可以设置验证码字符个数;
2、可以设置动态或者静态显示验证码;
3、点击验证码可以刷新;
4、鼠标进入验证码区提示点击刷新;
自定义验证码控件代码
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Windows.Forms;
namespace NumVerificationCode
{
public partial class numVerificationCode: UserControl
{
#region 外部接口
/// <summary>
/// 获取验证码内容
/// </summary>
public string RandomCode
{
get { return this.randomCode; }
}
/// <summary>
/// 是否动态显示验证码
/// </summary>
public bool Activity
{
set { activity = value; }
}
/// <summary>
/// 设置字符码个数
/// </summary>
public int CodeSum
{
set { codeSum = value; }
}
#endregion
// 声明
private string randomCode = ""; // 随机码字符串
private bool activity = false; // 验证码动态显示标记
private int codeSum = 6; // 验证码字符个数
Bitmap codeImage; // 验证码图片
Bitmap hintImage; // 提示图片
private int[] site = new int[2] { 0, 0 }; // 当前画字符位置
Random rand = new Random(); // 实例化随机对象,用于创建随机数
/// <summary>
/// 构造函数
/// </summary>
public numVerificationCode()
{
InitializeComponent();
this.freshCode();
}
/// <summary>
/// 验证码图片点击事件
/// </summary>
private void showCode_Click(object sender, EventArgs e)
{
this.freshCode();
}
/// <summary>
/// 创建设定长度字符串验证码
/// </summary>
private void CreateRandomCode()
{
string allChar = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
string oneCode; // 获取到的随机字符码
int index; // 判断字符码是否已存在标记
this.randomCode = "";
for (int i = 0; i < codeSum; i++)
{
do // 去重
{
oneCode = allChar[this.rand.Next(allChar.Length)].ToString();
index = this.randomCode.IndexOf(oneCode);
}
while (index != -1);
this.randomCode += oneCode;
}
}
/// <summary>
/// 往指定图片上绘制指定字符串
/// </summary>
/// <param name="image"><需要添加的图片/param>
/// <param name="randomCode"><需要添加的字符串/param>
/// <param name="cle"><true清除底色,false不清除/param>
/// <returns><绘制后的图片/returns>
private Bitmap CreateImage(Bitmap image, string randomCode, bool cle=false)
{
int imageWidth = this.showCode.Width; // 图像宽度
int imageHeight = this.showCode.Height; // 图像高度
Graphics g = Graphics.FromImage(image);
// 背景
if (cle)
{
g.Clear(Color.White);
Random rnd = new Random();
// 给背景添加随机生成的燥点
for (int i = 0; i < 100; i++)
{
int x = rnd.Next(imageWidth);
int y = rnd.Next(imageHeight);
SolidBrush c = new SolidBrush(Color.FromArgb(rnd.Next()));
g.FillRectangle(c, x, y, 2, 2);
}
// 给背景添加随机生成的干扰线
for (int i = 0; i < 50; i++)
{
int x1 = rnd.Next(imageWidth);
int x2 = rnd.Next(imageWidth);
int y1 = rnd.Next(imageHeight);
int y2 = rnd.Next(imageHeight);
Pen v = new Pen(Color.FromArgb(rnd.Next()), 2);
g.DrawLine(v, x1, y1, x2, y2);
}
}
// 字符串
Font font = new Font("宋体", 23, FontStyle.Bold);
SolidBrush s = new SolidBrush(Color.Blue);
g.DrawString(randomCode, font, s, site[0], site[1]); // 绘制文本
return image;
}
/// <summary>
/// 刷新验证码
/// </summary>
public void freshCode()
{
site[0] = 0; // 清空位置
site[1] = 0;
this.setWidgetSize(); // 设置控件大小
this.CreateRandomCode(); // 创建随机数
// 生成提示图片
hintImage = this.CreateImage(hintImage, "点击刷新", true);
// 生成验证码图片
if (activity)
{
// 动态码,定时器刷新显示
this.TimeUpdate.Enabled = true;
}
else
{
// 静态码
this.TimeUpdate.Enabled = false;
this.setStaticCode();
}
}
/// <summary>
/// 根据字符数设置控件大小
/// </summary>
private void setWidgetSize()
{
int width = 30 + 20 * (this.codeSum -1);
int height = 35;
this.Width = width;
this.Height = height;
this.showCode.Width = width;
this.showCode.Height = height;
codeImage = new Bitmap(width, height); // 生成验证码图片
hintImage = new Bitmap(width, height); // 生成提示图片
}
/// <summary>
/// 设置静态码
/// </summary>
private void setStaticCode()
{
this.showCode.Image = this.CreateImage(codeImage, randomCode, true);
}
/// <summary>
/// 定时器更新动态码
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void timer_Tick(object sender, EventArgs e)
{
for (int i=0; i< this.randomCode.Length; i++ )
{
site[0] = i * 20 + this.rand.Next(10);
site[1] = this.rand.Next(10);
if (i==0)
codeImage = this.CreateImage(codeImage, this.randomCode[i].ToString(), true);
else
codeImage = this.CreateImage(codeImage, this.randomCode[i].ToString());
}
this.showCode.Image = codeImage;
}
/// <summary>
/// 鼠标进入控件事件
/// </summary>
private void showCode_MouseEnter(object sender, EventArgs e)
{
this.showCode.Image = hintImage;
}
/// <summary>
/// 鼠标离开控件事件
/// </summary>
private void showCode_MouseLeave(object sender, EventArgs e)
{
this.showCode.Image = codeImage;
}
}
}
测试代码可自行编辑,接口为
RandomCode 获取显示的验证码,返回 string
Activity 设置是否动态显示验证码,接收 bool
CodeSum 设置验证码字符个数, 接收 int
源码下载地址