ASP.NET 2.0 HttpHandler實現產生圖片驗證碼(範例程式碼下載) __.net

來源:互聯網
上載者:User
學習整理了一下 (一).功能         用HttpHandler實現圖片驗證碼         (二).代碼如下    1. 處理常式檔案 ValidateImageHandler.ashx代碼如下    1  <% @ WebHandler Language = " C# "  Class = " ValidateImageHandler "   %>
 2 
 3  using  System;
 4  using  System.Web;
 5  using  System.Web.SessionState;
 6  using  System.Drawing;
 7  using  System.Drawing.Imaging;
 8  using  System.Text;
 9 
10  ///   <summary>
11  ///  ValidateImageHandler 產生網站驗證碼功能
12  ///   </summary>
13  public   class  ValidateImageHandler : IHttpHandler, IRequiresSessionState
14  {
15       int  intLength  =   5 ;                // 長度
16       string  strIdentify  =   " Identify " ;  // 隨機字串儲存索引值,以便儲存到Session中
17       public  ValidateImageHandler()
18      {        
19      }
20 
21       ///   <summary>
22       ///   產生驗證圖片核心代碼
23       ///   </summary>
24       ///   <param name="hc"></param>
25       public   void  ProcessRequest(HttpContext hc)
26      {
27           // 設定輸出資料流圖片格式
28          hc.Response.ContentType  =   " image/gif " ;
29          
30          Bitmap b  =   new  Bitmap( 200 ,  60 );
31          Graphics g  =  Graphics.FromImage(b);
32          g.FillRectangle( new  SolidBrush(Color.YellowGreen),  0 ,  0 ,  200 ,  60 );
33          Font font  =   new  Font(FontFamily.GenericSerif,  48 , FontStyle.Bold, GraphicsUnit.Pixel);
34          Random r  =   new  Random();
35 
36           // 合法隨機顯示字元列表
37           string  strLetters  =   " abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890 " ;
38          StringBuilder s  =   new  StringBuilder();
39          
40           // 將隨機產生的字串繪製到圖片上
41           for  ( int  i  =   0 ; i  <  intLength; i ++ )
42          {
43              s.Append(strLetters.Substring(r.Next( 0 , strLetters.Length  -   1 ),  1 ));
44              g.DrawString(s[s.Length  -   1 ].ToString(), font,  new  SolidBrush(Color.Blue), i  *   38 , r.Next( 0 ,  15 ));
45          }
46 
47           // 產生幹擾線條
48          Pen pen  =   new  Pen( new  SolidBrush(Color.Blue),  2 );
49           for  ( int  i  =   0 ; i  <   10 ; i ++ )
50          {
51              g.DrawLine(pen,  new  Point(r.Next( 0 ,  199 ), r.Next( 0 ,  59 )),  new  Point(r.Next( 0 ,  199 ), r.Next( 0 ,  59 )));
52          }
53          b.Save(hc.Response.OutputStream, ImageFormat.Gif);
54          hc.Session[strIdentify]  =  s.ToString();  // 先儲存在Session中,驗證與使用者輸入是否一致
55          hc.Response.End();
56     
57      }
58      
59       ///   <summary>
60       ///  表示此類執行個體是否可以被多個請求共用(重用可以提高效能)
61       ///   </summary>
62       public   bool  IsReusable
63      {
64           get
65          {
66               return   true ;
67          }
68      }
69  }
70 

 2. 前台頁面代碼  1  < asp:Login ID = " Login1 "  runat = " server "  BackColor = " #EFF3FB "  BorderColor = " #B5C7DE "  BorderPadding = " 4 "  BorderStyle = " Solid "  BorderWidth = " 1px "  Font - Names = " Verdana "  Font - Size = " 0.8em "  ForeColor = " #333333 "  OnAuthenticate = " Login1_Authenticate " >
 2               < TitleTextStyle BackColor = " #507CD1 "  Font - Bold = " True "  Font - Size = " 0.9em "  ForeColor = " White "   />
 3               < InstructionTextStyle Font - Italic = " True "  ForeColor = " Black "   />
 4               < TextBoxStyle Font - Size = " 0.8em "   />
 5               < LoginButtonStyle BackColor = " White "  BorderColor = " #507CD1 "  BorderStyle = " Solid "  BorderWidth = " 1px "
 6                  Font - Names = " Verdana "  Font - Size = " 0.8em "  ForeColor = " #284E98 "   />
 7          &nbs

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.