首页 > 代码库 > 验证码的实现

验证码的实现

验证码的实现方式有很多种,我用的是:新建一个网页,在里面编写代码,然后实现验证码的产生,在然后通过img路径的修改实现。具体代码如下:

这是img的修改:

然后新建一个网站页面code.aspx,编辑代码如下:

  1 using System;  2 using System.Collections.Generic;  3 using System.Drawing;  4 using System.Drawing.Drawing2D;  5 using System.Drawing.Imaging;  6 using System.IO;  7 using System.Linq;  8 using System.Text;  9 using System.Web; 10 using System.Web.UI; 11 using System.Web.UI.WebControls; 12  13 public partial class code : System.Web.UI.Page 14 { 15     private string code_str = string.Empty; 16  17     private const int ImageHeigth = 22;             //验证码图片的高度 18  19     private const double ImageLengthBase = 12.5;    //验证码图片中每个字符的宽度 20  21     private const int ImageLineNumber = 25;         //噪音线的数量 22  23     private const int ImagePointNumber = 100;       //噪点的数量 24  25     private int length = 4; 26  27     public string VALIDATECODEKEY;       //此处用来保存验证码到Session的Key的名称 28  29  30  31  32     /// <summary> 33  34     /// 产生随机的验证码并加入Session 35  36     /// </summary> 37  38     /// <param name="length">验证码长度</param> 39  40     /// <returns></returns> 41  42     public string CreateCode(int length) 43     { 44  45         if (length <= 0) 46         { 47  48             return string.Empty; 49  50         } 51  52         Random random = new Random(); 53  54         StringBuilder builder = new StringBuilder(); 55  56         //产生随机的验证码并拼接起来 57  58         for (int i = 0; i < length; i++) 59         { 60  61             builder.Append(random.Next(0, 10)); 62  63         } 64  65         this.code_str = builder.ToString(); 66  67         this.Session[VALIDATECODEKEY] = this.code_str; 68         Session["code"] = code_str; 69         return this.code_str; 70  71     } 72  73  74  75     /// <summary> 76  77     /// 根据长度产生验证码 78  79     /// 并将验证码画成图片 80  81     /// </summary> 82  83     /// <param name="length">验证码长度</param> 84  85     public void CreateValidateImage(int length) 86     { 87  88         this.code_str = this.CreateCode(length); 89  90         this.CreateValidateImage(this.code_str); 91  92     } 93  94     /// <summary> 95  96     /// 根据产生的验证码生成图片 97  98     /// </summary> 99 100     /// <param name="code">验证码</param>101 102     public void CreateValidateImage(string code)103     {104 105         if (!string.IsNullOrEmpty(code))106         {107 108             this.Session[VALIDATECODEKEY] = code;109 110             //初始化位图Bitmap对象,指定图片对象的大小(宽,高)111 112             Bitmap image = new Bitmap((int)Math.Ceiling((double)(code.Length * ImageLengthBase)), ImageHeigth);113 114             //初始化一块画布115 116             Graphics graphics = Graphics.FromImage(image);117 118             Random random = new Random();119 120             try121             {122 123                 int num5;124 125                 graphics.Clear(Color.White);126 127                 //绘制噪音线128 129                 for (num5 = 0; num5 < ImageLineNumber; num5++)130                 {131 132                     int num = random.Next(image.Width);133 134                     int num3 = random.Next(image.Height);135 136                     int num2 = random.Next(image.Width);137 138                     int num4 = random.Next(image.Height);139 140                     graphics.DrawLine(new Pen(Color.Silver), num, num3, num2, num4);141 142                 }143 144                 //验证码字体样式145 146                 Font font = new Font("Tahoma", 12, FontStyle.Italic | FontStyle.Bold);147 148                 LinearGradientBrush brush = new LinearGradientBrush(new Rectangle(0, 0, image.Width, image.Height), Color.Blue, Color.DarkRed, 1.2f, true);149 150                 //绘制验证码到画布151 152                 graphics.DrawString(code, font, brush, (float)2, (float)2);153 154                 //绘制噪点155 156                 for (num5 = 0; num5 < ImagePointNumber; num5++)157                 {158 159                     int x = random.Next(image.Width);160 161                     int y = random.Next(image.Height);162 163                     image.SetPixel(x, y, Color.FromArgb(random.Next()));164 165                 }166 167                 graphics.DrawRectangle(new Pen(Color.Silver), 0, 0, image.Width - 1, image.Height - 1);168 169                 MemoryStream stream = new MemoryStream();170 171                 //保存图片172 173                 image.Save(stream, ImageFormat.Gif);174 175                 base.Response.ClearContent();176 177                 base.Response.ContentType = "image/Gif";178 179                 base.Response.BinaryWrite(stream.ToArray());180 181             }182 183             finally184             {185 186                 graphics.Dispose();187 188                 image.Dispose();189 190             }191 192         }193 194     }195 196     protected override void onl oad(EventArgs e)197     {198 199         this.CreateValidateImage(this.length);200 201     }202 203 204 205     // Properties206 207     public string Code208     {209 210         get211         {212 213             return this.Code;214 215         }216 217     }218 219     public int Length220     {221 222         get223         {224 225             return this.length;226 227         }228 229         set230         {231 232             this.length = value;233 234         }235 236     }237 238 }
验证码