主頁 > 知識庫 > asp.net驗證碼的簡單制作

asp.net驗證碼的簡單制作

熱門標簽:服務器配置 集中運營管理辦法 科大訊飛語音識別系統(tǒng) 阿里云 網(wǎng)站排名優(yōu)化 硅谷的囚徒呼叫中心 百度競價排名 地方門戶網(wǎng)站

實際上關(guān)于asp.net驗證碼制作的文章已經(jīng)很多很多了,但是今天還是要和大家繼續(xù)分享,親,可以綜合幾篇實例,編寫出適用于自己網(wǎng)站的ASP.NET驗證碼,大概也就兩大部分:

先建立一個asp.net窗體ValidateCode.aspx;不寫任何東西。直接在后臺ValidateCode.aspx.cs中寫如下代碼:

    protected void Page_Load(object sender, EventArgs e)
    {      
      string validateCode = CreateValidateCode();//生成驗證碼 
      Bitmap bitmap = new Bitmap(imgWidth,imgHeight);//生成Bitmap圖像 
      DisturbBitmap(bitmap); //圖像背景 
      DrewValidateCode(bitmap,validateCode);//繪制驗證碼圖像 
      bitmap.Save(Response.OutputStream,ImageFormat.Gif);//保存圖像,等待輸出 

    }

    private int codeLen = 4;//驗證碼長度 
    private int fineness = 85;//圖片清晰度 
    private int imgWidth = 48;//圖片寬度 
    private int imgHeight = 24;//圖片高度 
    private string fontFamily = "Times New Roman";//字體名稱 
    private int fontSize = 14;//字體大小 
    //private int fontStyle = 0;//字體樣式 
    private int posX = 0;//繪制起始坐標X 
    private int posY = 0;//繪制坐標Y 
    private string CreateValidateCode() //生成驗證碼 
    {
      string validateCode = "";
      Random random = new Random();// 隨機數(shù)對象 
      for (int i = 0; i  codeLen; i++)//循環(huán)生成每位數(shù)值 
      {
        int n = random.Next(10);//數(shù)字 
        validateCode += n.ToString();
      }
      Session["vcode"] = validateCode;//保存驗證碼 這Session是在前臺調(diào)用的。
      return validateCode;// 返回驗證碼 
    }

    private void DisturbBitmap(Bitmap bitmap)//圖像背景 
    {
      Random random = new Random();//通過隨機數(shù)生成 
      for (int i = 0; i  bitmap.Width; i++)//通過循環(huán)嵌套,逐個像素點生成 
      {
        for (int j = 0; j  bitmap.Height; j++)
        {
          if (random.Next(90) = this.fineness)
            bitmap.SetPixel(i, j, Color.LightGray);
        }
      }
    }
    private void DrewValidateCode(Bitmap bitmap, string validateCode)//繪制驗證碼圖像 
    {
      Graphics g = Graphics.FromImage(bitmap);//獲取繪制器對象 
      Font font = new Font(fontFamily, fontSize, FontStyle.Bold);//設(shè)置繪制字體 
      g.DrawString(validateCode, font, Brushes.Black, posX, posY);//繪制驗證碼圖像 
    } 

在Login.aspx窗體頁面中實現(xiàn)如下圖功能:

Login.aspx窗體前臺:

//這個函數(shù)是在點擊驗證碼圖片就會更換驗證碼
//可以使用微軟自帶的jqury.js 下面jquery-1.4.1.min.js版本之上的?;蛘咴趈query官網(wǎng)上下載就可以。
 script src="styles/jquery-1.4.1.min.js" type="text/javascript">/script>
     function f_refreshtype() {
       var Image1 = document.getElementByIdx_x_x_x("img");
       if (Image1 != null) {
         Image1.src = Image1.src + "?";
       }
     }
---img src="ValidateCode.aspx" id="img" onclick="f_refreshtype()" width="50px"/>//調(diào)用函數(shù),實現(xiàn)更換驗證碼

后臺代碼:點擊登錄驗證用戶是否輸入正確。 

  string usercode = txtcode.Text.Trim();
      if (usercode == Session["vcode"].ToString())//Session["vcode"]
      {
  }

其他代碼就是跟其他一樣。

以上就是跟大家分享的關(guān)于生成ASP.NET驗證碼的過程,希望大家可以學以致用。

您可能感興趣的文章:
  • 一個簡單的ASP.NET驗證碼
  • asp.net mvc驗證碼類使用
  • ASP.NET驗證碼實現(xiàn)(附源碼)
  • 12306動態(tài)驗證碼啟發(fā)之ASP.NET實現(xiàn)動態(tài)GIF驗證碼(附源碼)
  • ASP.NET驗證碼(3種)
  • asp.net 驗證碼生成和刷新及驗證
  • ASP.net 驗證碼實現(xiàn)代碼(C#)
  • asp.net(C#) 生成隨機驗證碼的代碼
  • Asp.net(C#)實現(xiàn)驗證碼功能代碼
  • ASP.NET ashx實現(xiàn)無刷新頁面生成驗證碼

標簽:烏蘭察布 甘孜 威海 西雙版納 隨州 梧州 開封 廣西

巨人網(wǎng)絡通訊聲明:本文標題《asp.net驗證碼的簡單制作》,本文關(guān)鍵詞  ;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問題,煩請?zhí)峁┫嚓P(guān)信息告之我們,我們將及時溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡,涉及言論、版權(quán)與本站無關(guān)。
  • 相關(guān)文章
  • 收縮
    • 微信客服
    • 微信二維碼
    • 電話咨詢

    • 400-1100-266