public Captcha CreateCaptcha(CaptchaOptions options)
        {
            if (options.ImgWidth < 0)
            {
                throw new ArgumentException("图片绘制宽度不能小于0");
            }
            if (options.ImgHeight < 0)
            {
                throw new ArgumentException("图片绘制高度不能小于0");
            }
            if (options.MinCharsLength < 1 || options.MinCharsLength > 10)
            {
                throw new ArgumentException("随机最少字符长度范围为[1~10]之间");
            }
            if (options.MaxCharsLength < 1 || options.MaxCharsLength > 10)
            {
                throw new ArgumentException("随机最多字符长度范围为[1~10]之间");
            }
            if (options.MaxCharsLength < options.MinCharsLength)
            {
                throw new ArgumentException("随机最多字符长度不能少于最少字符长度");
            }

            var length  = _randomProvider.ToNumber(options.MinCharsLength, options.MaxCharsLength);
            var code    = _randomProvider.ToChars(length);
            var captcha = _graphicsStrategy.Drawing(code, options.ImgWidth, options.ImgHeight, length, options.FontColor, options.BackgroundColor);

            //存储验证码
            _captchaStore.Add(captcha);
            return(captcha);
        }
Example #2
0
        public Captcha Drawing(string code, int width, int height, int length, string fontColor, string backgroundColor)
        {
            var chars = _randomProvider.ToChars(length);

            using (Bitmap image = new Bitmap(width, height))
            {
                using (Graphics g = Graphics.FromImage(image))
                {
                    g.Clear(ColorTranslator.FromHtml(backgroundColor));
                    g.SmoothingMode      = SmoothingMode.AntiAlias;
                    g.SmoothingMode      = SmoothingMode.HighQuality;
                    g.CompositingQuality = CompositingQuality.HighQuality;
                    g.InterpolationMode  = InterpolationMode.High;
                    g.TextRenderingHint  = TextRenderingHint.AntiAlias;
                    g.PixelOffsetMode    = PixelOffsetMode.HighSpeed;
                    var fontFamily = new FontFamily("Arial");
                    var pen        = new Pen(ColorTranslator.FromHtml(fontColor));

                    var    ch = new Chars();
                    string c;
                    int    toLeft = 10, toTop = 15;
                    int    perLineCount = length / 2;
                    for (int i = 0; i < length; i++)
                    {
                        c = chars[i].ToString();
                        var rectangle = ch[chars[i]];
                        using (GraphicsPath path = new GraphicsPath(FillMode.Alternate))
                        {
                            path.AddString(c, fontFamily, (int)FontStyle.Regular, 50, new Point(toLeft + rectangle.X, toTop + rectangle.Y), StringFormat.GenericTypographic);
                            g.DrawPath(pen, path);
                        }

                        //if (maxHeight < rectangle.Height)
                        //    maxHeight = rectangle.Height;
                        //if (i == perLineCount)
                        //{
                        //    toTop = maxHeight + 2;
                        //    toLeft = 10;
                        //}
                        toLeft += rectangle.Width;
                    }


                    AddWaveEffectGraphics(width, height, image);
                    using (var stream = new MemoryStream())
                    {
                        image.Save(stream, ImageFormat.Png);
                        return(new Captcha(chars, stream.ToArray(), "image/png"));
                    }
                }
            }
        }