今天写的一个 验证码生成 类

来源:互联网 发布:linux chown 递归修改 编辑:程序博客网 时间:2024/06/11 23:21

花了几节上机课写的

调用该类的 列子及源码  在我的资源里面

 

 

using System;

using System.Drawing;

 

namespace VerifyCode

{

    class VerifyCode

    {

        /// <summary>

        /// 生成的验证码(如果不区分大小写的话请自行在外部转换大小写字母)

        /// </summary>

        public string VerifyCodeString;

        /// <summary>

        /// 生成的验证码图像

        /// </summary>

        public Bitmap VerifyCodeImage;

        /// <summary>

        /// 生成验证码

        /// </summary>

        /// <param name="codelengt">验证码的长度</param>

        /// <param name="fontsize">验证码的大小</param>

        public void RandomVerifyCode(int codelengt, int fontsize,CodeType codetype)

        {

            Random r = new Random();

            Font CodeFont = new Font("微软雅黑", (float)fontsize, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(134)));

            Bitmap tmp = new Bitmap(codelengt * (fontsize + 4) + 10, fontsize + 25);

            Graphics g = Graphics.FromImage(tmp);

            char[] charcode = CodeChar(codetype);

            g.Clear(Color.White);

            g.DrawImage(creatrBackImage(tmp.Width,tmp.Height,codelengt*20), 0, 0);

            g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;

            SolidBrush b = new SolidBrush(Color.Black);

            for (int i = 0; i < codelengt; i++)

            {

                string  rtmp =charcode [ r.Next(charcode.Length )].ToString();

                VerifyCodeString += rtmp;

                b.Color = ranColor();

                g.DrawString(rtmp, CodeFont, b, 0 + i * (fontsize + 2) + r.Next(10), 0 + r.Next(5));

            }

            g.Dispose();

            VerifyCodeImage = tmp;

        }

        /// <summary>

        /// 生成随机颜色

        /// </summary>

        /// <returns>生成的随机颜色</returns>

        private Color ranColor()

        {

            int[] rgb = new int[3];

            Random rd = new Random();

            for (int i = 0; i < 3; i++)

            {

                rgb[i] = rd.Next(256);

            }

            System.Threading.Thread.Sleep(10);

            return Color.FromArgb(rgb[0], rgb[1], rgb[2]);

        }

        /// <summary>

        /// 创建背景图案 

        /// </summary>

        /// <param name="w">长度</param>

        /// <param name="h">高度</param>

        ///<param name="count">线条的数量</param>

        /// <returns>创建好的图案</returns>      

        private Bitmap creatrBackImage(int w, int h,int count)

        {

            Bitmap tmpimage = new Bitmap(w, h);

            Graphics g = Graphics.FromImage(tmpimage);

            Pen line = new Pen(Color.FromArgb(133, 133, 133));

            Point p = new Point();

            Random rd = new Random();

            g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;

            for (int i = 0; i < count ; i++)

            {

                p = new Point(rd.Next(w), rd.Next(h));

                g.DrawLine(line, p, new Point(p.X + rd.Next(11), p.Y - 5 + rd.Next(11)));

            }

            line.Dispose();

            g.Dispose();

            return tmpimage;

        }

        /// <summary>

        /// 验证码类型

        /// </summary>

        public enum CodeType

        {

            /// <summary>

            /// 数字 

            /// </summary>

            Numeral = 1,

            /// <summary>

            /// 大写字母

            /// </summary>

            Capital = 2,

            /// <summary>

            /// 小写字母

            /// </summary>

            Lower = 3,

            /// <summary>

            /// 大小写字母

            /// </summary>

            Letter = 4,

            /// <summary>

            /// 数字和大写字母

            /// </summary>

            NumeralAndCapital = 5,

            /// <summary>

            /// 数字和小写字母

            /// </summary>

            NumeralAndLower = 6,

            /// <summary>

            /// 数字和大小写字母

            /// </summary>

            NumeralAndLetter = 7

        }

        /// <summary>

        /// 生成验证码候选的数组

        /// </summary>

        /// <param name="codetype">验证码类型</param>

        /// <returns>验证码候选数组</returns>

        private char[] CodeChar(CodeType codetype)

        {

            char[] tmpchar;

            int n = 0;

            switch (codetype)

            {

                case CodeType.Numeral:

                    tmpchar = new char[10];

                    for (int i = 48; i <= 57; i++)

                    {

                        tmpchar[n] = char.Parse(char.ConvertFromUtf32(i));

                        n++;

                    }

                    break;

                case CodeType.Capital:

                    tmpchar = new char[26];

                    for (int i = 65; i <= 90; i++)

                    {

                        tmpchar[n] = char.Parse(char.ConvertFromUtf32(i));

                        n++;

                    }

                    break;

                case CodeType.Lower:

                    tmpchar = new char[26];

                    for (int i = 97; i <= 122; i++)

                    {

                        tmpchar[n] = char.Parse(char.ConvertFromUtf32(i));

                        n++;

                    }

                    break;

                case CodeType.Letter:

                    tmpchar = new char[52];

                    for (int i = 65; i <= 90; i++)

                    {

                        tmpchar[n] = char.Parse(char.ConvertFromUtf32(i));

                        n++;

                    }

                    for (int i = 97; i <= 122; i++)

                    {

                        tmpchar[n] = char.Parse(char.ConvertFromUtf32(i));

                        n++;

                    }

                    break;

                case CodeType.NumeralAndCapital:

                    tmpchar = new char[36];

                    for (int i = 48; i <= 57; i++)

                    {

                        tmpchar[n] = char.Parse(char.ConvertFromUtf32(i));

                        n++;

                    }

                    for (int i = 65; i <= 90; i++)

                    {

                        tmpchar[n] = char.Parse(char.ConvertFromUtf32(i));

                        n++;

                    }

                    break;

                case CodeType.NumeralAndLower:

                    tmpchar = new char[36];

                    for (int i = 48; i <= 57; i++)

                    {

                        tmpchar[n] = char.Parse(char.ConvertFromUtf32(i));

                        n++;

                    }

                    for (int i = 97; i <= 122; i++)

                    {

                        tmpchar[n] = char.Parse(char.ConvertFromUtf32(i));

                        n++;

                    }

                    break;

                case CodeType.NumeralAndLetter:

                    tmpchar = new char[62];

                    for (int i = 48; i <= 57; i++)

                    {

                        tmpchar[n] = char.Parse(char.ConvertFromUtf32(i));

                        n++;

                    }

                    for (int i = 65; i <= 90; i++)

                    {

                        tmpchar[n] = char.Parse(char.ConvertFromUtf32(i));

                        n++;

                    }

                    for (int i = 97; i <= 122; i++)

                    {

                        tmpchar[n] = char.Parse(char.ConvertFromUtf32(i));

                        n++;

                    }

                    break;

                default:

                    tmpchar = new char[0];

                    break;

            }

            return tmpchar;

        }

    }

}