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; using Microsoft;namespace CheckCode { public partial class Form1 : Form { public Form1() { InitializeComponent(); } string checkCode = String.Empty; private void Form1_Load(object sender, EventArgs e) {
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Microsoft;namespace CheckCode
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
string checkCode = String.Empty; private void Form1_Load(object sender, EventArgs e)
{
this.labelX1.Visible = false;
this.pictureBox1.Visible = false;
this.linkLabel1.Visible = false;
this.textBoxX1.GotFocus += new EventHandler(textBox1_GotFocus);
}
private Image CreateCheckCodeImage(string checkCode)
{
if (checkCode == null || checkCode.Trim() == String.Empty)
return null ; System.Drawing.Bitmap image = new System.Drawing.Bitmap((int)Math.Ceiling((checkCode.Length * 20.0)), 28);
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 y1 = random.Next(image.Height);
int y2 = random.Next(image.Height); g.DrawLine(new Pen(Color.Silver), x1, y1, x2, y2);
} Font font = new System.Drawing.Font("Arial", 18, (System.Drawing.FontStyle.Bold | System.Drawing.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);
System.IO.MemoryStream ms = new System.IO.MemoryStream();
image.Save(ms, System.Drawing.Imaging.ImageFormat.Gif);
return image;
}
finally
{
//g.Dispose();
//image.Dispose();
}
}
private string GenerateCheckCode()
{
//得到随机文字
int number;
char code;
System.Random random = new Random(); for (int i = 0; i < 5; i++)
{
number = random.Next(); if (number % 2 == 0)
code = (char)('0' + (char)(number % 10));
else
code = (char)('A' + (char)(number % 26)); checkCode += code.ToString();
}
return checkCode;
} private void textBox1_GotFocus(object sender, EventArgs e)
{
//首次得到焦点触发 显示验证文字
if (this.labelX1.Visible == false && this.pictureBox1.Visible == false && this.linkLabel1.Visible ==false)
{
checkCode = String.Empty;
this.labelX1.Visible = true;
this.pictureBox1.Visible = true;
this.linkLabel1.Visible = true; this.labelX1.Text = GenerateCheckCode();
this.pictureBox1.Image = this.CreateCheckCodeImage(this.labelX1.Text);
}
else
{ } }
private void buttonX1_Click(object sender, EventArgs e)
{ if (this.textBoxX1.Text.ToUpper().Trim()=="")
{
MessageBoxEx.Show("请输入验证码", "提示");
checkCode = String.Empty;
this.labelX1.Text = GenerateCheckCode();
this.pictureBox1.Image = this.CreateCheckCodeImage(this.labelX1.Text);
return;
}
if (this.textBoxX1.Text.ToUpper().Trim() !=checkCode)
{
MessageBoxEx.Show("验证码输入错误", "提示");
checkCode = String.Empty;
this.labelX1.Text = GenerateCheckCode();
this.pictureBox1.Image = this.CreateCheckCodeImage(this.labelX1.Text);
return;
}
else
{
MessageBoxEx.Show("验证信息正确", "提示");
}
} private void linkLabel1_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
{
//看不清,换一张
checkCode = String.Empty;
this.labelX1.Text = GenerateCheckCode();
this.pictureBox1.Image = this.CreateCheckCodeImage(this.labelX1.Text); }
}
}
//窗体中加上代码中出现的控件 textbox,picturebox,linklabel,label(可在代码中删掉),button