asp.net驗證碼應用

weixin_33858249發表於2017-11-07

很久沒在自己部落格上更新,今天突然想寫一寫。最近忙於學習ASP.NET,學習有一段時間了,想寫一些我們比較常用的東西,和大家分享一下。

asp.net驗證碼應用

下面這個就是動態驗證碼的生成,如下圖

 

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 checkcode : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        CreateCheckCodeImage(GenerateCheckCode());//輸出圖片驗證碼
    }

    private string GenerateCheckCode()
    {
        int number;        //定義一個整數型別的變數number
        char code;        //定義一個字元型別的變數code
        string checkCode = String.Empty;//定義一個變數,儲存驗證碼字串
        System.Random random = new Random();//例項化Random類物件

        for (int i = 0; i < 4; i++)
        {
            number = random.Next();//呼叫Next方法返回一個隨機數
            //通過判斷生成的隨機數的奇偶性確定驗證碼字串中的某一位是數字還是字母
            if (number % 2 == 0)
                code = (char)('0' + (char)(number % 10));
            else
                code = (char)('A' + (char)(number % 26));

            checkCode += code.ToString();//組合新的驗證碼字串
        }
        //使用Cookie記錄生成的驗證碼字串
        Response.Cookies.Add(new HttpCookie("CheckCode", checkCode));
        return checkCode;
    }

    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 < 2; 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.Black), x1, y1, x2, y2);
            }
            Font font = new System.Drawing.Font("Arial", 12, (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);
            //畫圖片的前景噪音點
            for (int i = 0; i < 100; 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();
        }
    }
}

 


本文轉自gauyanm 51CTO部落格,原文連結:http://blog.51cto.com/gauyanm/412275,如需轉載請自行聯絡原作者

相關文章