Example #1
0
 /// <summary>
 /// 构造函数
 /// </summary>
 public VerificationCodeHelper(VerificationCodeRule rule)
 {
     HttpContext.Current.Response.Expires         = 0;
     HttpContext.Current.Response.Buffer          = true;
     HttpContext.Current.Response.ExpiresAbsolute = DateTime.Now.AddSeconds(-1);
     HttpContext.Current.Response.AddHeader("pragma", "no-cache");
     HttpContext.Current.Response.CacheControl = "no-cache";
     CreateImage(rule);
 }
Example #2
0
        /// <summary>
        /// 绘制验证码
        /// </summary>
        public void CreateImage(VerificationCodeRule rule)
        {
            _text = RandomHelper.Number(rule.LetterCount);
            int      intImageWidth = _text.Length * rule.LetterWidth;
            Bitmap   dstImage      = new Bitmap(intImageWidth, rule.LetterHeight);
            Graphics g             = Graphics.FromImage(dstImage);

            g.Clear(Color.White);
            for (int i = 0; i < 2; i++)
            {
                int x1 = Next(dstImage.Width - 1);
                int x2 = Next(dstImage.Width - 1);
                int y1 = Next(dstImage.Height - 1);
                int y2 = Next(dstImage.Height - 1);
                g.DrawLine(new Pen(Color.Silver), x1, y1, x2, y2);
            }

            int px = -12;

            for (int intIndex = 0; intIndex < this._text.Length; intIndex++)
            {
                px += Next(12, 16);
                var    py      = Next(-2, 2);
                string strChar = this._text.Substring(intIndex, 1);
                strChar = Next(1) == 1 ? strChar.ToLower() : strChar.ToUpper();
                Brush newBrush = new SolidBrush(GetRandomColor());
                Point thePos   = new Point(px, py);
                g.DrawString(strChar, _fonts[Next(_fonts.Length - 1)], newBrush, thePos);
            }

            for (int i = 0; i < 10; i++)
            {
                int x = Next(dstImage.Width - 1);
                int y = Next(dstImage.Height - 1);
                dstImage.SetPixel(x, y, Color.FromArgb(Next(0, 255), Next(0, 255), Next(0, 255)));
            }

            dstImage = TwistImage(dstImage, true, Next(1, 3), Next(4, 6));
            g.DrawRectangle(new Pen(Color.LightGray, 1), 0, 0, intImageWidth - 1, rule.LetterHeight - 1);
            _image = dstImage;
        }