用一般处理程序做的图片验证码
2008-06-04 18:05
344 查看
新建一般处理程序:ValidateImageHandler.ashx
<%@ WebHandler Language="C#" Class="ValidateImageHandler" %>
using System;
using System.Web;
using System.Web.SessionState;
using System.Drawing;
using System.Drawing.Imaging;
using System.Text;
/// <summary>
/// ValidateImageHandler 生成网站验证码功能
/// </summary>
public class ValidateImageHandler : IHttpHandler, IRequiresSessionState
{
int intLength = 5; //长度
string strIdentify = "Identify"; //随机字串存储键值,以便存储到Session中
public ValidateImageHandler()
{
}
/// <summary>
/// 生成验证图片核心代码
/// </summary>
/// <param name="hc"></param>
public void ProcessRequest(HttpContext hc)
{
//设置输出流图片格式
hc.Response.ContentType = "image/gif";
Bitmap b = new Bitmap(200, 60);
Graphics g = Graphics.FromImage(b);
g.FillRectangle(new SolidBrush(Color.YellowGreen), 0, 0, 200, 60);
Font font = new Font(FontFamily.GenericSerif, 48, FontStyle.Bold, GraphicsUnit.Pixel);
Random r = new Random();
//合法随机显示字符列表
string strLetters = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890";
StringBuilder s = new StringBuilder();
//将随机生成的字符串绘制到图片上
for (int i = 0; i < intLength; i++)
{
s.Append(strLetters.Substring(r.Next(0, strLetters.Length - 1), 1));
g.DrawString(s[s.Length - 1].ToString(), font, new SolidBrush(Color.Blue), i * 38, r.Next(0, 15));
}
//生成干扰线条
Pen pen = new Pen(new SolidBrush(Color.Blue), 2);
for (int i = 0; i < 10; i++)
{
g.DrawLine(pen, new Point(r.Next(0, 199), r.Next(0, 59)), new Point(r.Next(0, 199), r.Next(0, 59)));
}
b.Save(hc.Response.OutputStream, ImageFormat.Gif);
hc.Session[strIdentify] = s.ToString(); //先保存在Session中,验证与用户输入是否一致
hc.Response.End();
}
/// <summary>
/// 表示此类实例是否可以被多个请求共用(重用可以提高性能)
/// </summary>
public bool IsReusable
{
get
{
return true;
}
}
}
调用:
<script language="javascript" type="text/javascript">
<!--
function ChangeCode()
{
document.all.verifyCode.src="ValidateImageHandler.ashx?"+Math.random();
}
-->
</script>
<img id="verifyCode" width="80px" height="20px" src="ValidateImageHandler.ashx" /><a style="cursor:hand;" onclick="ChangeCode();">看不清,换一张?</a>
运行结果:
<%@ WebHandler Language="C#" Class="ValidateImageHandler" %>
using System;
using System.Web;
using System.Web.SessionState;
using System.Drawing;
using System.Drawing.Imaging;
using System.Text;
/// <summary>
/// ValidateImageHandler 生成网站验证码功能
/// </summary>
public class ValidateImageHandler : IHttpHandler, IRequiresSessionState
{
int intLength = 5; //长度
string strIdentify = "Identify"; //随机字串存储键值,以便存储到Session中
public ValidateImageHandler()
{
}
/// <summary>
/// 生成验证图片核心代码
/// </summary>
/// <param name="hc"></param>
public void ProcessRequest(HttpContext hc)
{
//设置输出流图片格式
hc.Response.ContentType = "image/gif";
Bitmap b = new Bitmap(200, 60);
Graphics g = Graphics.FromImage(b);
g.FillRectangle(new SolidBrush(Color.YellowGreen), 0, 0, 200, 60);
Font font = new Font(FontFamily.GenericSerif, 48, FontStyle.Bold, GraphicsUnit.Pixel);
Random r = new Random();
//合法随机显示字符列表
string strLetters = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890";
StringBuilder s = new StringBuilder();
//将随机生成的字符串绘制到图片上
for (int i = 0; i < intLength; i++)
{
s.Append(strLetters.Substring(r.Next(0, strLetters.Length - 1), 1));
g.DrawString(s[s.Length - 1].ToString(), font, new SolidBrush(Color.Blue), i * 38, r.Next(0, 15));
}
//生成干扰线条
Pen pen = new Pen(new SolidBrush(Color.Blue), 2);
for (int i = 0; i < 10; i++)
{
g.DrawLine(pen, new Point(r.Next(0, 199), r.Next(0, 59)), new Point(r.Next(0, 199), r.Next(0, 59)));
}
b.Save(hc.Response.OutputStream, ImageFormat.Gif);
hc.Session[strIdentify] = s.ToString(); //先保存在Session中,验证与用户输入是否一致
hc.Response.End();
}
/// <summary>
/// 表示此类实例是否可以被多个请求共用(重用可以提高性能)
/// </summary>
public bool IsReusable
{
get
{
return true;
}
}
}
调用:
<script language="javascript" type="text/javascript">
<!--
function ChangeCode()
{
document.all.verifyCode.src="ValidateImageHandler.ashx?"+Math.random();
}
-->
</script>
<img id="verifyCode" width="80px" height="20px" src="ValidateImageHandler.ashx" /><a style="cursor:hand;" onclick="ChangeCode();">看不清,换一张?</a>
运行结果:
相关文章推荐
- 一般处理程序下载图片
- html5 file调用一般处理程序上传图片
- “上传图片到服务器”之静态页面与一般处理程序
- 如何读取二进制图片-.ashx一般处理程序 --1
- 上传图片及添加水印一般处理程序的
- 一般处理程序ashx结合gdi+处理图片
- MVC、一般处理程序hanlder 输出图片文件
- ASP.NET 应用程序级 验证用户是否登录 一般处理程序
- .Net一般处理程序来实现用户名的验证
- 异步(ajax)配合一般处理程序 验证用户名
- 一般处理程序上传图片
- 使用一般处理程序(IHttpHandler)制作图片水印
- 【ASP.NET 进阶】无刷新上传图片之一:利用一般处理程序
- ASP.NET的一般处理程序对图片文件的基本操作
- Handler一般处理程序的应用--随机图片生成
- ASP.NET:在一般处理程序中通过 Session 保存验证码却无法显示图片?
- 一般处理程序为图片添加水印
- 一般处理程序 给图片添加文字水印
- ASP_一般处理程序_图片上传
- MVC、一般处理程序hanlder 输出图片文件