【玩一玩】验证码

newtee 2012-07-23 04:43:30
建模一轮培训结束了暑假终于到来,今天来玩玩C#写验证码。http://b225.photo.store.qq.com/psb?/V11GVizq1Caq4x/LtMkj.O9VS3.caaW3aHolTyy0kslvTI206g0wvqejrY!/b/Yd1yIoZPRgAAYgoSKoanRgAA
总体上实现了数字和字母的验证 但是发现数字和字母组成的验证码不能保持居中 求高手来解答
代码如下:

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 System.Drawing.Drawing2D;

namespace 验证码
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
public string txt = "";
private void Form1_Load(object sender, EventArgs e)
{
CreateImage();
}
private void CreateImage()
{
string[] r = new String[62]{ "0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "a", "b", "c", "d", "e", "f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z",
"A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q","R","S","T","U","V","W","X","Y","Z"};
Random x = new Random();
string str1 = r[x.Next(0, 62)], str2 =r[x.Next(0, 62)], str3 =r[x.Next(0, 62)], str4 =r[x.Next(0, 62)];
txt = str1 + str2 + str3 + str4;
if (txt == null || txt == String.Empty)
{
return;
}
Bitmap image = new Bitmap((int)Math.Ceiling((txt.Length*15.0)), 20);
Graphics g = Graphics.FromImage(image);
try
{
//生成随机生成器
Random random = new Random();
//清空图片背景色以白色填充
g.Clear(Color.White);
//画图片的背景噪音线
for (int i = 0; i < 3; i++)
{
Point tem_Point_1 = new Point(random.Next(image.Width), random.Next(image.Height));
Point tem_Point_2 = new Point(random.Next(image.Width), random.Next(image.Height));
g.DrawLine(new Pen(Color.Black), tem_Point_1, tem_Point_2);
}
Font font = new Font("Arial", 12, (FontStyle.Bold));
LinearGradientBrush brush = new LinearGradientBrush(new Rectangle(0, 0, image.Width, image.Height), Color.Pink , Color.Red, 1.2f, true);
g.DrawString(txt, font, brush, 2, 2);
//画图片的前景噪音点
for (int i = 0; i < 100; i++)
{
Point tem_point = new Point(random.Next(image.Width), random.Next(image.Height));
image.SetPixel(tem_point.X, tem_point.Y, Color.FromArgb(random.Next()));
}
//画图片的边框线
g.DrawRectangle(new Pen(Color.Silver), 0, 0, image.Width - 1, image.Height - 1);
pictureBox1.Image = image;
}
catch (Exception e)
{
MessageBox.Show(e.Message);
}
}

private void button2_Click(object sender, EventArgs e)
{
CreateImage();
}

private void button1_Click(object sender, EventArgs e)
{
if (textBox1.Text.Trim() =="")
{
return;
}
else
{
if (textBox1.Text.Trim().ToLower() == txt.ToLower())
{
MessageBox.Show("提示:输入正确", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
else
{
MessageBox.Show("提示:验证码输入错误,请重新输入", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
}
}

}
}
...全文
286 13 打赏 收藏 转发到动态 举报
写回复
用AI写文章
13 条回复
切换为时间正序
请发表友善的回复…
发表回复
bookc-man 2012-07-23
  • 打赏
  • 举报
回复
我要压缩包
newtee 2012-07-23
  • 打赏
  • 举报
回复
[Quote=引用 10 楼 的回复:]

这是C#的内容?
[/Quote]
是的啊 C#博大精深啊 慢慢研究
newtee 2012-07-23
  • 打赏
  • 举报
回复
[Quote=引用 9 楼 的回复:]

靠,你说的,给我的,别忘了
[/Quote]
好的呢 给你源码
  • 打赏
  • 举报
回复
这是C#的内容?
bookc-man 2012-07-23
  • 打赏
  • 举报
回复
靠,你说的,给我的,别忘了
q568022847 2012-07-23
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 的回复:]

GIF的验证码其实也挺好玩的。
[/Quote]
是滴
q107770540 2012-07-23
  • 打赏
  • 举报
回复
泪寒雪 2012-07-23
  • 打赏
  • 举报
回复
其实这个问题还是小有难度的 就像那个递归的东西 看似简单 但是真正真研究起来还是有点疼的 慢慢学习吧 做个标记 .......
newtee 2012-07-23
  • 打赏
  • 举报
回复
newtee 2012-07-23
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 的回复:]

不知道你是指什么居中,加上下面代码,文字在噪音区域居中
C# code

SizeF size = g.MeasureString(txt, font);
g.DrawString(txt, font, brush, (image.Width-size.Width)/2, (image.Height-size.Height)/2……
[/Quote]
谢谢1楼
铜臂阿铁木 2012-07-23
  • 打赏
  • 举报
回复
GIF的验证码其实也挺好玩的。
bdmh 2012-07-23
  • 打赏
  • 举报
回复
不知道你是指什么居中,加上下面代码,文字在噪音区域居中

SizeF size = g.MeasureString(txt, font);
g.DrawString(txt, font, brush, (image.Width-size.Width)/2, (image.Height-size.Height)/2);

110,532

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