public void ProcessRequest(HttpContext context) { int width = 200; int height = 30; try { width = Convert.ToInt32(context.Request.QueryString["w"]); height = Convert.ToInt32(context.Request.QueryString["h"]); } catch (Exception) { // Nothing } // 从 Session 中读取验证码,并创建图片 CaptchaImage.CaptchaImage ci = new CaptchaImage.CaptchaImage(context.Session["CaptchaImageText"].ToString(), width, height, "Consolas"); // 输出图片 context.Response.Clear(); context.Response.ContentType = "image/jpeg"; ci.Image.Save(context.Response.OutputStream, ImageFormat.Jpeg); ci.Dispose(); }
// 返回验证图片 public ActionResult CaptchaImage(int w = 200, int h = 300) { byte[] imageBytes; // 从 Session 中读取验证码,并创建图片 using (CaptchaImage.CaptchaImage ci = new CaptchaImage.CaptchaImage(Session["CaptchaImageText"].ToString(), w, h, "Consolas")) { using (MemoryStream ms = new MemoryStream()) { ci.Image.Save(ms, ImageFormat.Jpeg); imageBytes = ms.ToArray(); } } return(File(imageBytes, "image/jpeg")); }
public void ValidateCode() { int width = 100; int height = 26; try { width = Convert.ToInt32(Request.QueryString["w"]); height = Convert.ToInt32(Request.QueryString["h"]); } catch (Exception) { // Nothing } try { string checkCode = GenerateRandomCode(); Session["HHCaptchaImageText"] = checkCode; // 从 Session 中读取验证码,并创建图片 // HH.Common.CaptchaImage ci = new HH.Common.CaptchaImage(checkCode, width, height, "Consolas"); CaptchaImage.CaptchaImage ci = new CaptchaImage.CaptchaImage(checkCode, width, height, "Consolas"); // 输出图片 Response.Clear(); Response.ContentType = "image/jpeg"; ci.Image.Save(Response.OutputStream, ImageFormat.Jpeg); ci.Dispose(); Response.End(); } catch (Exception ee) { string ss = ee.Message; Response.ContentType = "text/plain"; Response.Write("请求错误"); return; } }