1 2 3 usingSystem;4 usingSystem.Web;5 usingSystem.Web.SessionState;6 usingSystem.Drawing;7 usingSystem.Drawing.Imaging;8 usingSystem.Text;9 10 ///11 ///ValidateImageHandler 生成网站验证码功能12 ///13 publicclassValidateImageHandler : IHttpHandler, IRequiresSessionState14 {15 intintLength=5;//长度16 stringstrIdentify="Identify";//随机字串存储键值,以便存储到Session中17 publicValidateImageHandler()18 {19 }20 21 ///22 ///生成验证图片核心代码23 ///24 ///25 publicvoidProcessRequest(HttpContext hc)26 {27 //设置输出流图片格式28 hc.Response.ContentType="image/gif";29 30 Bitmap b=newBitmap(200,60);31 Graphics g=Graphics.FromImage(b);32 g.FillRectangle(newSolidBrush(Color.YellowGreen),0,0,200,60);33 Font font=newFont(FontFamily.GenericSerif,48, FontStyle.Bold, GraphicsUnit.Pixel);34 Random r=newRandom();35 36 //合法随机显示字符列表37 stringstrLetters="abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890";38 StringBuilder s=newStringBuilder();39 40 //将随机生成的字符串绘制到图片上41 for(inti=0; i60 ///表示此类实例是否可以被多个请求共用(重用可以提高性能)61 ///62 publicboolIsReusable63 {64 get65 {66 returntrue;67 }68 }69 }