ValidNumberashx 代码 <%@ WebHandler Language=C# Class=ValidNumber %> using System; using SystemWeb; using SystemWebSessionState; //第一步导入此命名空间 public class ValidNumber : IHttpHandler IRequiresSessionState { public void ProcessRequest(HttpContext context) { //种子 string[] strlist = new[] { abcdqwertyuiopzxnvWQBADDFGHJKL }; //生成随机数 string[] list = new string[]; Random rd = new Random(DateTimeNowMillisecond * ) for (int i = ; i < ; i++) { list[i] = strlist[rdNext(strlistLength)]; SystemThreadingThreadSleep() } SystemTextStringBuilder sb = new SystemTextStringBuilder() for (int i = ; i < ; i++) { sb = sbAppend(list[i]) } HttpContextCurrentSession[validNumber] = sb; //创建一张图片 SystemDrawingBitmap image = new SystemDrawingBitmap(listLength * ) //创建画布 SystemDrawingGraphics g = SystemDrawingGraphicsFromImage(image) //清空背景色并添充成白色 gClear(SystemDrawingColorWhite) //噪声线 for (int i = ; i < ; i++) { Random rds = new Random() float x = rdsNext(imageWidth ) float x = rdsNext(imageWidth ) float y = rdsNext(imageHeight ) float y = rdsNext(imageHeight ) SystemThreadingThreadSleep() gDrawLine(new SystemDrawingPen(SystemDrawingColorSilver) x y x y) } SystemDrawingFont f = new SystemDrawingFont(黑体 SystemDrawingGraphicsUnitPixel) SystemDrawingBrush bs = new SystemDrawingSolidBrush(SystemDrawingColorRed) gDrawString(sbToString() f bs ) //当前请求输出的类型 HttpContextCurrentResponseContentType = image/jpeg; //保存到输出流 imageSave(HttpContextCurrentResponseOutputStream SystemDrawingImagingImageFormatJpeg) gDispose() imageDispose() } public bool IsReusable { get { return false; } } } 页面调用时 <img src=ValidNumberashx alt=图片验证码 /> 但是回发session值改变页面图片没变暂无比较理想的解决办法提交时后台用 ResponseWrite(<script> windowlocationhref=windowlocationhref</script>) 刷新下页面 |