コード例 #1
0
        /// <summary>
        /// Gets the dictionary captcha text.
        /// </summary>
        /// <param name="extended">The extended.</param>
        /// <returns>dictionary captcha text.</returns>
        protected virtual String GetDictionaryCaptchaText(Boolean extended = false)
        {
            if (WordsFile == null || WordsFile.Length == 0)
            {
                return(null);
            }

            var filePath = SimpleCaptcha.MapPath(WordsFile);

            if (!File.Exists(filePath))
            {
                return(null);
            }

            try
            {
                var file     = new FileInfo(filePath);
                var fileSize = file.Length;
                using (var fs = File.OpenRead(filePath))
                    using (var sr = new StreamReader(fs))
                    {
                        var firstLine  = sr.ReadLine();
                        var lineLength = firstLine.Length;
                        var lineCount  = 1;
                        unchecked
                        {
                            lineCount = (Int32)(fileSize / firstLine.Length);
                        }

                        var lineNum = _random.Next(0, lineCount);
                        lineLength += Environment.NewLine.Length;
                        fs.Seek(lineLength * lineNum, SeekOrigin.Begin);
                        sr.DiscardBufferedData();
                        var text = sr.ReadLine().Trim();
                        if (extended)
                        {
                            var textChars = text.ToCharArray();
                            var vocals    = new List <Char>()
                            {
                                'a', 'e', 'i', 'o', 'u'
                            };
                            for (int i = 0; i < textChars.Length; i++)
                            {
                                if (vocals.Contains(text[i]) && _random.Next(0, 1) == 1)
                                {
                                    textChars[i] = vocals[_random.Next(0, 4)];
                                }
                            }

                            text = new String(textChars);
                        }

                        return(text);
                    }
            }
            catch
            {
                return(null);
            }
        }
コード例 #2
0
 protected override void OnPreInit(EventArgs e)
 {
     Response.Clear();
     var captcha = new SimpleCaptcha();
     captcha.SessionName = CaptchaSessionName;
     captcha.WordsFile = "~/App_Data/words-en.txt";
     using (var image = captcha.CreateImage())
     {
         Response.ContentType = "image/png";
         image.Save(Response.OutputStream, ImageFormat.Png);
         Response.End();
     }
 }
コード例 #3
0
ファイル: Program.cs プロジェクト: 88037199/CoolCaptcha.NET
 public static void GenerateAll()
 {
     var foldeName = String.Format("{0:yyyyMMddHHmmss}", DateTime.Now);
     var captcha = new SimpleCaptcha();
     captcha.MinWordLength = 6;
     captcha.MaxWordLength = 6;
     captcha.Scale = 6;
     for (int i = 0; i < 8; i++)
     {
         for (int j = 0; j < 10; j++)
         {
             captcha.FontConfig = SimpleCaptcha.Fonts[i];
             using (var image = captcha.CreateImage())
             {
                 var path = String.Format("{0}{1}\\{2}-{3}.png", AppDomain.CurrentDomain.BaseDirectory, foldeName, i, j);
                 Directory.CreateDirectory(Path.GetDirectoryName(path));
                 image.Save(path, ImageFormat.Png);
             }
         }
     }
 }