一直要把這個整理出來,該好今天有廠商需要這段,就順便把說明打一打,分享出來這段程式碼
流程如下
產生驗證圖片的 CheckImageCode.aspx 

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="CheckImageCode.aspx.cs" Inherits="CheckImageCode" %>

<!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>

</div>

</form>

</body>

</html>

CheckImageCode.cs

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;

public partial class CheckImageCode : System.Web.UI.Page

{

protected void Page_Load(object sender, EventArgs e)

    {

this.CreateCheckCodeImage(GenerateCheckCode());

    }

private string GenerateCheckCode()

    {

int number;

char code;

string checkCode = String.Empty;

        System.Random random = new Random();

//要製造出幾個驗證碼

for (int i = 0; i < 4; i++)

        {

            number = random.Next();

//亂數決定哪一個是數字或字母

if (number % 2 == 0)

                code = (char)('0' + (char)(number % 10));

else

                code = (char)('A' + (char)(number % 26));

            checkCode += code.ToString();

        }

//寫入Cook

        Response.Cookies.Add(new HttpCookie("CheckCode", checkCode));

return checkCode;

    }

}

使用在投票頁面 投票頁面.aspx
<tr>

<td width="40%" height="20" valign="middle"><div align="right" ><font color="#FF0066">請輸入檢核文字:</font></div></td>

<td width="26%" valign="bottom">

<asp:TextBox ID="txtCheckCode" runat="server" MaxLength="8" Width="80%" AutoCompleteType="Disabled"></asp:TextBox></td>

<td width="19%"valign="bottom">

<img src="CheckImageCode.aspx" height="20"/> </td>

<td width="20%" valign="bottom">

<asp:Button ID="btnSend" runat="server" OnClick="btnSend_Click" Text="送出資料" /></td>

</tr>

投票頁面.cs   要加入的流程

// 驗證碼是否能夠寫入cookie內

if (Request.Cookies["CheckCode"] == null)

                {

                    lblMessage.Text = "您的瀏覽器設定禁止使用 Cookies,您必須設定瀏覽器允許使用 Cookies 選項後才能參與投票。";

                    lblMessage.Visible = true;

return;

                }

//檢查驗證碼是否打的正確

if (String.Compare(Request.Cookies["CheckCode"].Value, txtCheckCode.Text, true) != 0)

                {

                    lblMessage.Text = "圖片驗證碼錯誤";

                    lblMessage.Visible = true;

                }

else

                {

//.寫入DB 的相關動作

                }

                //清除目前輸入
                txtCheckCode.Text = “”;

最後就能產生出
image
至於要調字的顏色、大小、變形字,檢驗字八碼,都可以在註解中找到要修改的地方,就分享給大家試試看。

 

2008/6/4 補上完整專案,有興趣的朋友,就直接下載下去研究。
CaptchaPractice.rar

posted on 2008/3/26 18:55 | 閱讀數 : 6188 | 分類 [ ASP.NET ] | 訂閱
 

回覆

# re: Captcha 安全碼[ASP.NET] 產生驗證圖片字
Posted by 路人甲
on 2008/3/31 下午 06:19
CreateCheckCodeImage沒有這個函數
# re: Captcha 安全碼[ASP.NET] 產生驗證圖片字
Posted by dotjum
on 2008/3/31 下午 07:21

抱歉,原來以前就忘記貼上,我補上一下
 

  private void CreateCheckCodeImage(string checkCode)
    {
        if (checkCode == null || checkCode.Trim() == String.Empty)
            return;

       System.Drawing.Bitmap image = new System.Drawing.Bitmap((int)Math.Ceiling((checkCode.Length * 12.5)), 22);


        Graphics g = Graphics.FromImage(image);

        try
        {
            //生成隨機生成器
            Random random = new Random();

            //清空圖片背景色
            g.Clear(Color.White);

            //畫圖片的背景噪音線
            for (int i = 0; i < 25; i++)
            {
                int x1 = random.Next(image.Width);
                int x2 = random.Next(image.Width);
                int y1 = random.Next(image.Height);
                int y2 = random.Next(image.Height);

                g.DrawLine(new Pen(Color.Silver), x1, y1, x2, y2);
            }


            Font font = new System.Drawing.Font("Arial", 22, (System.Drawing.FontStyle.Bold | System.Drawing.FontStyle.Italic));
            System.Drawing.Drawing2D.LinearGradientBrush brush = new System.Drawing.Drawing2D.LinearGradientBrush(new Rectangle(0, 0, image.Width, image.Height), Color.Blue, Color.DarkRed, 1.2f, true);
            //g.DrawString(checkCode, font, brush, 2, 2);
            g.DrawString(checkCode, font, brush, 2, 2);

            //畫圖片的前景噪音點
            for (int i = 0; i < 500; i++)
            {
                int x = random.Next(image.Width);
                int y = random.Next(image.Height);

                image.SetPixel(x, y, Color.FromArgb(random.Next()));
            }


            //畫圖片的邊框線
            g.DrawRectangle(new Pen(Color.Silver), 0, 0, image.Width - 1, image.Height - 1);

            System.IO.MemoryStream ms = new System.IO.MemoryStream();
            image.Save(ms, System.Drawing.Imaging.ImageFormat.Gif);
            Response.ClearContent();
            Response.ContentType = "image/Gif";
            Response.BinaryWrite(ms.ToArray());
        }

        finally
        {
            g.Dispose();
            image.Dispose();
        }

    }
