zoukankan      html  css  js  c++  java
  • 生成验证码

    Login.html

     1 <!DOCTYPE html>
     2 <html xmlns="http://www.w3.org/1999/xhtml">
     3 <head>
     4 <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
     5     <title></title>
     6 </head>
     7 <body>
     8     <table>
     9         <tr>
    10             <td>用户名:</td>
    11             <td><input type="text" /></td>
    12         </tr>
    13         <tr>
    14             <td>密码:</td>
    15             <td><input type="password" /></td>
    16         </tr>
    17         <tr>
    18             <td>验证码</td>
    19             <td><img src="ValidCode.ashx" /></td>
    20         </tr>
    21         <tr></tr>
    22     </table>
    23 </body>
    24 </html>
    View Code

    ValidCode.ashx

     1 <%@ WebHandler Language="C#" Class="ValidCode" %>
     2 
     3 using System;
     4 using System.Web;
     5 using System.Drawing;
     6 
     7 public class ValidCode : IHttpHandler
     8 {
     9 
    10     public void ProcessRequest(HttpContext context)
    11     {
    12         context.Response.ContentType = "image/jpeg";
    13         //生成随机验证码的字符串
    14         string code = MakeRanStr();
    15         using (Image img = new Bitmap(100, 26))
    16         {
    17             using (Graphics g = Graphics.FromImage(img))
    18             {
    19                 //背景为白色
    20                 g.Clear(Color.White);
    21                 //验证码边框
    22                 g.DrawRectangle(Pens.Black, new Rectangle(0, 0, img.Width - 1, img.Height - 1));
    23                 //干扰线
    24                 DrwaGanRaoXian(50, g, img);
    25                 g.DrawString(code, new Font("微软雅黑", 12), Brushes.Red, 1, 2);                
    26             }
    27             img.Save(context.Response.OutputStream, System.Drawing.Imaging.ImageFormat.Jpeg);
    28         }
    29     }
    30 
    31     /// <summary>
    32     /// 画干扰线
    33     /// </summary>
    34     void DrwaGanRaoXian(int count, Graphics g, Image img)
    35     {
    36         Random ran = new Random();
    37         for (int i = 0; i < count; i++)
    38         {
    39             Point p1 = new Point(ran.Next(img.Width), ran.Next(img.Height));
    40             Point p2 = new Point(p1.X - ran.Next(-50, 50), p1.Y - ran.Next(-50, 50));
    41             g.DrawLine(Pens.Black, p1, p2);
    42         }
    43     }
    44 
    45     /// <summary>
    46     /// 生成随机码
    47     /// </summary>
    48     /// <returns></returns>
    49     string MakeRanStr()
    50     {
    51         char[] charArr = new char[10] { 'a', 'b', 'c', '1', '2', '3', '', '', '', '' };
    52         string strValidCode = "";
    53         Random ran = new Random();
    54         int index = 0;
    55         for (int i = 0; i < 5; i++)
    56         {
    57             index = ran.Next(9);
    58             strValidCode += charArr[index];
    59         }
    60         return strValidCode;
    61     }
    62 
    63     public bool IsReusable
    64     {
    65         get
    66         {
    67             return false;
    68         }
    69     }
    70 
    71 }
    View Code
  • 相关阅读:
    hdu4726
    hdu2709
    hdu4706
    hdu4715
    快速幂取模
    快速幂
    asp.net中页面传值
    微信小程序支付
    sql 查询重复记录值取一条
    bower使用
  • 原文地址:https://www.cnblogs.com/HuoAA/p/4087935.html
Copyright © 2011-2022 走看看