ASP.NET 实现验证码以及刷新验证码

实现代码

  1 /// <summary>
  2     /// 生成验证码图片,保存session名称VerificationCode
  3     /// </summary>
  4     public static void CreateVerificationCode()
  5     {
  6         int number;
  7         string checkCode = string.Empty;
  8 
  9         //随机数种子
 10         Random randoms = new Random();
 11 
 12         for (int i = 0; i < 4; i++) //校验码长度为4
 13         {
 14             //随机的整数
 15             number = randoms.Next();
 16 
 17             //字符从0-9,A-Z中随机产生,对应的ASCII码分别为
 18             //48-57,65-90
 19             number = number % 36;
 20             if (number < 10)
 21             {
 22                 number += 48;
 23             }
 24             else
 25             {
 26                 number += 55;
 27             }
 28             checkCode += ((char)number).ToString();
 29         }
 30 
 31         //在session中保存校验码
 32         System.Web.HttpContext.Current.Session["VerificationCode"] = checkCode;
 33 
 34         //若校验码为空,则直接返回
 35         if (checkCode == null || checkCode.Trim() == String.Empty)
 36         {
 37             return;
 38         }
 39         //根据校验码的长度确定输出图片的长度
 40         System.Drawing.Bitmap image = new System.Drawing.Bitmap(55, 20);//(int)Math.Ceiling(Convert.ToDouble(checkCode.Length * 15))
 41         //创建Graphics对象
 42         Graphics g = Graphics.FromImage(image);
 43         try
 44         {
 45             //生成随机数种子
 46             Random random = new Random();
 47             //清空图片背景色
 48             g.Clear(Color.White);
 49             //画图片的背景噪音线 10条
 50             //---------------------------------------------------
 51             for (int i = 0; i < 10; i++)
 52             {
 53                 //噪音线起点坐标(x1,y1),终点坐标(x2,y2)
 54                 int x1 = random.Next(image.Width);
 55                 int x2 = random.Next(image.Width);
 56                 int y1 = random.Next(image.Height);
 57                 int y2 = random.Next(image.Height);
 58 
 59                 //用银色画出噪音线
 60                 g.DrawLine(new Pen(Color.Silver), x1, y1, x2, y2);
 61             }
 62             //---------------------------------------------------
 63             //Brush b = Brushes.Silver;
 64             //g.FillRectangle(b, 0, 0, image.Width, image.Height);
 65             //---------------------以上两种任选其一------------------------------
 66             //输出图片中校验码的字体: 12号Arial,粗斜体
 67             Font font = new Font("Arial", 12, (FontStyle.Bold | FontStyle.Italic));
 68 
 69             //线性渐变画刷
 70             LinearGradientBrush brush = new LinearGradientBrush(new Rectangle(0, 0, image.Width, image.Height), Color.Blue, Color.Purple, 1.2f, true);
 71             g.DrawString(checkCode, font, brush, 2, 2);
 72 
 73             //画图片的前景噪音点 50个
 74             for (int i = 0; i < 50; i++)
 75             {
 76                 int x = random.Next(image.Width);
 77                 int y = random.Next(image.Height);
 78                 image.SetPixel(x, y, Color.FromArgb(random.Next()));
 79             }
 80 
 81             //画图片的边框线
 82             g.DrawRectangle(new Pen(Color.Peru), 0, 0, image.Width - 1, image.Height - 1);
 83 
 84             //创建内存流用于输出图片
 85             using (MemoryStream ms = new MemoryStream())
 86             {
 87                 //图片格式指定为png
 88                 image.Save(ms, ImageFormat.Jpeg);
 89                 //清除缓冲区流中的所有输出
 90                 System.Web.HttpContext.Current.Response.ClearContent();
 91                 //输出流的HTTP MIME类型设置为"image/Png"
 92                 System.Web.HttpContext.Current.Response.ContentType = "image/Jpeg";
 93                 //输出图片的二进制流
 94                 System.Web.HttpContext.Current.Response.BinaryWrite(ms.ToArray());
 95             }
 96         }
 97         finally
 98         {
 99             //释放Bitmap对象和Graphics对象
100             g.Dispose();
101             image.Dispose();
102         }
103     }
Create Verification Code

创建一个aspx页面

1 <%@ Page Language="C#" AutoEventWireup="true" CodeFile="AuthCode.aspx.cs" Inherits="AuthCode" %>
2 
3 <%Help.CreateVerificationCode(); %>
Create WebFrom

添加HTML代码,引用

1 <div class="positionR">
2     <label>验证码:</label>
3     <span class="style1"> *</span>
4     <input type="text" class="yanZm" runat="Server" reg="^.+$" id="txtAuthCode" tip="请输入验证码!" />
5     <img class="yanZm_img" src="AuthCode.aspx" alt="" id="imgAuthCode" />
6 </div>
HTML Code

如何实现刷新?

1     <script type="text/javascript">
2         $("#imgAuthCode").click(function () {
3             $(this).attr("src", "AuthCode.aspx?code=" + (new Date()).getTime());
4         });
5     </script>
Refresh - JS Code

效果图

 注明:内容来源不可考 = = 验证码图片生成非原创,如果谁知道原作者请告诉我.

DEMO

原文地址:https://www.cnblogs.com/Chendaqian/p/3357386.html