# re: Captcha 安全碼[ASP.NET] 產生驗證圖片字
Posted by 路人
on 2008/6/4 下午 12:54
檔案 '/CheckImageCode.aspx.cs' 不存在。
# re: Captcha 安全碼[ASP.NET] 產生驗證圖片字
Posted by dotjum
on 2008/6/4 下午 10:46

to 路人:
已補上完整專案檔案下載,可以下載下去跑看看。

# re: Captcha 安全碼[ASP.NET] 產生驗證圖片字
Posted by cerdelia
on 2008/6/6 上午 10:24

不好意思~~~完整專案檔案下載 不能下載耶?

# re: Captcha 安全碼[ASP.NET] 產生驗證圖片字
Posted by dotjum
on 2008/6/6 上午 11:11

to cerdelia:
不好意思,路徑沒設好,已經可以正常下載。

# re: Captcha 安全碼[ASP.NET] 產生驗證圖片字
Posted by egg
on 2008/7/2 下午 06:35
感謝大大分享
# re: Captcha 安全碼[ASP.NET] 產生驗證圖片字
Posted by leeway
on 2008/7/15 下午 02:44
有辦法讓字體稍微扭曲變形嗎?
因為似乎有專門辨識識別碼的機器人程式
另外我好多地方看不太懂
可以指教嗎?
Font font = new System.Drawing.Font("Arial", 22, (System.Drawing.FontStyle.Bold | System.Drawing.FontStyle.Italic));
System.Drawing.Drawing2D.LinearGradientBrush brush = new System.Drawing.Drawing2D.LinearGradientBrush(new Rectangle(0, 0, image.Width, image.Height), Color.Blue, Color.DarkRed, 1.2f, true);

g.DrawString(checkCode, font, brush, 2, 2);

這幾行的公用還有裡面的參數作用?
System.IO.MemoryStream ms = new System.IO.MemoryStream();
image.Save(ms, System.Drawing.Imaging.ImageFormat.Gif);
Response.ClearContent();
Response.ContentType = "image/Gif";
Response.BinaryWrite(ms.ToArray());
這幾行的IO是用來存圖的嗎?
圖片存到哪裡了呢?

finally
{
g.Dispose();
image.Dispose();
}

最後的finally是要用來做什麼?
# re: Captcha 安全碼[ASP.NET] 產生驗證圖片字
Posted by williams
on 2008/7/15 下午 03:03

感謝大鈞大的分享 :)

# re: Captcha 安全碼[ASP.NET] 產生驗證圖片字
Posted by dotjum
on 2008/7/19 上午 11:53

to leeway :
圖片是存在在記憶體,再輸出到前端的頁面,沒有存在實體檔案。System.Drawing 是.NET 畫圖的物件NameSpace,詳細的可以參考
MSDN library
http://msdn.microsoft.com/zh-tw/library/system.drawing(VS.80).aspx
我這邊的字體有放到加粗體,因為之前作給客戶,客戶說看不清楚,
才加租體的,但這樣反而增加辨識軟體辨識更輕鬆,你可以把這個拿掉或透過 Captcha 關鍵字找網路上前輩門已經做好的扭曲字的版本。
 

# re: Captcha 安全碼[ASP.NET] 產生驗證圖片字
Posted by Tom
on 2008/8/14 下午 12:33
if (number % 2 == 0){
code = (char)('0' + (char)(number % 10));
}
else {
code = (char)('A' + (char)(number % 26));
}

How can I add small capital letter 'a'?
# re: Captcha 安全碼[ASP.NET] 產生驗證圖片字
Posted by Samuel
on 2008/8/30 上午 08:09
很棒的分享,不過是把驗證碼寫入cookie,那有心人只要讀了cookie的的資料,再輸入不就破解了嗎?
# re: Captcha 安全碼[ASP.NET] 產生驗證圖片字
Posted by dotjum
on 2008/9/2 下午 06:04

to Samuel :
謝謝你的回應,這個範例在儲存對應碼方面沒有加密,
之後在寫一篇我目前用的加密cookie加上圖片驗證的。

# re: Captcha 安全碼[ASP.NET] 產生驗證圖片字
Posted by 試試看
on 2009/3/10 上午 09:24
我在 VS2005 開發環境測試時,圖片會出現.
但是佈署至伺服器後, IE7 出現一個"X"的圖, Firefox3.0.7圖片沒出現.
請問佈署時,還需注意哪些條件?
Title *
Name *
Email
Url
Comment *  
 
 
登入後使用進階評論
 
Please add 3 and 8 and type the answer here: