public ActionResult GetCaptchaImage(string fgcolor, string bgcolor, string ncolor)
        {
            var context = System.Web.HttpContext.Current;

            context.Response.Cache.VaryByParams["fgcolor"] = true;
            context.Response.Cache.VaryByParams["bgcolor"] = true;
            context.Response.Cache.VaryByParams["ncolor"]  = true;

            DoCacheMagic(context, 3);

            Color f = ColorTranslator.FromHtml(CaptchaImage.FGColorDef);
            Color b = ColorTranslator.FromHtml(CaptchaImage.BGColorDef);
            Color n = ColorTranslator.FromHtml(CaptchaImage.NColorDef);

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

            if (bmpCaptcha == null)
            {
                Response.StatusCode        = 404;
                Response.StatusDescription = "Not Found";
                byte[] bb = new byte[0];

                return(File(bb, "image/png"));
            }

            bmpCaptcha.Save(stream, ImageFormat.Png);
            bmpCaptcha.Dispose();

            return(File(stream.ToArray(), "image/png"));
        }
Ejemplo n.º 2
0
        public ActionResult GetCaptchaImage(string fgcolor, string bgcolor, string ncolor)
        {
            Color f = ColorTranslator.FromHtml(CaptchaImage.FGColorDef);
            Color b = ColorTranslator.FromHtml(CaptchaImage.BGColorDef);
            Color n = ColorTranslator.FromHtml(CaptchaImage.NColorDef);

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

            if (bmpCaptcha == null)
            {
                Response.StatusCode        = 404;
                Response.StatusDescription = "Not Found";
                byte[] bb = new byte[0];

                return(File(bb, "image/png"));
            }

            using (MemoryStream imgStream = new MemoryStream()) {
                bmpCaptcha.Save(imgStream, ImageFormat.Png);
                bmpCaptcha.Dispose();

                Response.StatusCode        = 200;
                Response.StatusDescription = "OK";

                return(File(imgStream.ToArray(), "image/png"));
            }
        }