zoukankan      html  css  js  c++  java
  • ASP.NET画验证码

      1 protected void Page_Load(object sender, EventArgs e)
      2 
      3     {
      4 
      5         CreateCheckCodeImage(GenerateCheckCode());
      6 
      7     }
      8 
      9  
     10 
     11     private string GenerateCheckCode()
     12 
     13     {
     14 
     15         int number;
     16 
     17         char code;
     18 
     19         string checkCode = String.Empty;
     20 
     21         System.Random random = new Random();
     22 
     23  
     24 
     25         for (int i = 0; i < 4; i++)
     26 
     27         {
     28 
     29             number = random.Next();
     30 
     31  
     32 
     33             if (number % 2 == 0)
     34 
     35                 code = (char)('0' + (char)(number % 10));
     36 
     37             else
     38 
     39                 code = (char)('A' + (char)(number % 26));
     40 
     41  
     42 
     43             checkCode += code.ToString();
     44 
     45         }
     46 
     47         Response.Cookies.Add(new HttpCookie("CheckCode", checkCode));
     48 
     49         return checkCode;
     50 
     51     }
     52 
     53  
     54 
     55     private void CreateCheckCodeImage(string checkCode)
     56 
     57     {
     58 
     59         if (checkCode == null || checkCode.Trim() == String.Empty)
     60 
     61             return;
     62 
     63         System.Drawing.Bitmap image = new System.Drawing.Bitmap((int)Math.Ceiling((checkCode.Length * 12.5)), 22);
     64 
     65         Graphics g = Graphics.FromImage(image);
     66 
     67         try
     68 
     69         {
     70 
     71             //生成随机生成器
     72 
     73             Random random = new Random();
     74 
     75             //清空图片背景色
     76 
     77             g.Clear(Color.White);
     78 
     79             //画图片的背景噪音线
     80 
     81             for (int i = 0; i < 2; i++)
     82 
     83             {
     84 
     85                 int x1 = random.Next(image.Width);
     86 
     87                 int x2 = random.Next(image.Width);
     88 
     89                 int y1 = random.Next(image.Height);
     90 
     91                 int y2 = random.Next(image.Height);
     92 
     93                 g.DrawLine(new Pen(Color.Black), x1, y1, x2, y2);
     94 
     95             }
     96 
     97             Font font = new System.Drawing.Font("Arial", 12, (System.Drawing.FontStyle.Bold | System.Drawing.FontStyle.Italic));
     98 
     99             System.Drawing.Drawing2D.LinearGradientBrush brush = new System.Drawing.Drawing2D.LinearGradientBrush(new Rectangle(0, 0, image.Width, image.Height), Color.Blue, Color.DarkRed, 1.2f, true);
    100 
    101             g.DrawString(checkCode, font, brush, 2, 2);
    102 
    103             //画图片的前景噪音点
    104 
    105             for (int i = 0; i < 100; i++)
    106 
    107             {
    108 
    109                 int x = random.Next(image.Width);
    110 
    111                 int y = random.Next(image.Height);
    112 
    113                 image.SetPixel(x, y, Color.FromArgb(random.Next()));
    114 
    115             }
    116 
    117             //画图片的边框线
    118 
    119             g.DrawRectangle(new Pen(Color.Silver), 0, 0, image.Width - 1, image.Height - 1);
    120 
    121             System.IO.MemoryStream ms = new System.IO.MemoryStream();
    122 
    123             image.Save(ms, System.Drawing.Imaging.ImageFormat.Gif);
    124 
    125             Response.ClearContent();
    126 
    127             Response.ContentType = "image/Gif";
    128 
    129             Response.BinaryWrite(ms.ToArray());
    130 
    131         }
    132 
    133         finally
    134 
    135         {
    136 
    137             g.Dispose();
    138 
    139             image.Dispose();
    140 
    141         }
    142 
    143 }
  • 相关阅读:
    java 复习001
    Some_sort_algorithms
    Install_pygments
    Install_ruby
    Ubuntu_wifi&pppoe
    Some_problem_with_octopress
    复习C语言
    VSim [a Racing-simulator by Vell001]
    Hello, Github Blog
    我的新计划 《2Dof Racing Simulator》2014/3/9 20:30:00
  • 原文地址:https://www.cnblogs.com/it-bobo/p/3860738.html
Copyright © 2011-2022 走看看