基本信息
源码名称:C#实现数字字母混合随机产生验证码
源码大小:0.04M
文件格式:.zip
开发语言:C#
更新时间:2017-08-19
友情提示:(无需注册或充值,赞助后即可获取资源下载链接)
嘿,亲!知识可是无价之宝呢,但咱这精心整理的资料也耗费了不少心血呀。小小地破费一下,绝对物超所值哦!如有下载和支付问题,请联系我们QQ(微信同号):78630559
本次赞助数额为: 2 元×
微信扫码支付:2 元
×
请留下您的邮箱,我们将在2小时内将文件发到您的邮箱
源码介绍
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 WindowsFormsApp7
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
if(textBox1 .Text .Trim ()==null||textBox1 .Text .Trim ()=="")
{
MessageBox.Show("请输入验证码!!", "警告", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
}
else
{
if(textBox1 .Text .Trim ()==strValidateCode )
{
MessageBox.Show("验证通过!", "通过", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
else
{
MessageBox.Show("验证码错误!!", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
}
private string checkCode = String.Empty;
private string strValidateCode;
public string GetRandomNumberString(int Number)
{
string strNumber = string.Empty;
Random theRandomNumber = new Random();
for (int i = 0; i < Number; i )
{
strNumber = theRandomNumber.Next(10).ToString();
}
return strNumber;
}
public string GetRandomAlpahabetString(int Number)
{
string strAlphabet = string.Empty;
Random theRandomAlphabet = new Random();
for (int i = 0; i < Number; i )
{
char ch = (char)theRandomAlphabet.Next(97, 122);
strAlphabet = ch;
}
return strAlphabet;
}
public string GetRandomAlphbetOrNumberString(int Number)
{
string strAlphabetOrNumber = string.Empty;
Random theRandomAlphabetOrNumber = new Random();
int ran = theRandomAlphabetOrNumber.Next(2);
int numAlphabet = theRandomAlphabetOrNumber.Next(Number);
int numNumber = Number - numAlphabet;
if (ran == 1)
{
for (int i = 0; i < numAlphabet; i )
{
char ch = (char)theRandomAlphabetOrNumber.Next(97, 122);
strAlphabetOrNumber = ch;
}
for (int i = 0; i < numNumber; i )
{
strAlphabetOrNumber = theRandomAlphabetOrNumber.Next(10);
}
}
else
{
for (int i = 0; i < numNumber; i )
{
strAlphabetOrNumber = theRandomAlphabetOrNumber.Next(10);
}
for (int i = 0; i < numAlphabet; i )
{
char ch = (char)theRandomAlphabetOrNumber.Next(97, 122);
strAlphabetOrNumber = ch;
}
}
return strAlphabetOrNumber;
}
private void CreateCheckCodeImage(string checkCode)
{
if (checkCode == null || checkCode.Trim() == String.Empty)
{
return;
}
int int_ImageWidth = strValidateCode.Length * 13;
Bitmap image = new Bitmap(int_ImageWidth, 20);
Graphics g = Graphics.FromImage(image);
try
{
Random random = new Random();
g.Clear(Color.White);
for (int i = 0; i < 25; i )
{
int x1 = random.Next(image.Width);
int x2 = random.Next(image.Width);
int x3 = random.Next(image.Width);
int x4 = random.Next(image.Width);
g.DrawLine(new Pen(Color.Silver), x1, x2, x3, x4);
}
Font font = new Font("Arial", 12, (System.Drawing.FontStyle.Bold | FontStyle.Italic));
System.Drawing.Drawing2D.LinearGradientBrush brush = new System.Drawing.Drawing2D.LinearGradientBrush(new Rectangle(0, 0, image.Width, image.Height), Color.Blue, Color.DarkRed, 1.2f, true);
g.DrawString(checkCode, font, brush, 2, 2);
for (int i = 0; i < 100; 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;
this.pictureBox1.Height = image.Height;
this.pictureBox1.Image = image;
}
catch (Exception error)
{
MessageBox.Show(error.Message);
g.Dispose();
}
}
private void RadomString()
{
strValidateCode = GetRandomAlphbetOrNumberString(4);
CreateCheckCodeImage(strValidateCode);
}
private void Form1_Load(object sender, EventArgs e)
{
RadomString();
}
private void pictureBox1_Click(object sender, EventArgs e)
{
RadomString();
}
}
}