首页 > 其他 > 详细

【日常笔记】生成验证码图片

时间:2018-07-20 10:54:11      阅读:176      评论:0      收藏:0      [点我收藏+]
        public string MakeValidateCode()
        {
            char[] s = new char[] {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};
            string num = "";
            Random r = new Random();
            for (int i = 0; i < 4; i++)
            {
                num += s[r.Next(0, s.Length)].ToString();
            }
            return num;
        }

生成验证所需的字母和数字组合

        public byte[] CreateCheckCodeImage(string checkCode)
        {
            Color[] colors = { Color.Black, Color.Red, Color.DarkBlue, Color.Green, Color.Brown, Color.DarkCyan, Color.Purple };
            int len = colors.Length;

            System.Drawing.Bitmap image = new System.Drawing.Bitmap((int)Math.Ceiling(checkCode.Length * 14.5d), 25);
            Graphics g = Graphics.FromImage(image);
            try
            {
                //生成随机生成器
                Random random = new Random();
                //清空图片背景色
                g.Clear(Color.White);
                Font font = new System.Drawing.Font("Arial", 12, FontStyle.Bold);
                System.Drawing.Brush brush = new SolidBrush(colors[random.Next(0, len)]);
                SizeF size = g.MeasureString(checkCode, font);
                g.DrawString(checkCode, font, brush, (image.Width - size.Width) / 2, (image.Height - size.Height) / 2);

                Twist tw = new Twist();
                //正弦扭曲 沿X轴
                image = tw.TwistBitmap(image, false, 4, Math.PI);
                //正弦扭曲 沿Y轴
                image = tw.TwistBitmap(image, true, 2, Math.PI / 2);

                g.Dispose();

                g = Graphics.FromImage(image);
                //画图片的背景噪音线
                for (int i = 0; i < 2; 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);
                }
                //画图片的边框线
                g.DrawRectangle(new Pen(Color.Silver), 0, 0, image.Width - 1, image.Height - 1);

                System.IO.MemoryStream ms = new System.IO.MemoryStream();
                image.Save(ms, System.Drawing.Imaging.ImageFormat.Gif);
                return ms.ToArray();
            }
            finally
            {
                g.Dispose();
                image.Dispose();
            }
        }

生成图片,并且转化为字节数据

【日常笔记】生成验证码图片

原文:https://www.cnblogs.com/weimingtian/p/9339706.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!