摘要:下載時所需輸入的驗證碼Validate Image
相信應該很多人在網路上下載或是註冊帳號……等之類的活動中,不難看到這個東西 XD
這也是小弟我忘了在哪個地方看到的!印象中也是某位大大的Blog上看到的!只做一點修改而以 XD
Default.aspx
01 <html xmlns="http://www.w3.org/1999/xhtml" >
02 <head runat="server">
03 <title>ValidateImage</title>
04 </head>
05 <body>
06 <form id="form1" runat="server">
07 <div>
08 <img src="ValidateImage.aspx" alt="Validate Code" border="1" /><br />
09 ValidateCode:
10 <asp:TextBox ID="TextBox1" runat="server" Style="position: static" Width="94px"></asp:TextBox>
11 <asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Style="position: static" Text="Validate" />
12 </div>
13 </form>
14 </body>
15 </html>
02 <head runat="server">
03 <title>ValidateImage</title>
04 </head>
05 <body>
06 <form id="form1" runat="server">
07 <div>
08 <img src="ValidateImage.aspx" alt="Validate Code" border="1" /><br />
09 ValidateCode:
10 <asp:TextBox ID="TextBox1" runat="server" Style="position: static" Width="94px"></asp:TextBox>
11 <asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Style="position: static" Text="Validate" />
12 </div>
13 </form>
14 </body>
15 </html>
Default.aspx.cs
01
public partial class _Default : System.Web.UI.Page
02
{
03
protected void Page_Load(object sender, EventArgs e)
04
{
05
06
}
07
08 protected void Button1_Click(object sender, EventArgs e)
09
{
10
//利用Session驗證
11
if (TextBox1.Text.ToUpper() == (string)Session["ValidateCode"])
12
{
13
Response.Write("OK");
14
}
15
else
16
{
17
Response.Write("ERROR");
18
}
19
20
21
//利用cookie驗證
22
if (String.Compare(Request.Cookies["ValidateCode"].Value, this.TextBox1.Text.ToUpper(), true) == 0)
23
{
24
Response.Write("正確");
25
}
26
else
27
{
28
Response.Write("錯誤");
29
}
30
31
}
32
33
}
public partial class _Default : System.Web.UI.Page 02
{03
protected void Page_Load(object sender, EventArgs e)04
{05

06
}07

08 protected void Button1_Click(object sender, EventArgs e)
09
{10
//利用Session驗證11
if (TextBox1.Text.ToUpper() == (string)Session["ValidateCode"])12
{13
Response.Write("OK");14
}15
else16
{17
Response.Write("ERROR");18
}19

20
21
//利用cookie驗證 22
if (String.Compare(Request.Cookies["ValidateCode"].Value, this.TextBox1.Text.ToUpper(), true) == 0) 23
{ 24
Response.Write("正確"); 25
} 26
else 27
{ 28
Response.Write("錯誤"); 29
} 30

31
}32

33
}
ValidateImage.aspx.cs
01 
using System.Drawing;
02
03
public partial class ValidateImage : System.Web.UI.Page
04
{
05
protected void Page_Load(object sender, EventArgs e)
06
{
07
//Validate Code
08
string[] Code ={ "A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M",
09
"N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z",
10
"0", "1", "2", "3", "4", "5", "6", "7", "8", "9" };
11
string strRd = string.Empty;
12
13 Random rd = new Random(unchecked((int)DateTime.Now.Ticks));
14
Bitmap Bmp = new Bitmap(80, 25); //建立實體圖檔並設定大小
15
Graphics Gpi = Graphics.FromImage(Bmp);
16
Font Font1 = new Font("Verdana", 14, FontStyle.Italic);
17
18 for (int i = 0; i < 5; i++) // 亂數產生驗證文字
19
{
20
strRd += Code[rd.Next(35)];
21
}
22
23 //Pen PenLine = new Pen(Brushes.Red, 1); //實體化筆刷並設定顏色、大小(畫X,Y軸用)
24
Gpi.Clear(Color.White); //設定背景顏色
25
for (int i = 1; i <= 10; i++)
26
{
27
Gpi.DrawLine(getPen(i), rd.Next(80), 0, rd.Next(80), 30); // ↘
28
}
29
Gpi.DrawString(strRd, Font1, Brushes.Black, 0, 0);
30
31 for (int i = 0; i <= 50; i++) //亂數產生霧點,擾亂機器人辨別
32
{
33
int RandPixelX = rd.Next(0, 80);
34
int RandPixelY = rd.Next(0, 25);
35
Bmp.SetPixel(RandPixelX, RandPixelY, Color.Blue);
36
}
37
38 Session["ValidateCode"] = strRd; //將驗證碼存入Session以便稍後進行驗證
39
40 HttpContext.Current.Response.Cookies.Add(new HttpCookie("ValidateCode", strRd)); //將驗證碼存入cookie以便稍後進行驗證
41
42 Bmp.Save(Response.OutputStream, System.Drawing.Imaging.ImageFormat.Gif);
43
}
44
45
getPen 取得筆刷的設定顏色
85
}

using System.Drawing;02

03
public partial class ValidateImage : System.Web.UI.Page04
{05
protected void Page_Load(object sender, EventArgs e)06
{07
//Validate Code 08
string[] Code ={ "A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", 09
"N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z", 10
"0", "1", "2", "3", "4", "5", "6", "7", "8", "9" };11
string strRd = string.Empty;12

13 Random rd = new Random(unchecked((int)DateTime.Now.Ticks));
14
Bitmap Bmp = new Bitmap(80, 25); //建立實體圖檔並設定大小 15
Graphics Gpi = Graphics.FromImage(Bmp);16
Font Font1 = new Font("Verdana", 14, FontStyle.Italic);17

18 for (int i = 0; i < 5; i++) // 亂數產生驗證文字
19
{20
strRd += Code[rd.Next(35)];21
}22

23 //Pen PenLine = new Pen(Brushes.Red, 1); //實體化筆刷並設定顏色、大小(畫X,Y軸用)
24
Gpi.Clear(Color.White); //設定背景顏色25
for (int i = 1; i <= 10; i++)26
{27
Gpi.DrawLine(getPen(i), rd.Next(80), 0, rd.Next(80), 30); // ↘28
}29
Gpi.DrawString(strRd, Font1, Brushes.Black, 0, 0);30

31 for (int i = 0; i <= 50; i++) //亂數產生霧點,擾亂機器人辨別
32
{33
int RandPixelX = rd.Next(0, 80);34
int RandPixelY = rd.Next(0, 25);35
Bmp.SetPixel(RandPixelX, RandPixelY, Color.Blue);36
}37

38 Session["ValidateCode"] = strRd; //將驗證碼存入Session以便稍後進行驗證
39

40 HttpContext.Current.Response.Cookies.Add(new HttpCookie("ValidateCode", strRd)); //將驗證碼存入cookie以便稍後進行驗證
41

42 Bmp.Save(Response.OutputStream, System.Drawing.Imaging.ImageFormat.Gif);
43
}44

45
getPen 取得筆刷的設定顏色85
}ps. ValidateImage.aspx裡完全沒東西,所以就沒附上程式碼!圖檔的輸出是在第42行!
