public void ProcessRequest(HttpContext context)
        {
            context.Response.ContentType = "image/jpg";
            context.Response.Cache.SetCacheability(HttpCacheability.NoCache);
            context.Response.BufferOutput = false;
            string strCaptcha = context.Request.QueryString["c"].ToString();

            /*using (var stream = new MemoryStream())
            {
                using (var bmp = new Bitmap(IMAGE_WIDTH, IMAGE_HEIGHT))
                {
                    using (Graphics g = Graphics.FromImage(bmp))
                    {
                        using (var font = new Font(FONT_FAMILY, 1f))
                        {
                            g.Clear(_background);
                            SizeF finalSize;
                            SizeF testSize = g.MeasureString(strCaptcha, font);
                            float bestFontSize = Math.Min(IMAGE_WIDTH / testSize.Width, IMAGE_HEIGHT / testSize.Height)
                                                 * 0.95f;
                            using (var finalFont = new Font(FONT_FAMILY, bestFontSize))
                            {
                                finalSize = g.MeasureString(strCaptcha, finalFont);
                            }
                            g.PageUnit = GraphicsUnit.Point;
                            var textTopLeft = new PointF(
                                (IMAGE_WIDTH - finalSize.Width) / 2, (IMAGE_HEIGHT - finalSize.Height) / 2);
                            using (var path = new GraphicsPath())
                            {
                                path.AddString(
                                    strCaptcha,
                                    new FontFamily(FONT_FAMILY),
                                    0,
                                    bestFontSize,
                                    textTopLeft,
                                    StringFormat.GenericDefault);
                                g.SmoothingMode = SmoothingMode.HighQuality;
                                g.FillPath(_foreground, path);
                                g.Flush();
                                bmp.Save(stream, ImageFormat.Png);
                                stream.Seek(0, SeekOrigin.Begin);
                            }
                        }
                    }
                }
                }*/
            // Generate image
            CaptchaImage ci = new CaptchaImage(strCaptcha, Color.White, 120, 50);

            // Return
            ci.Image.Save(context.Response.OutputStream, ImageFormat.Jpeg);
            // Dispose
            ci.Dispose();
        }
Exemple #2
0
        public void ProcessRequest(HttpContext context)
        {
            context.Response.ContentType = "image/jpeg";
            context.Response.Cache.SetCacheability(HttpCacheability.NoCache);
            context.Response.BufferOutput = false;

            // Get text
            string s = "No Text";
            if (context.Request.QueryString["c"] != null &&
                context.Request.QueryString["c"] != "")
            {
                string enc = context.Request.QueryString["c"].ToString();

                // space was replaced with + to prevent error
                enc = enc.Replace(" ", "+");
                try
                {
                    s = Encryptor.Decrypt(enc, "srgerg$%^bg", Convert.FromBase64String("srfjuoxp"));
                }
                catch { }
            }
            // Get dimensions
            int w = 120;
            int h = 50;
            // Width
            if (context.Request.QueryString["w"] != null &&
                context.Request.QueryString["w"] != "")
            {
                try
                {
                    w = Convert.ToInt32(context.Request.QueryString["w"]);
                }
                catch { }
            }
            // Height
            if (context.Request.QueryString["h"] != null &&
                context.Request.QueryString["h"] != "")
            {
                try
                {
                    h = Convert.ToInt32(context.Request.QueryString["h"]);
                }
                catch { }
            }
            // Color
            Color Bc = Color.White;
            if (context.Request.QueryString["bc"] != null &&
                context.Request.QueryString["bc"] != "")
            {
                try
                {
                    string bc = context.Request.QueryString["bc"].ToString().Insert(0, "#");
                    Bc = ColorTranslator.FromHtml(bc);
                }
                catch { }
            }
            // Generate image
            CaptchaImage ci = new CaptchaImage(s, Bc, w, h);

            // Return
            ci.Image.Save(context.Response.OutputStream, ImageFormat.Jpeg);
            // Dispose
            ci.Dispose();
        }