.Net_一般处理程序ashx_生成验证码

<%@ WebHandler Language="C#" Class="imgCode" %>

using System;
using System.Web;
using System.Web.SessionState;
using System.Drawing;
using System.Drawing.Drawing2D;

//在Asp.net中,所有的处理程序类必须实现IHttpHandler接口或者实现IHttpAsyncHandler接口,一个同步,一个异步。
//对于需要读写会话状态的处理程序,必须实现一个特定的标记接口IRequireSessionState,这个接口定义在命名空间System.Web.SessionState中,其中没有定义任何成员
public class imgCode : IHttpHandler, IRequiresSessionState
{
//ProcessRequest是IHttpHandler接口的主要方法,接收并通过一个HttpContext类型的请求上下文对象,处理程序可以得到关于处理请求所需的信息。通过  HttpContext的Response属性可以得到响应的对象,用以向客户端返回服务器处理的结果。
public void ProcessRequest(HttpContext context)
{
//context.Response.ContentType = "text/plain";
//context.Response.Write("Hello World");

CreateImage(CreatecheckCode(5), context);


context.Response.ContentType = "image/jpeg";
//Image image = new Bitmap(60, 30);
//Random random = new Random();
生成随机数
//int code = random.Next(1000, 10000);
//string codeString = code.ToString();
使用会话状态
//context.Session["Code"] = codeString;
//using (Graphics g = Graphics.FromImage(image))
//{
// g.Clear(Color.WhiteSmoke);
// StringFormat sf = new StringFormat();
// sf.Alignment = StringAlignment.Center;
// sf.LineAlignment = StringAlignment.Center;
// g.DrawString(codeString, new Font("Arial", 14), Brushes.Blue, new RectangleF(0, 0, image.Width, image.Height), sf);
//}
//context.Response.ContentType = "image/jpeg";
//image.Save(context.Response.OutputStream, System.Drawing.Imaging.ImageFormat.Jpeg);
}
private string CreatecheckCode(int codeCount)
{
string checkCode = "";
string allChar = "0,1,2,3,4,5,6,7,8,9,A,B,C,D,E,F,G,H,I,J,K,L,M,N,O,P,Q,R,S,T,U,W,X,Y,Z";
string[] allCharArr = allChar.Split(',');
Random rnd = new Random(Guid.NewGuid().GetHashCode());
for (int i = 0; i < codeCount; i++)
{
int t = rnd.Next(35);
checkCode += allCharArr[t];
}
return checkCode;
}
private void CreateImage(string checkCode,HttpContext context)
{
int imgWidth = (int)(checkCode.Length * 16);
Bitmap bmp = new Bitmap(imgWidth, 26);
Graphics grap = Graphics.FromImage(bmp);
grap.Clear(Color.White);
Random rnd = new Random();
//随机画图片的背景噪音线
for (int i = 0; i < checkCode.Length; i++)
{
int x1 = rnd.Next(bmp.Width);
int x2 = rnd.Next(bmp.Width);
int y1 = rnd.Next(bmp.Height);
int y2 = rnd.Next(bmp.Height);
grap.DrawLine(new Pen(Color.Black, rnd.Next(3)), x1, y1, x2, y2);
}
Font fonter = new Font("Arial", 16, FontStyle.Bold);
LinearGradientBrush linear = new LinearGradientBrush(new Rectangle(0, 0, bmp.Width, bmp.Height), Color.Blue, Color.DarkRed, 1.2f, true);
grap.DrawString(checkCode, fonter, linear, 2, 2);
//随机画图片的前景噪音点
for (int j = 0; j < 100; j++)
{
int x = rnd.Next(bmp.Width);
int y = rnd.Next(bmp.Height);
bmp.SetPixel(x, y, Color.FromArgb(rnd.Next()));
}
grap.DrawRectangle(new Pen(Color.Silver), 0, 0, bmp.Width - 1, bmp.Height - 1);
bmp.Save(context.Response.OutputStream, System.Drawing.Imaging.ImageFormat.Png);
grap.Dispose();
bmp.Dispose();
}

//IsReusable属性表示:“当这个处理程序对象在使用之后,是否还可以被缓存起来,在以后的请求处理中再次使用”,这个属性主要用来配合处理程序工厂使用。
public bool IsReusable
{
get
{
return false;
}
}

}

转载于:https://www.cnblogs.com/ccccc05/articles/4211044.html

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值