您的位置:首页 > 编程语言 > ASP

asp.net服务端控件之 图形验证码

2010-05-04 02:37 363 查看
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO;
using System.Drawing;
namespace WebApplication2
{
[DefaultProperty("Text")]
[ToolboxData("<{0}:VerificationCode runat=server></{0}:VerificationCode>")]
public class VerificationCode : WebControl,IHttpHandler
{
private String _code="Hello";
public String Code
{
get
{
return _code;
}
set
{
_code = value;
}
}
public VerificationCode() : base(HtmlTextWriterTag.Img) { }
protected override void AddAttributesToRender(HtmlTextWriter writer)
{
base.AddAttributesToRender(writer);
writer.AddAttribute("onclick", "this.src=this.src+'?'");
writer.AddAttribute(HtmlTextWriterAttribute.Src, "img.Seven");
writer.AddAttribute("alt", "Click To Reload");
}
protected override void RenderContents(HtmlTextWriter output){}
bool IHttpHandler.IsReusable
{
get { return true; }
}
void IHttpHandler.ProcessRequest(HttpContext context)
{
MemoryStream ms = new MemoryStream();
Bitmap img = new Bitmap(100, 30);
Graphics g = Graphics.FromImage(img);

g.Clear(ColorTranslator.FromHtml("#CCCCCC"));
g.DrawString(Code, new Font("宋体", 12), Brushes.Black, 10, 10);
img.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);
this.Context.Response.Clear();
this.Context.Response.ContentType = "image/jpeg";
this.Context.Response.BinaryWrite(ms.ToArray());
img.Dispose();
ms.Close();
g.Dispose();
this.Context.Response.End();
}

}
} 


 

wenconfig System.web

<add verb="*" path="*.YourTypeName" type="Yournamespace.className"/>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息