aspx類的驗證碼處理程式,隨後還會記錄一般程式的的驗證碼類 1 using System; 2 using System.Collections.Generic; 3 using System.Web; 4 using System.Web.UI; 5 using System.Web.UI.We ...
aspx類的驗證碼處理程式,隨後還會記錄一般程式的的驗證碼類
1 using System; 2 using System.Collections.Generic; 3 using System.Web; 4 using System.Web.UI; 5 using System.Web.UI.WebControls; 6 using System.Drawing; 7 using Maticsoft.Common; 8 namespace Maticsoft.Web 9 { 10 public partial class ValidateCode : System.Web.UI.Page 11 { 12 private static char[] constant = //聲明一個靜態的字元數組,驗證碼從這裡面隨機的取 13 { 14 '1','2','3','4','5','6','7','8','9', 15 'a','b','c','d','e','f','g','h','i','j','k','l','m','n','p','q','r','s','t','u','v','w','x','y','z', 16 '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' 17 }; 18 private void Page_Load(object sender, System.EventArgs e) 19 { 20 string checkCode = CreateNumber(4); 21 // Session["validateCode"] = checkCode.ToLower() 22 Common_Sesson.Add("validateCode", checkCode.ToLower()); 23 CreateCheckCodeImage(checkCode); 24 } 25 26 #region 畫出圖片並顯示到調用這個方法的畫布上 27 private void CreateCheckCodeImage(string code) 28 { 29 var imageCode = new System.Drawing.Bitmap((int)Math.Ceiling((code.Length * 12.5)), 23); //定義圖片的寬度和高度 30 var g = Graphics.FromImage(imageCode); //載入圖片到畫布上 31 32 HttpContext.Current.Session["Code_Char"] = code; //將驗證碼寫入到session中實現驗證 33 34 try 35 { 36 var random = new Random(); 37 g.Clear(Color.White); //清空圖片背景色 38 39 //畫圖片的背景噪音線 40 for (int i = 0; i < 25; i++) 41 { 42 var x1 = random.Next(imageCode.Width); 43 var x2 = random.Next(imageCode.Width); 44 var y1 = random.Next(imageCode.Height); 45 var y2 = random.Next(imageCode.Height); 46 47 g.DrawLine(new Pen(Color.Silver), new Point(x1, y1), new Point(x2, y2)); 48 } 49 50 var font = new System.Drawing.Font("Arial", 12F, System.Drawing.FontStyle.Bold | System.Drawing.FontStyle.Italic); 51 var brush = new System.Drawing.Drawing2D.LinearGradientBrush( 52 new Rectangle(0, 0, imageCode.Width, imageCode.Height), 53 Color.Blue, Color.DarkRed, 1.2F, true); 54 g.DrawString(code, font, brush, 2, 2); 55 56 //畫圖片的前景噪音點 57 for (int i = 0; i < 100; i++) 58 { 59 var x = random.Next(imageCode.Width); 60 var y = random.Next(imageCode.Height); 61 imageCode.SetPixel(x, y, Color.FromArgb(random.Next())); 62 } 63 64 //畫圖片的邊框線 65 g.DrawRectangle(new Pen(Color.Silver), 0, 0, imageCode.Width - 1, imageCode.Height - 1); 66 var ms = new System.IO.MemoryStream(); 67 imageCode.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg); 68 69 HttpContext.Current.Response.ClearContent(); 70 HttpContext.Current.Response.ContentType = "image/jpg"; 71 HttpContext.Current.Response.BinaryWrite(ms.ToArray()); 72 } 73 finally 74 { 75 g.Dispose(); 76 imageCode.Dispose(); 77 } 78 } 79 #endregion 80 #region 生成0——60的隨機數 81 private static string CreateNumber(int n) //n表示產生字元串的長度 82 { 83 string str = ""; 84 System.Text.StringBuilder newRandom = new System.Text.StringBuilder(60); 85 Random rd = new Random(); 86 while (n-- > 0) 87 { 88 str = str + constant[rd.Next(60)]; 89 } 90 return str; 91 } 92 #endregion 93 94 95 } 96 }View Code
前臺的視圖代碼
1 <p>驗證碼: <input type="text" id="yzm" style=" width:100px"/> 2 <img title="看不清?" style=" cursor:pointer ;vertical-align:middle" width="70px" src="ValidateCode.aspx" onclick="this.src='ValidateCode.aspx?r='+Math.random()" /> 3 </p>
出錯更換的js代碼
1 $("#yzm").next().attr("src", "ValidateCode.aspx?r=" + Math.random());