• 验证码生成-->漂亮啊


    验证码不用输出太多的HTML代码,直接创建一个一般处理程序,直接上代码

      1  public class VCode : IHttpHandler
      2     {
      3 
      4         HttpContext context = null;
      5         public void ProcessRequest(HttpContext context)
      6         {
      7             this.context = context;
      8             CreateVImage(CreateRandomNum(4));
      9         }
     10 
     11         #region 创建随机数 CreateRandomNum(int NumCount)
     12         /// <summary>
     13         /// 创建随机数
     14         /// </summary>
     15         /// <param name="NumCount">条形码上的数字个数</param>
     16         /// <returns>返回随机数</returns>
     17         private string CreateRandomNum(int NumCount)
     18         {
     19             string allChar = "0,1,2,3,4,5,6,7,8,9,A,B,C,D,E,F,G,H,I,J,K,L,M,N,O,P,Q,R,S,T,U,V,W,X,Y,Z";
     20             string[] allCharArr = allChar.Split(',');
     21             string randomNum = "";
     22             int temp = -1;//记录上次随机数的数值,尽量避免产生几个相同的随机数
     23             Random random = new Random();
     24             for (int i = 0; i < NumCount; i++)
     25             {
     26                 if (temp != -1)
     27                 {
     28                     random = new Random(i * temp * ((int)DateTime.Now.Ticks));
     29                 }
     30                 int t = random.Next(36);
     31                 if (temp == t)
     32                 {
     33                     return CreateRandomNum(NumCount);
     34                 }
     35                 temp = t;
     36                 randomNum += allCharArr[t];
     37             }
     38             return randomNum;
     39         }
     40         #endregion
     41 
     42         #region 生成验证码图片  CreateVImage(string validateNum)
     43 
     44         /// <summary>
     45         /// 生成验证码图片 CreateVImage(string validateNum)
     46         /// </summary>
     47         /// <param name="validateNum">验证码数字</param>
     48         private void CreateVImage(string validateNum)
     49         {
     50             if (validateNum == null || validateNum.Trim() == string.Empty)
     51             {
     52                 return;
     53             }
     54             //生成Bitmap图像
     55             Bitmap image = new Bitmap(validateNum.Length * 12 + 10, 22);
     56             Graphics g = Graphics.FromImage(image);
     57             try
     58             {
     59                 //随机数生成
     60                 Random random = new Random();
     61                 //清空图片背景色,将背景色换为白色
     62                 g.Clear(Color.White);
     63                 //画图片的背景线
     64                 for (int i = 0; i < 25; i++)
     65                 {
     66                     int x1 = random.Next(image.Width);
     67                     int x2 = random.Next(image.Width);
     68                     int y1 = random.Next(image.Height);
     69                     int y2 = random.Next(image.Height);
     70                     g.DrawLine(new Pen(Brushes.Beige), x1, y1, x2, y2);
     71                 }
     72                 //设置字体
     73                 Font font = new Font("Arial", 12, (FontStyle.Bold | FontStyle.Italic));
     74                 //设置笔刷
     75                 System.Drawing.Drawing2D.LinearGradientBrush brush =
     76                     new System.Drawing.Drawing2D.LinearGradientBrush(new Rectangle(0, 0, image.Width, image.Height), Color.Blue, Color.DarkRed, 1.2f, true);
     77                 //用设置好的字体和笔刷将验证码写到图片上
     78                 g.DrawString(validateNum, font, brush, 2, 2);
     79                 //画图片上的点
     80                 for (int i = 0; i < 100; i++)
     81                 {
     82                     int x = random.Next(image.Width);
     83                     int y = random.Next(image.Height);
     84                     image.SetPixel(x, y, Color.FromArgb(random.Next()));
     85                 }
     86 
     87                 //画图片边框线
     88                 g.DrawRectangle(new Pen(Color.Silver), 0, 0, image.Width, image.Height);
     89                 //创建一个内存流
     90                 System.IO.MemoryStream ms = new System.IO.MemoryStream();
     91                 //将图片保存到指定流中,并指定图片格式
     92                 image.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);
     93                 //清空缓存区所有内容输出
     94                 context.Response.ClearContent();
     95                 context.Response.ContentType = "image/jpeg";
     96                 context.Response.BinaryWrite(ms.ToArray());
     97             }
     98             catch (Exception ex)
     99             {
    100                 context.Response.Write("异常..."+ex.Message);
    101             }
    102             finally
    103             {
    104                 //释放资源
    105                 g.Dispose();
    106                 image.Dispose();
    107             }
    108 
    109         }
    110         #endregion
    111 
    112         public bool IsReusable
    113         {
    114             get
    115             {
    116                 return false;
    117             }
    118         }
    119     }
    ValidateCode

    直接复制过去,改下类名直接可用,效果图如下:

    周围黑的不是验证码生成的,截图原因而已

  • 相关阅读:
    算法实践--最长公共子序列(Longest Common Subsquence)
    算法实践--最长递增子序列(Longest Increasing Subsquence)
    googletest--Death Test和Exception Test
    googletest--测试控制
    googletest--Test Fixture
    googletest基本测试宏
    使用googletest进行C++单元测试(Netbeans为例)
    Boost--optional
    Boost--any
    Boost--variant (C++中的union)
  • 原文地址:https://www.cnblogs.com/liyajie/p/ValidateCode.html
Copyright © 2020-2023  润新知