C#小程序—随机产生验证码

最近在家闲来无事,看了几天的书,头有点大。手痒写写代码。

1.目的

随机产生验证码。用C#GDI+绘图完成,由于我现在只会简单的winform程序,完成后在winform上显示。

                                               

2.思路

随机在一定的字符中挑选几个,然后将每个字符的各个属性(字体,大小,颜色)随机生成,接着画出来就可以了。

3.实现

3.1定义变量

这里的产生的字符数目和字体及颜色都可以自己添加。小建议:验证码所含的字符最好不要有歧义的,例如数字1和字母l。

char[] data = new char[6];  //用来装产生的随机字符,大小可变
        //字符颜色、字体等变化范围
string[] myFont = { "Times New Roman", "MS Mincho", "Book Antiqua", "Gungsuh", "PMingLiU", "Impact", "微软雅黑", "宋体" };
Color[] myColor = { Color.Black, Color.Red, Color.Blue, Color.Green, Color.Orange, Color.Brown, Color.Brown, Color.DarkBlue };
char[] myChar = { '2', '3', '4', '5', '6', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'J',
                               'K', 'L', 'M', 'N', 'P', 'R', 'S', 'T', 'W', 'X', 'Y','Z','a','b','c','d','e',
                           'f','g','h','j','k','m','n','p','r','s','t','w','x','y','z'};

3.2定义画布和bitmap等变量

背景为白色。从myBitmap创建画布。

Graphics g = this.CreateGraphics();
Bitmap myBitmap=new Bitmap(200,100);
g = Graphics.FromImage(myBitmap);
g.Clear(Color.White);

接下来是定义随机变量Random。

Random r = new Random(DateTime.Now.Second);
下面利用r产生的随机数是否一样并不重要,验证码中的字符可以相同。

3.3画

验证码还含有一些其他的线和点,来混淆视线。

	    
            //画燥线
            for (int i = 0; i < 10; i++)
            {
                int x1 = r.Next(200);
                int y1 = r.Next(100);
                int x2 = r.Next(200);
                int y2 = r.Next(100);
                Color clr = myColor[r.Next(myColor.Length)];
                g.DrawLine(new Pen(clr), new Point(x1, y1), new Point(x2, y2));
            }

            //画字符
            for (int i = 0; i < data.Length; i++)
                data[i] = (char)myChar[r.Next(myChar.Length)];
            for (int i = 0; i < data.Length; i++)
            {
                Font tempFont = new Font(myFont[r.Next(myFont.Length)], 18);
                g.DrawString(data[i].ToString(), tempFont,
                    new SolidBrush(myColor[r.Next(myColor.Length)]), (float)i * 20 + 40, (float)40);
            }
 
            //画燥点
            for (int i = 0; i < 100; i++)
            {
                int x = r.Next(myBitmap.Width);
                int y = r.Next(myBitmap.Height);
                Color tempColor = myColor[r.Next(myColor.Length)];
                myBitmap.SetPixel(x, y, tempColor);
            };

3.4显示和保存

画完成了,但这是并没有显示出来,这时只画在myBitmap中,为了将他显示出来,定义另一个画布。

Graphics thisGr;
thisGr = this.CreateGraphics();
thisGr.DrawImage(myBitmap, new Point(90, 35));

这就完成了。

                                 

在退出前,不要忘记释放资源。

4.不足

产生的验证码不美观,且验证码中的字符没有倾斜的效果。

5.代码

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.IO;
using System.Drawing.Imaging;

namespace Ch53_随机产生验证码
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            thisGr = this.CreateGraphics();
        }

        char[] data = new char[6];  //用来装产生的随机字符,大小可变
        //字符颜色、字体等变化范围
        string[] myFont = { "Times New Roman", "MS Mincho", "Book Antiqua", "Gungsuh", "PMingLiU", "Impact", "微软雅黑", "宋体" };
        Color[] myColor = { Color.Black, Color.Red, Color.Blue, Color.Green, Color.Orange, Color.Brown, Color.Brown, Color.DarkBlue };
        char[] myChar = { '2', '3', '4', '5', '6', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'J',
                               'K', 'L', 'M', 'N', 'P', 'R', 'S', 'T', 'W', 'X', 'Y','Z','a','b','c','d','e',
                           'f','g','h','j','k','m','n','p','r','s','t','w','x','y','z'};
        Graphics thisGr;

        private void btnSet_Click(object sender, EventArgs e)
        {
            Graphics g = this.CreateGraphics();
            Bitmap myBitmap=new Bitmap(200,100);
            g = Graphics.FromImage(myBitmap);
            g.Clear(Color.White);
            Random r = new Random(DateTime.Now.Second);

            //画燥线
            for (int i = 0; i < 10; i++)
            {
                int x1 = r.Next(200);
                int y1 = r.Next(100);
                int x2 = r.Next(200);
                int y2 = r.Next(100);
                Color clr = myColor[r.Next(myColor.Length)];
                g.DrawLine(new Pen(clr), new Point(x1, y1), new Point(x2, y2));
            }

            //画字符
            for (int i = 0; i < data.Length; i++)
                data[i] = (char)myChar[r.Next(myChar.Length)];
            for (int i = 0; i < data.Length; i++)
            {
                Font tempFont = new Font(myFont[r.Next(myFont.Length)], 18);
                g.DrawString(data[i].ToString(), tempFont,
                    new SolidBrush(myColor[r.Next(myColor.Length)]), (float)i * 20 + 40, (float)40);
            }
 
            //画燥点
            for (int i = 0; i < 100; i++)
            {
                int x = r.Next(myBitmap.Width);
                int y = r.Next(myBitmap.Height);
                Color tempColor = myColor[r.Next(myColor.Length)];
                myBitmap.SetPixel(x, y, tempColor);
            }

            myBitmap.Save("c:\\photo.gif");
            thisGr.DrawImage(myBitmap, new Point(90, 35));
            myBitmap.Dispose();
            g.Dispose();
        }

        private void Form1_FormClosing(object sender, FormClosingEventArgs e)
        {
            thisGr.Dispose();
        }
    }
}



原文地址:https://www.cnblogs.com/finlay/p/3234738.html