程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> .NET網頁編程 >> C# >> 關於C# >> ASP.NET HttpHandler實現生成圖片驗證碼(示例代碼下載)

ASP.NET HttpHandler實現生成圖片驗證碼(示例代碼下載)

編輯:關於C#
 

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 <LayoutTemplate>
8 <table border="0" cellpadding="4" cellspacing="0" style="border-collapse: collapse">
9 <tr>
10 <td style="width: 292px">
11 <table border="0" cellpadding="0">
12 <tr>
13 <td align="center" colspan="2" style="font-weight: bold; font-size: 0.9em; color: white;
14 background-color: #507cd1">
15 登錄</td>
16 </tr>
17 <tr>
18 <td align="left" style="width: 84px; height: 31px;">
19 <asp:Label ID="UserNameLabel" runat="server" AssociatedControlID="UserName">用戶名:</asp:Label></td>
20 <td style="height: 31px; width: 215px;">
21 <asp:TextBox ID="UserName" runat="server" Font-Size="0.8em" Width="113px"></asp:TextBox>
22 <asp:RequiredFieldValidator ID="UserNameRequired" runat="server" ControlToValidate="UserName"
23 ErrorMessage="必須填寫“用戶名”。" ToolTip="必須填寫“用戶名”。" ValidationGroup="Login1">*</asp:RequiredFieldValidator>
24 </td>
25 </tr>
26 <tr>
27 <td align="left" style="width: 84px">

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved