您现在的位置是:首页
>
验证码给别人了怎么办 ValidNumber.ashx验证码
ValidNum er.a hx验证码 ValidNum er a hx 代码 lt %@ We Ha dler La guage= C# Cla = ValidNum er % gt u i g S
ValidNumber.ashx验证码

ValidNumber ashx 代码 <%@ WebHandler Language= C# Class= ValidNumber %> using System; using System Web; using System Web SessionState; //第一步 导入此命名空间 public class ValidNumber : IHttpHandler IRequiresSessionState { public void ProcessRequest(HttpContext context) { //种子 string[] strlist = new[] { a b c d q w e r t y u i o p z x n v W Q B A D D F G H J K L }; //生成随机数 string[] list = new string[ ]; Random rd = new Random(DateTime Now Millisecond * ) for (int i = ; i < ; i++) { list[i] = strlist[rd Next(strlist Length)]; System Threading Thread Sleep( ) } System Text StringBuilder sb = new System Text StringBuilder() for (int i = ; i < ; i++) { sb = sb Append(list[i]) } HttpContext Current Session[ validNumber ] = sb; //创建一张图片 System Drawing Bitmap image = new System Drawing Bitmap(list Length * ) //创建画布 System Drawing Graphics g = System Drawing Graphics FromImage(image) //清空背景色 并添充成白色 g Clear(System Drawing Color White) //噪声线 for (int i = ; i < ; i++) { Random rds = new Random() float x = rds Next(image Width ) float x = rds Next(image Width ) float y = rds Next(image Height ) float y = rds Next(image Height ) System Threading Thread Sleep( ) g DrawLine(new System Drawing Pen(System Drawing Color Silver) x y x y ) } System Drawing Font f = new System Drawing Font( 黑体 System Drawing GraphicsUnit Pixel) System Drawing Brush bs = new System Drawing SolidBrush(System Drawing Color Red) g DrawString(sb ToString() f bs ) //当前请求输出的类型 HttpContext Current Response ContentType = image/jpeg ; //保存到输出流 image Save(HttpContext Current Response OutputStream System Drawing Imaging ImageFormat Jpeg) g Dispose() image Dispose() } public bool IsReusable { get { return false; } } } 页面调用时 <img src= ValidNumber ashx alt= 图片验证码 /> 但是回发session值改变页面图片没变 暂无比较理想的解决办法 提交时后台用 Response Write( <script> window location href=window location href</script> ) 刷新下页面 lishixinzhi/Article/program/net/201311/13143
很赞哦! (1078)
- 上一篇
台湾历史老师搞笑讲三国 郭英德老師講《三國演義》(十八):士人政治家的典型——說諸葛亮
郭英德老師講《三國演義》(十八):士人政治家的典型——說諸葛亮 文 郭英德毛宗崗在批評《三國演義》時,極力稱讚「三絕」:諸葛亮、關羽和曹操,其中,諸葛亮被認為是「古今來賢相中第一奇人」(毛本《三國演義
- 下一篇
早点包子的利润有多大 我是一个大二的学生,我想早点准备公务员考试,我该怎么准备好呢?看哪些资料?!
我是一个大二的学生,我想早点准备公务员考试,我该怎么准备好呢?看哪些资料? 我是一个大二的学生,我想早点准备公务员考试,我该怎么准备好呢?看哪些资料?公务员复习不用提前2年准备的,一般学生在半年一年时