Esempio n. 1
0
        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);
        }