asp.net一般处理程序(.ashx)动态生成验证码案例。
2014-04-28 21:07
676 查看
{使用一般处理程序动态生成验证码}
1.新建WebSite项目,添加一般处理程序命名为 yzm.ashx,添加如下代码:
public void ProcessRequest(HttpContext context)
{
//将context.Response.ContentType = "text/plain";修改为context.Response.ContentType = "image/JPEG";
context.Response.ContentType = "image/JPEG";
using (System.Drawing.Bitmap bitmap =
new System.Drawing.Bitmap(150, 50))
{
using (System.Drawing.Graphics grapgics =
System.Drawing.Graphics.FromImage(bitmap))
{
//测试示例
/*grapgics.DrawString("哈喽",
new System.Drawing.Font("宋体", 20),
System.Drawing.Brushes.Blue
, new System.Drawing.PointF(0, 0));
System.Drawing.Pen pen = (System.Drawing.Pen)
System.Drawing.Pens.Red.Clone();
pen.Width = 3;
grapgics.DrawEllipse(pen, new System.Drawing.Rectangle(20, 20, 10, 10));
bitmap.Save(context.Response.OutputStream,
System.Drawing.Imaging.ImageFormat.Jpeg);*/
//生成随机数
Random random = new Random();
int code = random.Next();
string strCode = code.ToString();
//存session
HttpContext.Current.Session["code"] = strCode;
grapgics.DrawString(strCode,
new System.Drawing.Font("宋体", 20),
System.Drawing.Brushes.Blue
, new System.Drawing.PointF(0, 0));
bitmap.Save(context.Response.OutputStream,
System.Drawing.Imaging.ImageFormat.Jpeg);
}
}
//本身就有的
context.Response.Write("Hello World");
}
2.新建webApplication。
在<div>中添加<img src="yzm.ashx"/>
添加TextBox 和Button,Button的单击事件如下
protected void Button1_Click(object sender, EventArgs e)
{
string stryzm = Convert.ToString(Session["code"]);
if (stryzm==TextBox1.Text)
{
Response.Write("正确");
}
else
{
Response.Write("错误");
}
}
3.随后即可在WebApplication中进行测试。
1.新建WebSite项目,添加一般处理程序命名为 yzm.ashx,添加如下代码:
public void ProcessRequest(HttpContext context)
{
//将context.Response.ContentType = "text/plain";修改为context.Response.ContentType = "image/JPEG";
context.Response.ContentType = "image/JPEG";
using (System.Drawing.Bitmap bitmap =
new System.Drawing.Bitmap(150, 50))
{
using (System.Drawing.Graphics grapgics =
System.Drawing.Graphics.FromImage(bitmap))
{
//测试示例
/*grapgics.DrawString("哈喽",
new System.Drawing.Font("宋体", 20),
System.Drawing.Brushes.Blue
, new System.Drawing.PointF(0, 0));
System.Drawing.Pen pen = (System.Drawing.Pen)
System.Drawing.Pens.Red.Clone();
pen.Width = 3;
grapgics.DrawEllipse(pen, new System.Drawing.Rectangle(20, 20, 10, 10));
bitmap.Save(context.Response.OutputStream,
System.Drawing.Imaging.ImageFormat.Jpeg);*/
//生成随机数
Random random = new Random();
int code = random.Next();
string strCode = code.ToString();
//存session
HttpContext.Current.Session["code"] = strCode;
grapgics.DrawString(strCode,
new System.Drawing.Font("宋体", 20),
System.Drawing.Brushes.Blue
, new System.Drawing.PointF(0, 0));
bitmap.Save(context.Response.OutputStream,
System.Drawing.Imaging.ImageFormat.Jpeg);
}
}
//本身就有的
context.Response.Write("Hello World");
}
2.新建webApplication。
在<div>中添加<img src="yzm.ashx"/>
添加TextBox 和Button,Button的单击事件如下
protected void Button1_Click(object sender, EventArgs e)
{
string stryzm = Convert.ToString(Session["code"]);
if (stryzm==TextBox1.Text)
{
Response.Write("正确");
}
else
{
Response.Write("错误");
}
}
3.随后即可在WebApplication中进行测试。
相关文章推荐
- ASP.NET用一般处理程序生成验证码
- 利用ASP.NET一般处理程序动态生成Web图像(转)
- .net一般处理程序ashx生成验证码(原创+转载)
- 利用ASP.NET一般处理程序动态生成Web图像
- ASP.NET知识总结(一般处理程序动态处理图片(验证码、水印、缩略图))
- asp.net 一般处理程序 .ashx 处理各种验证码 一维码 二维码
- ASP.NET用一般处理程序生成验证码
- 在ASP.NET中使用一般处理程序生成验证码
- asp.net中使用一般处理程序生成验证码
- asp.net的验证码插件及方法、ashx验证码一般处理程序
- ASP.NET使用一般处理程序生成验证码
- Asp.net jQuery与ashx 安全性问题 (一般处理程序的安全问题) ashx 安全
- asp.net 一般处理程序实现网站验证码
- .NET使用一般处理程序生成验证码!
- Asp.net jQuery与ashx 安全性问题 (一般处理程序的安全问题) ashx 安全
- ASP.NET的ASHX一般处理程序中Session为null且不能赋值
- .NET使用一般处理程序生成验证码!
- ASP.NET一般处理程序(.ashx.cs)中使用Session的问题
- ASP.NET的ASHX一般处理程序中Session为null且不能赋值
- asp.net 一般处理程序Handler.ashx应用实例