// Make a captcha image for the text. public Bitmap MakeCaptchaImge(string txt, int min_size, int max_size, int wid, int hgt, CaptchaStyle cStyle = CaptchaStyle.TextWithLineAndCircles) { // Make the bitmap and associated Graphics object. Bitmap bm = new Bitmap(wid, hgt); using (Graphics gr = Graphics.FromImage(bm)) { gr.SmoothingMode = SmoothingMode.HighQuality; gr.Clear(Color.LightYellow); var r = new Random(); if (cStyle == CaptchaStyle.TextWithLineAndCircles) { for (int i = 0; i < 10; i++) { var s = r.Next(10, 15); gr.DrawEllipse(new Pen(Color.SandyBrown, 2), r.Next(5, wid), r.Next(5, hgt), s, s); } } // See how much room is available for each character. int ch_wid = (int)(wid / txt.Length); // Draw each character. for (int i = 0; i < txt.Length; i++) { float font_size = Rand.Next(min_size, max_size); using (Font the_font = new Font("Times New Roman", font_size, FontStyle.Bold)) { DrawCharacter(txt.Substring(i, 1), gr, the_font, i * ch_wid, ch_wid, wid, hgt); } } if (cStyle == CaptchaStyle.TextWithLineAndCircles || cStyle == CaptchaStyle.TextWithLine) { var points = new List <PointF>(); for (int i = 0; i < 7; i++) { var x = r.Next(0, wid); var y = r.Next(0, hgt); points.Add(new PointF(x, y)); } gr.DrawCurve(new Pen(Color.LightGray, 3), points.ToArray()); } } return(bm); }
// Methods internal static bool ParseDistorsionParam(NameValueCollection parameters, out CaptchaStyle distStyle) { if ((null != parameters) && !string.IsNullOrEmpty(parameters[CaptchaImageTransformation.DISTSTYLE_KEY])) { distStyle = (CaptchaStyle)Enum.Parse(typeof(CaptchaStyle), parameters[CaptchaImageTransformation.DISTSTYLE_KEY]); if (CaptchaStyle.Random == distStyle) { distStyle = (CaptchaStyle)new Random(DateTime.Now.Millisecond).Next(3); } return true; } else { distStyle = CaptchaStyle.Confetti; return false; } }
// Methods internal static string ToQueryString( int width, int height, int clientCacheDuration, int serverCacheDuration, RotateFlipType rotateFlip, bool drawGrayscale, bool drawSepia, DynamicText text, DynamicImageFormat imageFormat, KeyValuePair<Type, IDictionary<string, string>>? imageCreator, Dictionary<Type, IDictionary<string, string>> imageTransformations, TextContainerSizeType sizeType, CaptchaStyle distortionStyle, ReadnessLevel readnessLevel, Color backColor) { string dynamicImageQueryString = DynamicImageProvider.ToQueryString( null, width, height, clientCacheDuration, serverCacheDuration, rotateFlip, drawGrayscale, drawSepia, text, imageFormat, imageCreator, imageTransformations); List<string> parameters = new List<string>(); string parameterFormat = "{0}={1}"; // size type if (TextContainerSizeType.Specified != sizeType) { parameters.Add(string.Format(parameterFormat, TextDependentImageCreator.TEXTCONTSIZETYPE_KEY, sizeType)); } // distortion style if (CaptchaStyle.Confetti != distortionStyle) { parameters.Add(string.Format(parameterFormat, CaptchaImageTransformation.DISTSTYLE_KEY, distortionStyle)); } // readness level if (ReadnessLevel.Normal != readnessLevel) { parameters.Add(string.Format(parameterFormat, CaptchaImageTransformation.READLEVEL_KEY, readnessLevel)); } // back color if (Color.White != backColor) { parameters.Add(string.Format(parameterFormat, CaptchaImageTransformation.CAPTCHABACKCOLOR_KEY, backColor.ToArgb())); } return (0 < parameters.Count) ? string.Concat(dynamicImageQueryString, "&", string.Join("&", parameters.ToArray())) : dynamicImageQueryString; }