当前位置: 首页 > news >正文

图片验证

  /// <summary>
  2     /// ValidaterCodeHandler 的摘要说明(此项是建立一般处理数据)
 3 /// </summary>  4 public class ValidaterCodeHandler : IHttpHandler,IRequiresSessionState  5  {  6 public void ProcessRequest(HttpContext context)  7  {  8 string chechCode = GenCode1(5);  9 // 创建用来绘制验证码的位图  10 System.Drawing.Bitmap image = new System.Drawing.Bitmap(100,23);  11 //从位图中获取对应的绘图类对象  12 Graphics g = Graphics.FromImage(image);  13 // 将产生的验证码先存到Session中,方便与用户填写的验证码进行比较  14 context.Session["code"] = chechCode;  15 try  16  {  17 // 生成随机生成器  18 Random random = new Random();  19 // 清空图片背景色  20  g.Clear(Color.White);  21 // 画图片的背景噪音线  22 for (int i = 0; i < 25; i++)  23  {  24 int x1 = random.Next(image.Width);  25 int x2 = random.Next(image.Width);  26 int y2 = random.Next(image.Height);  27 int y1 = random.Next(image.Height);  28 g.DrawLine(new Pen(Color.Silver),x1,y1,x2,y2);  29  }  30  31 Font font = new Font("Arial",12,System.Drawing.FontStyle.Bold);  32 LinearGradientBrush brush = new LinearGradientBrush(new Rectangle(0,0,image.Width,image.Height),Color.Blue,Color.DarkRed,1.2F,true);  33 g.DrawString(chechCode,font,brush,2,2);  34 // 画图片的前景噪音点  35 g.DrawRectangle(new Pen(Color.Silver),0,0,image.Width - 1,image.Height - 1);  36 MemoryStream ms = new MemoryStream();  37  image.Save(ms,ImageFormat.Gif);  38  context.Response.ClearContent();  39 context.Response.ContentType = "image/Gif";  40  context.Response.BinaryWrite(ms.ToArray());  41  }  42 finally  43  {  44  g.Dispose();  45  image.Dispose();  46  }  47  }  48  49 public bool IsReusable  50  {  51 get  52  {  53 return false;  54  }  55  }  56  57 /// <summary>  58 /// 产生随机数、字母字符串  59 /// </summary>  60 /// <param name="num">随机出几个字符</param>  61 /// <returns>随机出的字符</returns>  62 private string GenCode1(int num)  63  {  64 string str = "0123456789qwertyuioplkjhgfdsazxcvbnm";  65 string code = ""; 66 Random rd = new Random(); 67 for (int i = 0; i < num; i++) 68 { 69 code += str.Substring(rd.Next(0,str.Length),1); 70 } 71 return code; 72 } 73 74 /// <summary> 75 /// 产生汉字 76 /// </summary> 77 /// <param name="num">随机出几个字符</param> 78 /// <returns>随机出的字符串</returns> 79 private string GenCode2(int num) 80 { 81 string str = "啥打法是否客户看大能打开富室发高精度将对方开始"; 82 string code = ""; 83 Random rd = new Random(); 84 for (int i = 0; i < num; i++) 85 { 86 code += str.Substring(rd.Next(0,str.Length),1); 87 } 88 89 return code; 90 } 91 } 92 93 94 95 96 // 页面代码 97 <head runat="server"> 98 <title></title> 99 <script type = "text/javascript"> 100 function fresh() { 101 document.getElementById("code").src = "ValidaterCodeHandler.ashx?time = " + new Date(); 102 } 103 </script> 104 </head> 105 <body> 106 <form id="form1" runat="server"> 107 <div> 108 登录页面 109 <br /> 110 <br /> 111 用户名:<asp:TextBox ID="txtUserName" runat="server"></asp:TextBox> 112 <br /> 113 <br /> 114 密&nbsp; 码:<asp:TextBox ID="txtPwd" runat="server" TextMode="Password"></asp:TextBox> 115 <br /> 116 <br /> 117 验证码:<asp:TextBox ID="txtValidaterCode" runat="server"></asp:TextBox> 118 <img id = "code" src = "ValidaterCodeHandler.ashx" onclick = "fresh()" /> 119 <a href = "javascript:fresh()">看不清?换一个</a> 120 <br /> 121 <br /> 122 <asp:Button ID="btnLogin" runat="server" Text="登录" οnclick="btnLogin_Click" /> 123 124 </div> 125 </form> 126 </body>

转载于:https://www.cnblogs.com/hlh123/p/4705816.html

相关文章:

  • JDBC加载过程
  • 让DIV中文字换行显示
  • hdu 4050 2011北京赛区网络赛K 概率dp ***
  • git stash用法
  • 545E. Paths and Trees
  • hdu 1166 敌兵布阵 ( 线段树或者树状数组)
  • WIN7 自动同步服务器上备份文件
  • swift UI特殊培训38 与滚动码ScrollView
  • Objective-C:在类中设置不同协议
  • React Native 简介:用 JavaScript 搭建 iOS 应用(2)
  • 以ASPX生成静态页
  • android获得屏幕高度和宽度
  • 项目直播:任务管理系统应用
  • 苹果电脑键盘符号记录
  • 转:Windows 8上强制Visual Studio以管理员身份运行
  • Android路由框架AnnoRouter:使用Java接口来定义路由跳转
  • css选择器
  • js面向对象
  • Object.assign方法不能实现深复制
  • php的插入排序,通过双层for循环
  • sessionStorage和localStorage
  • SpringCloud集成分布式事务LCN (一)
  • vue-cli在webpack的配置文件探究
  • 构建工具 - 收藏集 - 掘金
  • 前端攻城师
  • 以太坊客户端Geth命令参数详解
  • 正则学习笔记
  • 曾刷新两项世界纪录,腾讯优图人脸检测算法 DSFD 正式开源 ...
  • #include
  • #QT(智能家居界面-界面切换)
  • #设计模式#4.6 Flyweight(享元) 对象结构型模式
  • ()、[]、{}、(())、[[]]等各种括号的使用
  • (1/2)敏捷实践指南 Agile Practice Guide ([美] Project Management institute 著)
  • (2021|NIPS,扩散,无条件分数估计,条件分数估计)无分类器引导扩散
  • (delphi11最新学习资料) Object Pascal 学习笔记---第8章第5节(封闭类和Final方法)
  • (PyTorch)TCN和RNN/LSTM/GRU结合实现时间序列预测
  • (多级缓存)缓存同步
  • (附源码)ssm旅游企业财务管理系统 毕业设计 102100
  • (蓝桥杯每日一题)love
  • (生成器)yield与(迭代器)generator
  • (译) 理解 Elixir 中的宏 Macro, 第四部分:深入化
  • (转)Google的Objective-C编码规范
  • (转)linux 命令大全
  • (转)利用PHP的debug_backtrace函数,实现PHP文件权限管理、动态加载 【反射】...
  • **Java有哪些悲观锁的实现_乐观锁、悲观锁、Redis分布式锁和Zookeeper分布式锁的实现以及流程原理...
  • .bat批处理(十一):替换字符串中包含百分号%的子串
  • .Net - 类的介绍
  • .NET delegate 委托 、 Event 事件
  • .Net8 Blazor 尝鲜
  • .netcore 获取appsettings
  • .NET文档生成工具ADB使用图文教程
  • .net项目IIS、VS 附加进程调试
  • .sdf和.msp文件读取
  • /etc/fstab 只读无法修改的解决办法
  • @Autowired注解的实现原理