读书人

【玩1玩】验证码

发布时间: 2012-08-08 14:32:45 作者: rapoo

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

C# code
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);                }            }        }    }} 



[解决办法]
不知道你是指什么居中,加上下面代码,文字在噪音区域居中
C# code
                SizeF size = g.MeasureString(txt, font);                g.DrawString(txt, font, brush, (image.Width-size.Width)/2, (image.Height-size.Height)/2);
[解决办法]
我要压缩包

读书人网 >C#

热点推荐