.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;
using System.IO;


public partial class yanzhma : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        CheckCodeImage(CheckCode());
    }
    //隨機生成驗證碼字符串
    private string CheckCode() {
        int number;
        string strCode = string.Empty;
        //隨機種子
        Random random = new Random();
        for (int i = 0; i < 4;i++ ) {//四位校驗碼
            number = random.Next();
            //字符從0-9,A—Z中隨機產生,對應的AscII碼爲48-57,65-90
            number = number % 36;
            if (number < 10)
            {
                number += 48;


            }
            else {
                number += 55;
            
            }
            strCode += ((char)number).ToString();
        
        
        }
    //在Cookie中保存校驗碼
        Response.Cookies.Add(new HttpCookie("CheckCode", strCode));
        return strCode;
    
    }
    //根據校驗碼輸出圖片
    private void CheckCodeImage(string checkCode) {
    
    //若校驗碼爲空,直接返回
        if(checkCode==null || checkCode.Trim()==string .Empty){
            return;
        }
        //根據校驗碼輸出圖片長度
        System.Drawing.Bitmap image = new System.Drawing.Bitmap((int)Math.Ceiling((double)(checkCode.Length * 15)), 20);
        //創建Graphics對象,繪製圖片
        Graphics g = Graphics.FromImage(image);
        try
        {
            Random random = new Random();
            //清空背景顏色
            g.Clear(Color.White);
            //繪製圖片的噪音線10條
            for (int i = 0; i < 10; i++)
            {
                //噪音線的起點座標(x1,y1)終點座標(x2,y2)
                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 Font("Arial", 12, FontStyle.Italic);
            //線性變換畫刷
            Brush brush = new SolidBrush(Color.Chocolate);
            g.DrawString(checkCode, font,brush, 2, 2);
            //繪製圖片的噪音點
            for (int i = 0; i < 50; 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.SaddleBrown), 0, 0, image.Width - 1, image.Height - 1);
            //創建內流輸出圖片
            using (MemoryStream ms = new MemoryStream())
            {
                image.Save(ms,System.Drawing.Imaging.ImageFormat.Png);//保存圖片格式爲png
                Response.ClearContent();
                Response.ContentType = "image/Png";
                //輸出二進制流
                Response.BinaryWrite(ms.ToArray());


            }
        }
        catch (Exception oe)
        {
            Response.Write(oe.ToString());
        }
        finally {
            g.Dispose();
            image.Dispose();
        
        }


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