登錄界面的驗證碼


首先在新建的登錄頁面添加驗證碼相關控件

 

<div class="row">

                    <input class="login-code" id="vcode" name="vcode" type="text" placeholder="*驗證碼:"

                        runat="server" />

                    <div class="code">

                        <img src="ValidateCode.aspx" id="img" alt="看不清楚,換一張?" width="50px" style="cursor: pointer;"

                            onclick="this.src=this.src+'?'" />

                    </div>

                </div>

 

(上面的 <img src="ValidateCode.aspx" id="img" alt="看不清楚,換一張?" width="50px" style="cursor: pointer;"

                            onclick="this.src=this.src+'?'" />

用於刷新驗證碼)

 

 

第二、在後臺代碼裏

 string code = vcode.Value.ToString();//輸入的驗證碼

        string scode = Session["vcode"].ToString();//獲取驗證碼

        if (code != scode)

        {

            MessageBox.Show(this, "請輸入正確的驗證碼!");

        }

第三、新建一個頁面ValidateCode.aspx

 

第三、後臺代碼

using System;

using System.Collections;

using System.Configuration;

using System.Data;

using System.Linq;

using System.Web;

using System.Web.Security;

using System.Web.UI;

using System.Web.UI.HtmlControls;

using System.Web.UI.WebControls;

using System.Web.UI.WebControls.WebParts;

using System.Xml.Linq;

using System.Drawing;//圖形

using System.Drawing.Imaging;

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

{

    protected void Page_Load(object sender, EventArgs e)

    {

        string validateCode = CreateValidateCode();//生成驗證碼

        Bitmap bitmap = new Bitmap(imgWidth, imgHeight);//生成BITMAP圖像

        DisturbBitmap(bitmap);//圖像背景

        DrewValidateCode(bitmap, validateCode);//繪製驗證碼圖像

        bitmap.Save(Response.OutputStream, ImageFormat.Gif);//保存圖像,等待輸出

    }

    private int codeLen = 4;//驗證碼長度

    private int fineness = 85;//圖片清晰度

    private int imgWidth = 48;//圖片寬度

    private int imgHeight = 24;//圖片高度

    private string fontFamily = "Times New Roman";//字體名稱

    private int fontSize = 14;//字體大小

    private int posX = 0;

    private int posY = 0;

    private string CreateValidateCode()//生成驗證碼

    {

        string validateCode = "";

        Random random = new Random();//隨機數對象

        for (int i = 0; i < codeLen; i++)//循環生成每位數值

        {

            int n = random.Next(10);

            validateCode += n.ToString();

        }

        Session["vcode"] = validateCode;//保存代碼,前臺調用

        return validateCode;//返回驗證碼

    }

    private void DisturbBitmap(Bitmap bitmap)//圖像背景

    {

        Random random = new Random();//通過隨機數生成

        for (int i = 0; i < bitmap.Width; i++)//通過循環嵌套,逐個像素點生成

        {

            for (int j = 0; j < bitmap.Height; j++)

            {

                if (random.Next(90) <= this.fineness)

                    bitmap.SetPixel(i, j, Color.LightGray);

            }

        }

    }

    private void DrewValidateCode(Bitmap bitmap, string validateCode)//繪製驗證碼圖像

    {

        Graphics g = Graphics.FromImage(bitmap);//獲取繪製器對象

        Font font = new Font(fontFamily, fontSize, FontStyle.Bold);//設置繪製字體

        g.DrawString(validateCode, font, Brushes.Black, posX, posY);//繪製驗證碼圖像

    }

 

}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章