万事屋软件科技

创建验证码图片

        /// <summary>
        /// 创建验证码图片
        /// </summary>
        /// <param name="verificationText">验证码字符串</param>
        /// <param name="width">图片宽度</param>
        /// <param name="height">图片长度</param>
        /// <returns>图片</returns>

       public static Bitmap CreateVerificationImage(string verificationText, int width, int height)
        {
            Pen _pen= new Pen(Color.Black);
            Font _font = new Font("Arial", 14, FontStyle.Bold);
            Brush _brush = null;
            Bitmap _bitmap = new Bitmap(width,height);
            Graphics _g = Graphics.FromImage(_bitmap);
            SizeF _totalSizeF = _g.MeasureString(verificationText,_font);
            SizeF _curCharSizeF;
            PointF _startPointF = new PointF((width-_totalSizeF.Width)/2,(height-_totalSizeF.Height)/2);
            //随机数产生器
            Random _random =new Random();
            _g.Clear(Color.White);
            for(int i=0;i<verificationText.Length;i++)
            {
                _brush = new LinearGradientBrush(new Point(0,0),new Point(1,1),Color.FromArgb(_random.Next(255),_random.Next(255),_random.Next(255)),Color.FromArgb(_random.Next(255),_random.Next(255),_random.Next(255)));
                _g.DrawString(verificationText[i].ToString(),_font,_brush,_startPointF);
                _curCharSizeF = _g.MeasureString(verificationText[i].ToString(),_font);
                _startPointF.X+= _curCharSizeF.Width;
            }
            _g.Dispose();
            return _bitmap;
        }

比较常用的创建验证码图片方法

万事屋软件~

上一篇:密码强度规则
下一篇:关键词