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

c#生成验证码

时间:2020-01-30 19:10:40      阅读:115      评论:0      收藏:0      [点我收藏+]

标签:rectangle   clear   static   pen   背景色   gradient   hit   bsp   for   

using System.Drawing;
using System.Drawing.Drawing2D;
using System.Drawing.Imaging;
using System.IO;

需要特别引用:System.Drawing

        /// <summary>
        /// 生成随机数
        /// </summary>
        /// <param name="codeLen">数据的长度</param>
        /// <returns></returns>
        public static string CreateRandomCode(int codeLen)
        {
            string allChar = "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[] allCharAry = allChar.Split(,);
            string randomCode = "";
            int temp = -1;
            Random rand = new Random();
            for (int i = 0; i < codeLen; i++)
            {
                if (temp != -1)
                {
                    rand = new Random(i * temp * ((int)DateTime.Now.Ticks));
                }
                int t = rand.Next(35);
                if (temp == t)
                {
                    return CreateRandomCode(codeLen);
                }
                temp = t;
                randomCode += allCharAry[t];
            }
            return randomCode;
        }

 

       /// <summary>
       /// 根据随机数生成图片
       /// </summary>
       /// <param name="validateCode">图片中的文字</param>
       /// <returns></returns>
        public byte[] CreateValidGraphic(string validateCode)
        {
            Bitmap img = new Bitmap((int)Math.Ceiling(validateCode.Length * 16.0), 27);
            Graphics g = Graphics.FromImage(img);
            try
            {
                Random random = new Random();//生成随机数
                g.Clear(Color.White);//清空图片背景色
                for (int i = 0; i < 25; i++)//画图片的干扰线
                {
                    int x1 = random.Next(img.Width);
                    int x2 = random.Next(img.Width);
                    int y1 = random.Next(img.Height);
                    int y2 = random.Next(img.Height);
                    g.DrawLine(new Pen(Color.Silver), x1, x2, y1, y2);
                }
                Font font = new Font("Arial", 13, (FontStyle.Bold | FontStyle.Italic));
                LinearGradientBrush brush = new LinearGradientBrush(new Rectangle(0, 0, img.Width, img.Height), Color.Blue, Color.DarkRed, 1.2f, true);
                g.DrawString(validateCode, font, brush, 3, 2);
                for (int i = 0; i < 100; i++)//画图片的前景干扰点
                {
                    int x = random.Next(img.Width);
                    int y = random.Next(img.Height);
                    img.SetPixel(x, y, Color.FromArgb(random.Next()));
                }
                g.DrawRectangle(new Pen(Color.Silver), 0, 0, img.Width - 1, img.Height - 1);//画图片的边框线
                MemoryStream stream = new MemoryStream();
                img.Save(stream, ImageFormat.Jpeg);
                return stream.ToArray();//输入图片
            }
            catch (Exception)
            {
                throw;
            }
        }

 

c#生成验证码

标签:rectangle   clear   static   pen   背景色   gradient   hit   bsp   for   

原文地址:https://www.cnblogs.com/vichin/p/12243345.html

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