验证码

Login.aspx代码

1 <td valign="middle" align="left">
2 <img height="20" id="imgRandom" onclick="changeImage()" title="看不清?点击图片换一张" src="CheckCode.aspx" style="cursor:pointer"  />                                       
3 </td>

CheckCode.aspx代码

 1 <%@ Page Language="C#" AutoEventWireup="true" Inherits="Login_CheckCode" Codebehind="CheckCode.aspx.cs" %>
 2 
 3 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
 4 
 5 <html xmlns="http://www.w3.org/1999/xhtml" >
 6 <head runat="server">
 7     <title>无标题页</title>
 8 </head>
 9 <body>
10     <form id="form1" runat="server">
11     <div>
12     
13     </div>
14     </form>
15 </body>
16 </html>

CheckCode.aspx.cs代码

  1 using System;
  2 using System.Data;
  3 using System.Configuration;
  4 using System.Collections;
  5 using System.Web;
  6 using System.Web.Security;
  7 using System.Web.UI;
  8 using System.Web.UI.WebControls;
  9 using System.Web.UI.WebControls.WebParts;
 10 using System.Web.UI.HtmlControls;
 11 using System.Drawing;
 12 using System.IO;
 13 
 14 public partial class Login_CheckCode : System.Web.UI.Page
 15 {
 16     protected void Page_Load(object sender, System.EventArgs e)
 17     {
 18         CreateCheckCodeImage(GenerateCheckCode());
 19     }
 20 
 21     private string GenerateCheckCode()
 22     {
 23         int number;
 24         char code;
 25         string checkCode = String.Empty;
 26 
 27         Random random = new Random();
 28 
 29         for (int i = 0; i < 4; i++)
 30         {
 31             number = random.Next();
 32 
 33            // if (number % 2 == 0)
 34                 code = (char)('0' + (char)(number % 10));
 35            // else
 36               //  code = (char)('A' + (char)(number % 26)); 
 37                  
 38              
 39             checkCode += code.ToString();
 40         }
 41         Session["CheckCode"] = checkCode;
 42         HttpCookie cookie = new HttpCookie("CheckCode", checkCode);
 43         this.Response.Cookies.Add(cookie);
 44         return checkCode;
 45         
 46     }
 47 
 48     private void CreateCheckCodeImage(string checkCode)
 49     {
 50         if (checkCode == null || checkCode.Trim() == String.Empty)
 51             return;
 52 
 53         System.Drawing.Bitmap image = new System.Drawing.Bitmap((int)Math.Ceiling((checkCode.Length * 13.99)), 23);
 54         Graphics g = Graphics.FromImage(image);
 55 
 56         try
 57         {
 58             //生成随机生成器
 59             Random random = new Random();
 60 
 61             //清空图片背景色
 62             g.Clear(Color.White);
 63 
 64             //画图片的背景噪音线
 65             for (int i = 0; i < 32; i++)
 66             {
 67                 int x1 = random.Next(image.Width);
 68                 int x2 = random.Next(image.Width);
 69                 int y1 = random.Next(image.Height);
 70                 int y2 = random.Next(image.Height);
 71 
 72                 g.DrawLine(new Pen(Color.Silver), x1, y1, x2, y2);
 73             }
 74 
 75             Font font = new System.Drawing.Font("Arial", 14, (System.Drawing.FontStyle.Bold | System.Drawing.FontStyle.Italic));
 76             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);
 77             g.DrawString(checkCode, font, brush, 1, 0);
 78 
 79             //画图片的前景噪音点
 80             for (int i = 0; i < 105; i++)
 81             {
 82                 int x = random.Next(image.Width);
 83                 int y = random.Next(image.Height);
 84 
 85                 //Bitmap.SetPixel 方法 :设置 Bitmap 对象中指定像素的颜色。
 86                 image.SetPixel(x, y, Color.FromArgb(random.Next()));
 87             }
 88 
 89             //画图片的边框线
 90             g.DrawRectangle(new Pen(Color.Silver), 0, 0, image.Width - 1, image.Height - 1);
 91 
 92             System.IO.MemoryStream ms = new System.IO.MemoryStream();
 93             image.Save(ms, System.Drawing.Imaging.ImageFormat.Gif);
 94             Response.ClearContent();
 95             Response.ContentType = "image/Gif";
 96             Response.BinaryWrite(ms.ToArray());
 97         }
 98         finally
 99         {
100             g.Dispose();
101             image.Dispose();
102         }
103     }
104 }

点击验证码更换验证码:Login.aspx代码

1     <script type="text/javascript" language="javascript">
2         function changeImage()
3         {
4             document.getElementById("imgRandom").src=document.getElementById("imgRandom").src+'?';
5         }
6     </script>

 

posted @ 2016-05-10 18:30  涂山吕吕  阅读(196)  评论(0编辑  收藏  举报