一般处理程序 验证码

一般处理程序 验证码
 
一般处理程序 验证码 
 
public class 验é证¤码 ? : IHttpHandler
{
public void ProcessRequest(HttpContext context)
{
context.Response.ContentType = "image/JPEG";
using (System.Drawing.Bitmap bitmap = new System.Drawing.Bitmap( 200, 50))
{
using (System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(bitmap))
{
g.DrawString( "十ê?一ò?月?的μ?柳á?叶ò?", new System.Drawing.Font( "宋?体ì?", 20), System.Drawing.Brushes.Green, new System.Drawing.PointF( 0, 0));
g.DrawEllipse(System.Drawing.Pens.Red, new System.Drawing.Rectangle( 10, 10, 10, 10));
g.DrawEllipse(System.Drawing.Pens.Red, new System.Drawing.Rectangle( 20, 20, 10, 10));
bitmap.Save(context.Response.OutputStream, System.Drawing.Imaging.ImageFormat.Jpeg);
}
}
}
 
 
随机验证码
一般处理程序 验证码 
Random rand = new Random();
int code = rand.Next();
string strcode = Convert.ToString(code);
g.DrawString(strcode, new System.Drawing.Font( "宋?体ì?", 30), System.Drawing.Brushes.Green, new System.Drawing.PointF( 0, 0));
bitmap.Save(context.Response.OutputStream, System.Drawing.Imaging.ImageFormat.Jpeg);
 
 
完整验证码示例:
 
一般处理程序
public void ProcessRequest(HttpContext context)
{
context.Response.ContentType = "image/JPEG";
using (System.Drawing.Bitmap bitmap = new System.Drawing.Bitmap( 200, 50))
{
using (System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(bitmap))
{
//g.DrawString("十ê?一ò?月?的μ?柳á?叶ò?",new System.Drawing.Font("宋?体ì?", 20), System.Drawing.Brushes.Green,new System.Drawing.PointF(0, 0));
//g.DrawEllipse(System.Drawing.Pens.Red, new System.Drawing.Rectangle(10, 10, 10, 10));
//g.DrawEllipse(System.Drawing.Pens.Red, new System.Drawing.Rectangle(20, 20, 10, 10));
//bitmap.Save(context.Response.OutputStream, System.Drawing.Imaging.ImageFormat.Jpeg);
Random rand = new Random();
int code = rand.Next();
string strcode = Convert.ToString(code);
HttpContext.Current.Session[ "code"] = strcode; //session需Dè要òa通í¨过1yhttpcontext实êμ现?接ó口ú
g.DrawString(strcode, new System.Drawing.Font( "宋?体ì?", 30), System.Drawing.Brushes.Green, new System.Drawing.PointF( 0, 0));
bitmap.Save(context.Response.OutputStream, System.Drawing.Imaging.ImageFormat.Jpeg);
}
}
前台
<form id = "form1" runat = "server" >
<div >
<input type = "image" src = "验é证¤码?.ashx" / >
<asp :TextBox ID = "TextBox1" runat = "server" > < /asp :TextBox >
<asp :Button ID = "Button1" runat = "server" Text = "Button" 
onclick = "Button1_Click" / >
< /div >
< /form >
后台
protected void Button1_Click( object sender, EventArgs e)
{
string strcode = Session[ "code"].ToString();
if (strcode ==TextBox1.Text)
{
Response.Write( "正y确è·");
}
else
{
Response.Write( "错′í误ó");
}
}
刷新验证码 <input type="image" src="验é证¤码?.ashx"  onclick="this.src='验证码.ashx?aaa='+new date()"/>




你可能感兴趣的:(验证码)