Carrotware.Web.UI.Controls.CaptchaImage.GetCaptchaImage C# (CSharp) Method

GetCaptchaImage() public static method

public static GetCaptchaImage ( Color fg, Color bg, Color n ) : Bitmap
fg Color
bg Color
n Color
return System.Drawing.Bitmap
        public static Bitmap GetCaptchaImage(Color fg, Color bg, Color n)
        {
            int topPadding = 2; // top and bottom padding in pixels
            int sidePadding = 3; // side padding in pixels

            SolidBrush textBrush = new SolidBrush(fg);
            Font font = new Font(FontFamily.GenericSansSerif, 32, FontStyle.Bold);

            string guid = GetKey();

            Bitmap bmpCaptcha = new Bitmap(500, 500);
            Graphics graphics = Graphics.FromImage(bmpCaptcha);
            SizeF textSize = graphics.MeasureString(guid, font);

            bmpCaptcha.Dispose();
            graphics.Dispose();

            int bitmapWidth = sidePadding * 2 + (int)textSize.Width;
            int bitmapHeight = topPadding * 2 + (int)textSize.Height;
            bmpCaptcha = new Bitmap(bitmapWidth, bitmapHeight);
            graphics = Graphics.FromImage(bmpCaptcha);

            Rectangle rect = new Rectangle(0, 0, bmpCaptcha.Width, bmpCaptcha.Height);

            HatchBrush hatch1 = new HatchBrush(HatchStyle.SmallGrid, n, bg);

            HatchBrush hatch2 = new HatchBrush(HatchStyle.DiagonalCross, bg, Color.Transparent);

            graphics.FillRectangle(hatch1, rect);
            graphics.DrawString(guid, font, textBrush, sidePadding, topPadding);
            graphics.FillRectangle(hatch2, rect);

            HttpContext.Current.Response.ContentType = "image/x-png";

            using (MemoryStream memStream = new MemoryStream()) {
                bmpCaptcha.Save(memStream, ImageFormat.Png);
            }

            textBrush.Dispose();
            font.Dispose();
            hatch1.Dispose();
            hatch2.Dispose();
            graphics.Dispose();

            return bmpCaptcha;
        }

Usage Example

Example #1
0
        private void DoCaptcha(HttpContext context)
        {
            Color f = ColorTranslator.FromHtml(CaptchaImage.FGColorDef);
            Color b = ColorTranslator.FromHtml(CaptchaImage.BGColorDef);
            Color n = ColorTranslator.FromHtml(CaptchaImage.NColorDef);

            Bitmap captchaImg = CaptchaImage.GetCaptchaImage(f, b, n);

            if (captchaImg == null)
            {
                context.Response.StatusCode        = 404;
                context.Response.StatusDescription = "Not Found";
                context.ApplicationInstance.CompleteRequest();
                return;
            }

            context.Response.ContentType = "image/x-png";

            using (MemoryStream memStream = new MemoryStream()) {
                captchaImg.Save(memStream, ImageFormat.Png);
                memStream.WriteTo(context.Response.OutputStream);
            }
            context.Response.StatusCode        = 200;
            context.Response.StatusDescription = "OK";
            context.ApplicationInstance.CompleteRequest();

            captchaImg.Dispose();
            context.Response.End();
        }