码迷,mamicode.com
首页 > Windows程序 > 详细

winfrom 底层类 验证码

时间:2014-12-17 12:50:36      阅读:260      评论:0      收藏:0      [点我收藏+]

标签:c#   底层类   验证码   

效果图:

bubuko.com,布布扣

底层类:

        /// <summary>
        /// 生成验证码
        /// </summary>
        /// <param name="len">验证码长度</param>
        /// <returns></returns>
        private static string CreateRandomCode(int len) {
            System.Random rand = new Random();
            string randomCode = "";//随机码
            char[] Allchar = { ‘0‘, ‘1‘, ‘2‘, ‘3‘, ‘4‘, ‘5‘, ‘6‘, ‘7‘, ‘8‘, ‘9‘, ‘A‘, ‘B‘, ‘C‘, ‘D‘, ‘E‘, ‘F‘, ‘G‘, ‘H‘, ‘J‘, ‘K‘, ‘L‘, ‘M‘, ‘N‘, ‘P‘, ‘T‘, ‘Q‘, ‘W‘, ‘X‘, ‘Y‘, ‘a‘, ‘b‘, ‘c‘, ‘d‘, ‘e‘, ‘f‘, ‘g‘, ‘h‘, ‘j‘, ‘k‘, ‘m‘, ‘n‘, ‘p‘, ‘q‘, ‘t‘, ‘w‘, ‘x‘, ‘y‘, ‘z‘ };
            for (int i = 0; i < len; i++) {
                //生成一个小于Allchar.Length的随机数
                int a = rand.Next(Allchar.Length);
                randomCode += Allchar[a].ToString();
            }
            //验证码
            return randomCode;
        }
        /// <summary>
        /// 生成验证码,填充到PictureBox中
        /// </summary>
        /// <param name="len">验证码长度</param>
        /// <param name="pic">PictureBox名称</param>
        /// <param name="revolve">旋转度数</param>
        /// <param name="picHeight">图片高度</param>
        /// <param name="picWidth">图片宽度</param>
        /// <param name="charDistance">验证码中各个字符间距</param>
        /// <param name="fontSize">验证码字符的大小</param>
        public static string CreatePic(int len, PictureBox pic, int revolve, int picHeight, int picWidth, int charDistance, float fontSizes) {          
                //获取验证码
                string strCode = CreateRandomCode(len);
                //创建背景图片
                Bitmap map = new Bitmap(picWidth, picHeight);

                Graphics grap = Graphics.FromImage(map);
                //清除画面,填充背景色为AliceBlue
                grap.Clear(Color.AliceBlue);
                //画一个矩形边框
                grap.DrawRectangle(new Pen(Color.Black, 0), 0, 0, map.Width - 1, map.Height - 1);

                //模式
                grap.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;

                //画噪点
                System.Random rand = new Random();
                for (int i = 0; i < 50; i++) {
                    int x = rand.Next(0, map.Width - 1);
                    int y = rand.Next(0, map.Height - 1);
                    grap.DrawRectangle(new Pen(Color.Gray, 0), x, y, 1, 1);
                }


                //把字符串拆成单个字符
                char[] chars = strCode.ToCharArray();

                //文字居中
                StringFormat format = new StringFormat(StringFormatFlags.NoClip);
                format.Alignment = StringAlignment.Center;
                format.LineAlignment = StringAlignment.Center;

                //定义颜色
                Color[] colors = { Color.Red, Color.Black, Color.Blue, Color.Green, Color.Orange, Color.DarkCyan, Color.Purple };

                //定义字体
                string[] fonts = { "Arial", "Verdana", "Georgia", "Cambria Math", "华文中宋" };

                for (int j = 0; j < len; j++) {
                    //颜色,字体的随机数,随机数作为下标
                    int cindex = rand.Next(colors.Length);
                    int findex = rand.Next(fonts.Length);
                    //字符颜色
                    Brush b = new System.Drawing.SolidBrush(colors[cindex]);
                    //字体样式、大小
                    System.Drawing.Font f = new System.Drawing.Font(fonts[findex], fontSizes, FontStyle.Bold);
                    //定义一个点
                    System.Drawing.Point p = new System.Drawing.Point(16, 20);

                    //转动的角度数
                    float angle = rand.Next(-revolve, revolve);
                    //移动光标到指定位置
                    grap.TranslateTransform(p.X, p.Y);
                    //转动angle度
                    grap.RotateTransform(angle);
                    //赋值
                    grap.DrawString(chars[j].ToString(), f, b, 1, 1, format);

                    //转动回去
                    grap.RotateTransform(-angle);
                    //移动光标到指定位置
                    grap.TranslateTransform(charDistance, -p.Y);
                }

                pic.Image = map;
                pic.SizeMode = PictureBoxSizeMode.StretchImage;      
         
                return strCode;
        }

前台调用:

说明:

(1)窗体要有一个PictureBox控件

(2)DataAccess 是底层类名称

                DataAccess.CreatePic(4, pic, 50, 40, 100, 3, 20);

winfrom 底层类 验证码

标签:c#   底层类   验证码   

原文地址:http://blog.csdn.net/u013816709/article/details/41979291

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!