/// <summary> /// Enables processing of HTTP Web requests by a custom HttpHandler that implements the <see cref="T:System.Web.IHttpHandler"/> interface. /// </summary> /// <param name="filterContext">An <see cref="T:System.Web.HttpContext"/> object that provides references to the intrinsic server objects (for example, Request, Response, Session, and Server) used to service HTTP requests.</param> public void ProcessRequest(HttpContext context) { // get the unique GUID of the captcha; this must be passed in via the querystring string guid = context.Request.QueryString["guid"]; CaptchaImage ci = CaptchaImage.GetCachedCaptcha(guid); if (String.IsNullOrEmpty(guid) || ci == null) { context.Response.StatusCode = 404; context.Response.StatusDescription = "Not Found"; context.Response.End(); return; } // write the image to the HTTP output stream as an array of bytes using (Bitmap b = ci.RenderImage()) { b.Save(context.Response.OutputStream, ImageFormat.Gif); } context.Response.ContentType = "image/gif"; context.Response.StatusCode = 200; context.Response.StatusDescription = "OK"; context.Response.End(); }
protected void Page_Load(object sender, EventArgs e) { //captcha = new CaptchaImage(); this.captcha.TextLength = 5; this.captcha.LineNoise = CaptchaImage.LineNoiseLevel.Low; Bitmap objBMP = captcha.RenderImage(); //This is to add the string to session cookie, to be compared later Session.Add("randomStr", captcha.Text); HttpCookie aCookie = new HttpCookie("randomStr"); //aCookie.Domain = ".dailyinfo.vn"; aCookie.Values["value"] = captcha.Text; Response.Cookies.Add(aCookie); //' Set the content type and return the image Response.ContentType = "image/GIF"; objBMP.Save(Response.OutputStream, ImageFormat.Gif); objBMP.Dispose(); }
public static void Main() { for (int i = 0; i < 1000; ++i) { var img = new CaptchaImage(); img.RenderImage().Save(".\\images\\" + img.Text + ".bmp"); } }
/// <summary> /// 验证码 GET: /User/ValidCode/1234 /// </summary> /// <param name="aid"></param> /// <returns></returns> public ActionResult ValidCode(string aid) { CaptchaImage ci = CaptchaImage.CreateCaptcha(aid); using (Bitmap b = ci.RenderImage()) { b.Save(Response.OutputStream, ImageFormat.Jpeg); } Response.ContentType = "image/jpeg"; Response.StatusCode = 200; Response.StatusDescription = "OK"; return(null); }
public FileResult Image(string id) { CaptchaImage captchaImage = CaptchaImageCache.GetCachedCaptcha(id); using (MemoryStream stream = new MemoryStream()) { if (captchaImage != null) { using (Bitmap bitmap = captchaImage.RenderImage()) { bitmap.Save(stream, ImageFormat.Png); } } else { LogContext.LogInformation("Iamge is not found in cache (CaptchaController method Image)", "CaptchaController", "captchaImage.Id = " + id); } return(File(stream.ToArray(), "image/png")); } }
public override void ExecuteResult(ControllerContext context) { var ci = new CaptchaImage { Height = Convert.ToInt32(context.HttpContext.Request.QueryString["height"]), Width = Convert.ToInt32(context.HttpContext.Request.QueryString["width"]) }; var guid = ci.Text; if (context.HttpContext.Session != null) { context.HttpContext.Session["RandomCode"] = guid; } using (var b = ci.RenderImage()) { b.Save(context.HttpContext.Response.OutputStream, ImageFormat.Gif); } context.HttpContext.Response.ContentType = "image/png"; context.HttpContext.Response.StatusCode = 200; context.HttpContext.Response.StatusDescription = "OK"; context.HttpContext.ApplicationInstance.CompleteRequest(); }