国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學(xué)院 > 開發(fā)設(shè)計(jì) > 正文

ASP.NET圖片驗(yàn)證碼學(xué)習(xí)!

2019-11-17 01:29:31
字體:
供稿:網(wǎng)友

asp.net圖片驗(yàn)證碼學(xué)習(xí)!

1. 新建一個(gè)Validate.aspx,然后在Validate.aspx.cs編寫代碼:

using System;using System.Collections;using System.ComponentModel;using System.Data;using System.Web;using System.Drawing;using System.Web.sessionState;using System.Web.UI;using System.Web.UI.WebControls;using System.Web.UI.HtmlControls;using System.Drawing.Imaging;using System.IO;

public partial class Validate : System.Web.UI.Page{   PRotected void Page_Load(object sender, EventArgs e)   {   this.CreateCheckCodeImage(RndNum());

  }   private string RndNum()   {   int number;   char code;   string checkCode = String.Empty;

   System.Random random = new Random();

   for (int i = 0; i < 4; i++)   {   number = random.Next();   if (number % 2 == 0)   code = (char)('0' + (char)(number % 10));   else   code = (char)('A' + (char)(number % 26));   checkCode += code.ToString();   }

  Session["CheckCode"] = checkCode;   return checkCode;   }   private void CreateCheckCodeImage(string checkCode)   {   if (checkCode == null || checkCode.Trim() == String.Empty)   return;   System.Drawing.Bitmap image = new System.Drawing.Bitmap((int)Math.Ceiling((checkCode.Length * 12.5)), 22);   Graphics g = Graphics.FromImage(image);   try   {   // 生成隨機(jī)生成器   Random random = new Random();   // 清空?qǐng)D片背景色   g.Clear(Color.White);   // 畫圖片的背景噪音線   for (int i = 0; i < 25; i++)   {   int x1 = random.Next(image.Width);   int x2 = random.Next(image.Width);   int y1 = random.Next(image.Height);         int y2 = random.Next(image.Height);         g.DrawLine(new Pen(Color.Silver), x1, y1, x2, y2);       }

      Font font = new System.Drawing.Font("Arial", 12, (System.Drawing.FontStyle.Bold | System.Drawing.FontStyle.Italic));       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);       g.DrawString(checkCode, font, brush, 2, 2);   // 畫圖片的前景噪音點(diǎn)   for (int i = 0; i < 100; i++)   {   int x = random.Next(image.Width);   int y = random.Next(image.Height);         image.SetPixel(x, y, Color.FromArgb(random.Next()));       }       //畫圖片的邊框線       g.DrawRectangle(new Pen(Color.Silver), 0, 0, image.Width - 1, image.Height - 1);       System.IO.MemoryStream ms = new System.IO.MemoryStream();       image.Save(ms, System.Drawing.Imaging.ImageFormat.Gif);       Response.ClearContent();       Response.ContentType = "image/Gif";       Response.BinaryWrite(ms.ToArray());     }     finally     {       g.Dispose();       image.Dispose();     }   }

}

2. 在前臺(tái)你想添加的頁面添加這段代碼:(我這里假設(shè)是在登錄頁面Login.aspx中添加圖片驗(yàn)證碼)

<img id="ImageCode" src="../Validate.aspx" style="cursor:pointer" onmouseup="RefreshImage()" alt="點(diǎn)擊重刷新"/>

還需要在<head>...</head>之間添加這段代碼:

<script language ="javascript" type="text/Javascript" >   function RefreshImage()

{     var img = document.getElementById("ImageCode"); // 這里的ImageCode就是上面你取圖片的Id名字,這里要一致!     img.src = img.src + '?';   } </script>

3. 在后臺(tái)Login.aspx.cs中添加實(shí)現(xiàn)代碼:

引用......

public partial class Login : System.Web.UI.Page{   protected void Page_Load(object sender, EventArgs e)   {

  }

  // 登錄按鈕點(diǎn)擊事件處理   protected void btnLogin_Click(object sender, ImageClickEventArgs e)   {     User user = new User();     user.LoginId = this.txt_LoginId.Text.Trim();     user.LoginPwd = this.txt_LoginPwd.Text.Trim();     if (this.txtCode.Text.ToLower() == Session["CheckCode"].ToString().ToLower())     {       Response.Redirect("Index.aspx");

      else       {         Session["user"] = null;         this.Page.ClientScript.RegisterStartupScript(this.GetType(), "str", "<script>alert(/"登錄失敗!!/")</script>");       }     }     else     {       this.Page.ClientScript.RegisterStartupScript(this.GetType(), "str", "<script>alert(/"驗(yàn)證碼錯(cuò)誤!!/")</script>");     }

  }

  // 新用戶注冊(cè)按鈕點(diǎn)擊事件處理   protected void btn_Register_Click(object sender, ImageClickEventArgs e)   {     Response.Redirect("Register.aspx");   }}

最后運(yùn)行出來的驗(yàn)證碼就是這個(gè)樣子的~~

當(dāng)然如果你想實(shí)現(xiàn)在其他頁面上代碼會(huì)有些小的變動(dòng),但是基本代碼就這些~~

Thanks~~


發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 昔阳县| 类乌齐县| 玛纳斯县| 来安县| 师宗县| 若尔盖县| 平阳县| 咸丰县| 泽州县| 绍兴市| 苗栗县| 新田县| 兴业县| 康定县| 富顺县| 闸北区| 大荔县| 延边| 札达县| 登封市| 广东省| 习水县| 云梦县| 鄂托克旗| 东丽区| 封开县| 峡江县| 弥勒县| 盖州市| 寻乌县| 崇义县| 岳西县| 华安县| 勃利县| 昌黎县| 宝应县| 吉木萨尔县| 塔城市| 黎平县| 娄底市| 大城县|