/// <summary>
/// 画出验证码区
/// </summary>
StringBuilder sb;
public void create()
{
Bitmap bt = new Bitmap(200, 50);
Graphics gp = Graphics.FromImage(bt);
Random ran = new Random();
Point p1 = new Point(20, 20);
gp.FillRectangle(new SolidBrush(Color.YellowGreen), 0, 0, 100, 40);
sb = new StringBuilder();
string s = "qwertyuiop1234QWERTYUIOPasdfghjkl5678ASDFGHJKLzxcvbnm90ZXCVBNM";
for (int i = 0; i < 5; i++)
{
string s1 = s.Substring(ran.Next(s.Length - 1), 1);
gp.DrawString(s1, new Font("宋体", 17, FontStyle.Bold | FontStyle.Italic),
new SolidBrush(Color.White), (i * 17), (ran.Next(0, 20)));
sb.Append(s1);
}
//画400 个点
for (int j = 0; j < 400; j++)
{
gp.DrawPie(new Pen(new SolidBrush(Color.Blue), 1), ran.Next(1, 100), ran.Next(1, 40), 1, 1, 180, 30);
}
//随机画5条横线
for (int j = 0; j < 5; j++)
{
gp.DrawLine(new Pen(new SolidBrush(Color.Blue), 1), new Point(ran.Next(0, 100), ran.Next(0, 40)), new Point(ran.Next(0, 100), ran.Next(0, 40)));
}
this.pictureBox1.Image = bt;
gp.Dispose();
}
/// <summary>
/// 加载事件
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void Form1_Load(object sender, EventArgs e)
{
create();
}
/// <summary>
/// 刷新事件
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void label1_Click(object sender, EventArgs e)
{
create();
}
/// <summary>
/// 验证事件
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void button1_Click(object sender, EventArgs e)
{
// 把"验证码字符串"和"要输入的字符串"实例化.把sb转化为toString()
if (sb.ToString();.Equals(textBox1.Text.Trim(), StringComparison.OrdinalIgnoreCase))//不区分大小写。
{
MessageBox.Show("验证成功");
Form2 f2 = new Form2();
this.Hide();
f2.Show();
}
else
{
MessageBox.Show("验证失败");
create();
}
}
有需要的拿去,欢迎指正... |