动态加载验证码
1.创建一般应用程序ashx
<%@ WebHandler Language="C#" Class="YZM" %>
using System;
using System.Web;
public class YZM : IHttpHandler,System .Web.SessionState .IRequiresSessionState{
public void ProcessRequest (HttpContext context) {
//动态生成图片
context.Response.ContentType = "image/JPEG";
using (System.Drawing.Bitmap bitmap = new System.Drawing.Bitmap(100, 30))
{
using (System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(bitmap))
{
/*
g.DrawString("验证码", new System.Drawing.Font("宋体", 10), System.Drawing.Brushes .Red , new System.Drawing.PointF(0, 0));
bitmap .Save (context .Response .OutputStream ,System .Drawing.Imaging.ImageFormat .Jpeg) ;
*/
//动态产生随机数
Random rand = new Random();
int code = rand.Next();
string strCode = code.ToString();
HttpContext.Current.Session["Code"] = strCode;
g.DrawString(strCode, new System.Drawing.Font("宋体", 12), System.Drawing.Brushes.Green, new System.Drawing.PointF(0, 0));
bitmap.Save(context.Response.OutputStream, System.Drawing.Imaging.ImageFormat.Jpeg);
}
}
}
public bool IsReusable {
get {
相关新闻>>
- 发表评论
-
- 最新评论 更多>>