代码改变世界

图片验证

2015-08-05 20:50  辉仔1  阅读(165)  评论(0编辑  收藏  举报
  /// <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="登录" onclick="btnLogin_Click" />
123 
124     </div>
125     </form>
126 </body>