当前位置:编程学习 > C#/ASP.NET >>

C#winform中如何判断验证码?

我的验证码图片是用的pictureBox1控件显示的   我如何用textbox验证图片上的值

 

追问:我的是配合的 这个 该如何做?  谢谢指导

private void CodeImage(string checkCode)
        {
            if (checkCode == null || checkCode.Trim() == String.Empty)
                return;
            System.Drawing.Bitmap image = new System.Drawing.Bitmap((int)Math.Ceiling((checkCode.Length * 13.5)), 22);
            Graphics g = Graphics.FromImage(image);
            try
            {
                Random random = new Random();
                g.Clear(Color.White);
                for (int i = 0; i < 3; 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.Black), x1, y1, x2, y2);
                }
                Font font = new System.Drawing.Font("Arial", 12, (System.Drawing.FontStyle.Bold));

                g.DrawString(checkCode, font, new SolidBrush(Color.Red), 2, 2);
                for (int i = 0; i < 150; 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.BackgroundImage = image;             
               
            }
            catch { }
        }

答案:我写了个生成图片的方法,可以和您的CheckCode()方法配合使用。。返回Bitmap对象。。

        private Bitmap DrawVerifyCodePicture(string verifyCode)
        {
            int imageWidth = 15 * (verifyCode.Length + 1);//定义图片宽度。。
            int imageHeight = 30;//定义图片高度。。
            Color[] colors = {Color.Black, Color.Red, Color.DarkBlue, Color.Green, Color.Chocolate,
                             Color.Brown, Color.Purple, Color.DarkGoldenrod};//验证码颜色集合。。
            string[] fonts = { "Verdana", "Microsoft Sans Serif", "Comic Sans MS", "Arial", "宋体",
                             "华文隶书", "Arial Black", "幼圆"};//验证码字体集合。。
            Random rand = new Random((int)DateTime.Now.Ticks);//创建Random类的实例rand。。
            Bitmap image = new Bitmap(imageWidth, imageHeight);//创建一个图像实例。。
            Graphics graphics = Graphics.FromImage(image);//从该图创建一个绘画实例。。
            graphics.Clear(Color.LightCyan);//先清空画面,接着用颜色填充。。
            Pen pen = new Pen(Color.LightGray, 0);//定义pen,用于绘制背景点。。
            for (int i = 0; i < verifyCode.Length * 50; i++)
            {
                int x = rand.Next(imageWidth);//定义背景点横坐标。。
                int y = rand.Next(imageHeight);//定义背景点纵坐标。。
                graphics.DrawRectangle(pen, x, y, 1, 1);//在矩形框中绘制背景点。。
            }
            for (int i = 0; i < verifyCode.Length; i++)//逐个定义字符的颜色、字体、高度等,并绘制。。
            {
                int colorIndex = rand.Next(colors.Length);//定义验证码颜色索引值。。
                int fontIndex = rand.Next(fonts.Length);//定义验证码字体索引值。。
                Brush brush = new SolidBrush(colors[colorIndex]);//颜色。。
                Font font = new Font(fonts[fontIndex], 16, FontStyle.Bold);//字体。。
                string singleCode = verifyCode.Substring(i, 1);//提取单个字符。。
                int x = 5 + (i * 15);//定义字符绘制的横坐标。。
                int y = 2;//定义字符绘制的纵坐标。。
                if (i % 2 == 0)//用于控制所有验证码不在同一高度上。。
                {
                    y = 1;
                }
                graphics.DrawString(singleCode, font, brush, x, y);//开始绘制。。
            }
            graphics.Dispose();//释放对象。。
            return (image);
        }


使用的时候,这样使用。。

            verifyCode = CheckCode();//将verifyCode声明为全局的string对象,以便其它方法使用。。
            Bitmap image = DrawVerifyCodePicture(verifyCode);
            pictureBox1.Image = image;
            pictureBox1.Height = image.Height;
            pictureBox1.Width = image.Width;


验证的时候,直接把用户输入的和生成的verifyCode进行对比。。

            if (textBox1.Text.ToUpper() == verifyCode.ToUpper())//不区分大小写。。
            {
                MessageBox.Show("Right!");
            }
            else
            {
                MessageBox.Show("Wrong!");
            }



效果如下:



先产生数,再生成图片,textbox中的值和你生成的数比较,而不是图片

上一个:C#使用Thread.Sleep();方法
下一个:C#里对注册表的修改

CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,