生成验证码(比较简单的那种)

 

   [Serializable]
    public class VerificationHelper
    {
        private static Font[] fonts = {
        new Font(new FontFamily("Times New Roman"),16,FontStyle.Regular),
        new Font(new FontFamily("Georgia"), 16,FontStyle.Regular),
        new Font(new FontFamily("Arial"), 16,FontStyle.Regular),
        new Font(new FontFamily("Comic Sans MS"), 16,FontStyle.Regular)
        };
        private static string value = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
        private int width { get; set; }
        private int height { get; set; }

        public string Id { get; set; }
        public string Text { get; set; }

        public VerificationHelper(int width, int height)
        {
            this.width = width;
            this.height = height;
            this.Id = Guid.NewGuid().ToString("N");
            this.Text = CreateText();
        }

        /// <summary>
        /// 随机生成4位验证码
        /// </summary>
        /// <returns></returns>
        public string CreateText()
        {
            string randomResult = string.Empty;
            char[] rtn = new char[4];
            Guid gid = Guid.NewGuid();
            var ba = gid.ToByteArray();
            for (var i = 0; i < 4; i++)
            {
                rtn[i] = value[((ba[i] + ba[4 + i]) % 35)];
            }

            foreach (char r in rtn)
            {
                randomResult += r;
            }
            return randomResult;
        }
        public byte[] CreateVerification()
        {
            Bitmap image = new Bitmap(width, height);
            Graphics g = Graphics.FromImage(image);
            try
            {
                //生成随机生成器
                Random random = new Random();
                //清空图片背景色
                g.Clear(Color.White);
                //画图片的干扰线
                for (int i = 0; i < 25; 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.Silver), x1, y1, x2, y2);
                }
                LinearGradientBrush brush = new LinearGradientBrush(new Rectangle(0, 0, image.Width, image.Height),
                 Color.Blue, Color.DarkRed, 1.2f, true);
                //居中(上下左右)
                StringFormat format = new StringFormat();
                format.LineAlignment = StringAlignment.Center;
                format.Alignment = StringAlignment.Center;
                //排版框
                Rectangle rec = new Rectangle(0, 0, width, height);

                g.DrawString(Text, fonts[random.Next(4)], brush, rec, format);
                //画图片的前景干扰点
                for (int i = 0; i < 100; 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);
                //保存图片数据
                MemoryStream stream = new MemoryStream();
                image.Save(stream, ImageFormat.Jpeg);
                //输出图片流
                return stream.ToArray();
            }
            finally
            {
                g.Dispose();
                image.Dispose();
            }
        }
    }

 

posted on 2017-12-02 19:14  奔游浪子  阅读(123)  评论(0)    收藏  举报

导航