c# +winform验证码生成并验证(通过创建公共类来实现)
2012-10-20 19:51
447 查看
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace WindowsFormsApplication2
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
#region//调用验证码函数
CodeImage(CheckCode());
#endregion
}
private string CheckCode()
{
#region//生成随机数
int number;
char code;
string checkcode = string.Empty;//声明变量存储随机生成的4位英文或数字
Random random = new Random();//生成随机数
for (int i = 0; i < 4; i++)
{
number = random.Next();//返回非负随机数
if (number % 2 == 0)//判断数字是否为偶数
code = (char)('0' + (char)(number % 10));
else //如果不是偶数
code = (char)('A' + (char)(number % 26));
checkcode += "" + code.ToString(); //累加字符串
}
test.strUpper = checkcode.ToUpper();//转化成大写的验证码
test.strLower = checkcode.ToLower();//转化成小写的验证码
return checkcode;//返回生成的字符串
#endregion
}
private void CodeImage(string checkcode)
{
#region//生成背景图
if (checkcode == null || checkcode.Trim() == string.Empty)
return;
System.Drawing.Bitmap image = new System.Drawing.Bitmap((int)Math.Ceiling((checkcode.Length * 12.5)), 22);//可以控制显示验证码的区域
Graphics g = Graphics.FromImage(image);//创建Graphics对象
try
{
Random random = new Random();//生成随机生成器
g.Clear(Color.White);//清空图片背景色
for (int i = 0; i <5; i++)//画图片的背景噪音线
{
int x1 = random.Next(image.Width);
int x2 = random.Next(image.Width);
int y1 = random.Next(image.Height);
int y2 = random.Next(image.Height);
g.DrawLine(new Pen(Color.Black), x1, y1, x2, y2);//把两点坐标用线连接起来
}
Font font = new Font("Arial", 12, (System.Drawing.FontStyle.Bold));//指定字体大小和样式
g.DrawString(checkcode, font, new SolidBrush(Color.Red),2, 2);//绘制文本字符串
for (int i = 0; i < 150; i++)//画图片的前景噪点
{
int x = random.Next(image.Width);
int y = random.Next(image.Height);
image.SetPixel(x, y, Color.FromArgb(random.Next()));//设置像素
}
g.DrawRectangle(new Pen(Color.Silver), 0, 0, image.Width - 1, image.Height - 1);//绘制矩形
this.pictureBox1.Width = image.Width;//设置pictureBox1的宽度
this.pictureBox1.Height = image.Height;//设置pictureBox1的高度
this.pictureBox1.BackgroundImage = image;//设置pictureBox1的背景图像
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
#endregion
}
private void button1_Click(object sender, EventArgs e)
{
#region//刷新验证码
CodeImage(CheckCode());
#endregion
}
private void button2_Click(object sender, EventArgs e)
{
//如果文本框输入的验证码和随机生成的验证相等
if (textBox1.Text.Trim() == test.strUpper || textBox1.Text.Trim() == test.strLower)
MessageBox.Show("验证码正确");
else
MessageBox.Show("验证码错误");
}
}
}
//注释:其中strUpper, strLower为test类中的变量。以下截图为test创建过程
//功能测试截图
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace WindowsFormsApplication2
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
#region//调用验证码函数
CodeImage(CheckCode());
#endregion
}
private string CheckCode()
{
#region//生成随机数
int number;
char code;
string checkcode = string.Empty;//声明变量存储随机生成的4位英文或数字
Random random = new Random();//生成随机数
for (int i = 0; i < 4; i++)
{
number = random.Next();//返回非负随机数
if (number % 2 == 0)//判断数字是否为偶数
code = (char)('0' + (char)(number % 10));
else //如果不是偶数
code = (char)('A' + (char)(number % 26));
checkcode += "" + code.ToString(); //累加字符串
}
test.strUpper = checkcode.ToUpper();//转化成大写的验证码
test.strLower = checkcode.ToLower();//转化成小写的验证码
return checkcode;//返回生成的字符串
#endregion
}
private void CodeImage(string checkcode)
{
#region//生成背景图
if (checkcode == null || checkcode.Trim() == string.Empty)
return;
System.Drawing.Bitmap image = new System.Drawing.Bitmap((int)Math.Ceiling((checkcode.Length * 12.5)), 22);//可以控制显示验证码的区域
Graphics g = Graphics.FromImage(image);//创建Graphics对象
try
{
Random random = new Random();//生成随机生成器
g.Clear(Color.White);//清空图片背景色
for (int i = 0; i <5; i++)//画图片的背景噪音线
{
int x1 = random.Next(image.Width);
int x2 = random.Next(image.Width);
int y1 = random.Next(image.Height);
int y2 = random.Next(image.Height);
g.DrawLine(new Pen(Color.Black), x1, y1, x2, y2);//把两点坐标用线连接起来
}
Font font = new Font("Arial", 12, (System.Drawing.FontStyle.Bold));//指定字体大小和样式
g.DrawString(checkcode, font, new SolidBrush(Color.Red),2, 2);//绘制文本字符串
for (int i = 0; i < 150; i++)//画图片的前景噪点
{
int x = random.Next(image.Width);
int y = random.Next(image.Height);
image.SetPixel(x, y, Color.FromArgb(random.Next()));//设置像素
}
g.DrawRectangle(new Pen(Color.Silver), 0, 0, image.Width - 1, image.Height - 1);//绘制矩形
this.pictureBox1.Width = image.Width;//设置pictureBox1的宽度
this.pictureBox1.Height = image.Height;//设置pictureBox1的高度
this.pictureBox1.BackgroundImage = image;//设置pictureBox1的背景图像
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
#endregion
}
private void button1_Click(object sender, EventArgs e)
{
#region//刷新验证码
CodeImage(CheckCode());
#endregion
}
private void button2_Click(object sender, EventArgs e)
{
//如果文本框输入的验证码和随机生成的验证相等
if (textBox1.Text.Trim() == test.strUpper || textBox1.Text.Trim() == test.strLower)
MessageBox.Show("验证码正确");
else
MessageBox.Show("验证码错误");
}
}
}
//注释:其中strUpper, strLower为test类中的变量。以下截图为test创建过程
//功能测试截图
相关文章推荐
- C#Winform实现验证码生成
- js实现登录界面验证码生成及验证
- PHP 用session与gd库实现简单验证码生成与验证的类方法
- PHP 用session与gd库实现简单验证码生成与验证的类方法
- ASP.NET 验证码的生成和刷新代码(已验证通过非原创)-asp.net关注
- 在c#的winform登录窗体中,如何实现窗口验证码
- C#WinForm中 验证码(汉字)的实现
- 通过创建元素从而实现三个下拉框的联动效果(create.Element("option"))和提交表单时的验证p.match("请选择")
- 关于在c#中创建用户控件后,winform应用程序在调用中无法通过点击用户控件的子控件为其自动添加事件代码的问题
- .NET1.1下,使用C#自动生成Word2003文档(通过操作COM组件实现)
- C# Winform DataGridView 公共分页实现
- C#实现登陆验证码图片的动态生成
- ASPX验证码生成程序(C#代码一个页面实现)
- C#中Winform程序中如何实现多维表头【不通过第三方报表程序】
- C#生成中文汉字验证码的具体实现
- 【qtp脚本】实现自动创建目录并通过测试写字板生成指定个数的文件
- 浅谈:如何用C#实现简单的带有验证码及密码使用MD5加密的“登录”WinForm小程序
- C#调用C++生成的类(通过CLR类库实现)
- Struts2实现生成动态验证码并验证实例代码
- PHP 用session与gd库实现简单验证码生成与验证的类