★33°空間‰


                                 ----★七彩服飾  【最潮拜☆日單精品】【Esprit】【Hotwind】滿150包郵-女裝-流行女裝    www.7color.hb.cn

                                 ----智力比知識重要,素質比智力重要,覺悟比素質更重要
          posts - 110,comments - 35,trackbacks - 0

          網上有很多關于驗證碼做法的資料,但是真正做得完整的卻非常少,所以昨天晚上參考了作者:CSDN 上kwklover 的 在ASP.NET下實現數字和字符相混合的驗證碼 (混合,完整,遺憾的是vb.net,說到這里我想我得感謝stone(QQ9337874),因為我在把VB譯成C#的過程中幾番出錯,最終這段翻譯由他完成)!!

          隨機漢字參考了
          http://navicy.cnblogs.com/archive/2005/05/08/150756.html 這篇文章的,

          而最終的純數字以及GDI+繪圖則是從□☆近三十☆(QQ19302038)傳給我的一個OA案例里面分離出來的!

          所以完整的說,這里面幾乎都是別人做好了的,我只是整理出來了!

          完整的下載地址是 
          http://www.cnblogs.com/Files/thcjp/gdi.rar  

          下面把代碼的重點部分貼出來,如果看源文件有什么不明白可以問偶?。眩眩保保埃担常担福埃?br />
          效果如圖(我確實懶了點,按紐的名字都沒有改,哈哈)
          需要說明的是,因為提交對比后,頁面是重新刷新了,所以文本框里面看見的和后面圖片顯示不一樣,這個不需要太多說明吧!!

          ?混合模式刷新出現約界錯誤 已經被 stone(QQ9337874) 修正!!


          下面是GDI+繪圖的代碼段,以及顯示的調用等
          using ?System;
          using
          ?System.Data;
          using
          ?System.Configuration;
          using
          ?System.Collections;
          using
          ?System.Web;
          using
          ?System.Web.Security;
          using
          ?System.Web.UI;
          using
          ?System.Web.UI.WebControls;
          using
          ?System.Web.UI.WebControls.WebParts;
          using
          ?System.Web.UI.HtmlControls;
          using
          ?System.Drawing.Drawing2D;
          using
          ?System.Drawing.Imaging;
          using
          ?System.Drawing.Text;
          using
          ?System.Drawing;
          using
          ?System.Text;?

          public ?partial? class
          ?png?:?System.Web.UI.Page
          {
          ????
          private ? readonly ? string ?ImagePath? = ? " Validator.jpg "
          ;
          ????
          private ? static ? string ?gif? = ? ""
          ;

          ????
          protected ? void ?Page_Load( object
          ?sender,?EventArgs?e)
          ????
          {

          ????????
          switch ?(Request.QueryString[ " aa "
          ])
          ????????
          {
          ????????????
          case ? " 1 "
          :
          ????????????????gif?
          =
          ?stxt();
          ????????????????Session[
          " gif " ] =
          ?stxt();
          ????????????????
          break
          ;
          ????????????
          case ? " 2 "
          :
          ????????????????gif?
          =
          ?GetRandomint();
          ?????????????????Session[
          " gif " ] =
          ?GetRandomint();
          ????????????????
          break
          ;
          ????????????
          case ? " 3 "
          :
          ????????????????gif?
          = ?RndNum( 3
          );
          ?????????????????Session[
          " gif " ] = ?RndNum( 3
          );
          ????????????????
          break
          ;
          ????????????
          default
          :
          ????????????????gif?
          = ?RndNum( 3
          );
          ?????????????????Session[
          " gif " ] =
          ?GetRandomint();
          ????????????????
          break
          ;
          ????????}

          ????????
          ????????
          /// 創建Bmp位圖
          ????????Bitmap?bitMapImage? = ? new
          ?System.Drawing.Bitmap(Server.MapPath(ImagePath));
          ????????Graphics?graphicImage?
          =
          ?Graphics.FromImage(bitMapImage);

          ????????
          /// 設置畫筆的輸出模式
          ????????graphicImage.SmoothingMode? =
          ?SmoothingMode.HighSpeed;
          ????????
          /// 添加文本字符串
          ????????graphicImage.DrawString(gif,? new ?Font( " Arial " ,? 20 ,?FontStyle.Bold),?SystemBrushes.WindowText,? new ?Point( 0 ,? 0
          ));

          ????????
          /// 設置圖像輸出的格式
          ????????Response.ContentType? = ? " image/jpeg "
          ;

          ????????
          /// 保存數據流
          ????????bitMapImage.Save(Response.OutputStream,?ImageFormat.Jpeg);

          ????????
          /// 釋放占用的資源
          ????????graphicImage.Dispose();
          ????????bitMapImage.Dispose();
          ????}

          返回純數字

          ???? private ?String?GetRandomint()
          ????
          {
          ????????Random?random?
          = ? new
          ?Random();
          ????????
          return ?(random.Next( 100000 ,? 999999
          ).ToString());
          ????}


          返回文字\數字\字母混合的

          public ? static ?String?RndNum( int ?VcodeNum)
          ????
          {
          ????????String?Vchar?
          = ? " 0,1,2,3,4,5,6,7,8,9,A,B,C,D,E,F,G,H,I,J,K,L,M,N,O,P,Q,R,S,T,U,W,X,Y,Z,呵,哈,彈,簧,秤,嬉,戲 "
          ;
          ????????String[]?VcArray?
          = ?Vchar.Split( ' , '
          );
          ????????String?VNum?
          = ? ""
          ;
          ????????Random?random?
          = ? new
          ?Random();
          ????????
          for ?( int ?i? = ? 1 ;?i? <= ?VcodeNum;?i ++
          )
          ????????
          {
          ????????????
          int ?iNum? = ? 0
          ;
          ????????????
          while ?((iNum? = ?Convert.ToInt32(VcArray.Length? * ?random.NextDouble()))? ==VcArray.Length ?
          )
          ????????????
          {
          ????????????????iNum?
          = ?Convert.ToInt32(VcArray.Length? *
          ?random.NextDouble());
          ????????????}

          ????????????VNum?
          += ?VcArray[iNum];
          ????????????
          // ?VNum?+=?VcArray[Convert.ToInt32(VcArray.Length*random.NextDouble())];

          ????????}

          ????????
          return ?VNum;
          ????}
          ?

          返回漢字的
          ??????? public ? static ? object []?CreateRegionCode( int ?strlength)?
          ????????
          {?
          ????????????
          // 定義一個字符串數組儲存漢字編碼的組成元素?

          ???????????? string []?rBase = new ?String?[ 16 ] { " 0 " , " 1 " , " 2 " , " 3 " , " 4 " , " 5 " , " 6 " , " 7 " , " 8 " , " 9 " , " a " , " b " , " c " , " d " , " e " , " f " } ;?
          ?????????????
          ????????????Random?rnd
          = new
          ?Random();?
          ?????????
          ????????????
          // 定義一個object數組用來?

          ???????????? object []?bytes = new ? object [strlength];?
          ?
          ????????????
          /**/
          /* 每循環一次產生一個含兩個元素的十六進制字節數組,并將其放入bject數組中?
          ?????????????每個漢字有四個區位碼組成?
          ?????????????區位碼第1位和區位碼第2位作為字節數組第一個元素?
          ?????????????區位碼第3位和區位碼第4位作為字節數組第二個元素?
          ????????????
          */
          ?
          ????????????
          for ( int ?i = 0 ;i < strlength;i ++
          )?
          ????????????
          {?
          ????????????????
          // 區位碼第1位?

          ???????????????? int ?r1 = rnd.Next( 11 , 14 );?
          ????????????????
          string ?str_r1 =
          rBase[r1].Trim();?
          ?
          ????????????????
          // 區位碼第2位?

          ????????????????rnd = new ?Random(r1 * unchecked (( int )DateTime.Now.Ticks) + i); // 更換隨機數發生器的種子避免產生重復值?
          ???????????????? int ?r2;?
          ????????????????
          if ?(r1 == 13
          )?
          ????????????????
          {?
          ????????????????????r2
          = rnd.Next( 0 , 7
          );?
          ????????????????}
          ?
          ????????????????
          else
          ?
          ????????????????
          {?
          ????????????????????r2
          = rnd.Next( 0 , 16
          );?
          ????????????????}
          ?
          ????????????????
          string ?str_r2 =
          rBase[r2].Trim();?
          ?
          ????????????????
          // 區位碼第3位?

          ????????????????rnd = new ?Random(r2 * unchecked (( int )DateTime.Now.Ticks) + i);?
          ????????????????
          int ?r3 = rnd.Next( 10 , 16
          );?
          ????????????????
          string ?str_r3 =
          rBase[r3].Trim();?
          ?
          ????????????????
          // 區位碼第4位?

          ????????????????rnd = new ?Random(r3 * unchecked (( int )DateTime.Now.Ticks) + i);?
          ????????????????
          int
          ?r4;?
          ????????????????
          if ?(r3 == 10
          )?
          ????????????????
          {?
          ????????????????????r4
          = rnd.Next( 1 , 16
          );?
          ????????????????}
          ?
          ????????????????
          else ? if ?(r3 == 15
          )?
          ????????????????
          {?
          ????????????????????r4
          = rnd.Next( 0 , 15
          );?
          ????????????????}
          ?
          ????????????????
          else
          ?
          ????????????????
          {?
          ????????????????????r4
          = rnd.Next( 0 , 16
          );?
          ????????????????}
          ?
          ????????????????
          string ?str_r4 =
          rBase[r4].Trim();?
          ?
          ????????????????
          // 定義兩個字節變量存儲產生的隨機漢字區位碼?

          ???????????????? byte ?byte1 = Convert.ToByte(str_r1? + ?str_r2, 16 );?
          ????????????????
          byte ?byte2 = Convert.ToByte(str_r3? + ?str_r4, 16
          );?
          ????????????????
          // 將兩個字節變量存儲在字節數組中?

          ???????????????? byte []?str_r = new ? byte [] {byte1,byte2} ;?
          ?
          ????????????????
          // 將產生的一個漢字的字節數組放入object數組中?

          ????????????????bytes.SetValue(str_r,i);?
          ?????????????????
          ????????????}
          ?
          ?
          ????????????
          return
          ?bytes;?
          ?
          ????????????}

          ????
          private ? string ?stxt()
          ????
          {
          ????????Encoding?gb?
          = ?Encoding.GetEncoding( " gb2312 "
          );

          ????????
          // 調用函數產生4個隨機中文漢字編碼?

          ???????? object []?bytes? = ?CreateRegionCode( 3 );

          ????????
          // 根據漢字編碼的字節數組解碼出中文漢字?

          ???????? string ?str1? = ?gb.GetString(( byte [])Convert.ChangeType(bytes[ 0 ],? typeof ( byte [])));
          ????????
          string ?str2? = ?gb.GetString(( byte [])Convert.ChangeType(bytes[ 1 ],? typeof ( byte
          [])));
          ????????
          string ?str3? = ?gb.GetString(( byte [])Convert.ChangeType(bytes[ 2 ],? typeof ( byte
          [])));

          ????????
          string ?txt? = ?str1? + ?str2? +
          ?str3;
          ????????
          return
          ?txt;
          ????}

          我們調用頁的代碼如下

          HTML代碼
          <% @?Page?Language = " C# " ?AutoEventWireup = " true " ?CodeFile = " view.aspx.cs " ?Inherits = " view " ? %>

          <! DOCTYPE?html?PUBLIC?"-//W3C//DTD?XHTML?1.0?Transitional//EN"?"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd" >

          < html? xmlns ="http://www.w3.org/1999/xhtml" ? >
          < head? runat ="server" >
          ????
          < title > 無標題頁 </ title >
          </ head >
          < body >
          ????
          < form? id ="form1" ?runat ="server" >
          ????
          < div >
          ????????
          &nbsp; < asp:TextBox? ID ="TextBox1" ?runat ="server" ></ asp:TextBox >
          ????????
          < asp:Image? ID ="Image1" ?runat ="server" ?ImageUrl ="png.aspx" ? />< br? />
          ????????
          < br? />
          ????????
          &nbsp; < asp:Button? ID ="Button2" ?runat ="server" ?OnClick ="Button2_Click" ?Text ="Button" ? />
          ????????
          < asp:DropDownList? ID ="DropDownList1" ?runat ="server" ?AutoPostBack ="True" ?OnSelectedIndexChanged ="DropDownList1_SelectedIndexChanged" >
          ????????????
          < asp:ListItem? Value ="3" > 默認 </ asp:ListItem >
          ????????????
          < asp:ListItem? Value ="1" > 文字 </ asp:ListItem >
          ????????????
          < asp:ListItem? Value ="2" > 數字 </ asp:ListItem >
          ????????????
          < asp:ListItem? Value ="3" > 混合 </ asp:ListItem >
          ????????
          </ asp:DropDownList ></ div >
          ????
          </ form >
          </ body >
          </ html >

          CS代碼
          ???? protected ? void ?DropDownList1_SelectedIndexChanged( object ?sender,?EventArgs?e)
          ????
          {
          ????????
          switch
          ?(DropDownList1.SelectedValue)
          ????????
          {
          ????????????
          case ? " 1 "
          :
          ????????????????Image1.ImageUrl?
          = ? " png.aspx?aa=1 "
          ;
          ????????????????
          break
          ;
          ????????????
          case ? " 2 "
          :
          ????????????????Image1.ImageUrl?
          = ? " png.aspx?aa=2 "
          ;
          ????????????????
          break
          ;
          ????????????
          case ? " 3 "
          :
          ????????????????Image1.ImageUrl?
          = ? " png.aspx?aa=3 "
          ;
          ????????????????
          break
          ;
          ????????}

          ????}

          ????
          protected ? void ?Button2_Click( object ?sender,?EventArgs?e)
          ????
          {
          ????????
          if ?(TextBox1.Text? == ?Session[ " gif "
          ].ToString())
          ????????????Response.Write(
          " OK,正確 "
          );
          ????????
          else

          ????????????Response.Write(
          " 驗證碼不符合 " );
          ????}
          posted on 2006-09-20 12:50 圣域飛俠 閱讀(534) 評論(0)  編輯  收藏 所屬分類: C#文章
          主站蜘蛛池模板: 南昌县| 万山特区| 思茅市| 宜阳县| 富民县| 济南市| 老河口市| 上蔡县| 潍坊市| 寿宁县| 富民县| 亚东县| 嘉祥县| 镇赉县| 和平区| 湘阴县| 化德县| 阿瓦提县| 康马县| 江阴市| 航空| 西峡县| 聂荣县| 扎兰屯市| 通海县| 河东区| 林口县| 莱芜市| 湘潭市| 黄平县| 千阳县| 库车县| 哈密市| 浦城县| 西林县| 南漳县| 平邑县| 镇赉县| 黔西| 四川省| 延津县|