国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > .NET > 正文

asp.net(C#) 生成隨機(jī)驗(yàn)證碼的代碼

2020-01-18 01:41:47
字體:
供稿:網(wǎng)友
常用的生成驗(yàn)證碼程序 ,圖片效果如下:
.  .  .
源程序如下:
復(fù)制代碼 代碼如下:

using System; 
using System.IO; 
using System.Drawing; 
using System.Drawing.Imaging; 
using System.Text; 
using System.Collections; 
using System.Web; 
using System.Web.UI; 
using System.Web.UI.WebControls; 
using System.Web.UI.WebControls.WebParts; 
using System.Web.UI.HtmlControls; 
/**//// <summary> 
///  
/// ** asp.net(C#) 生成驗(yàn)證碼 ** 
///  
///  File: GenerateCheckCode.aspx.cs 
///  
///  Author: 周振興 (Zxjay 飄遙) 
///  
///  E-Mail: tda7264@163.com 
///  
///  Date: 07-04-10 
///  
/// </summary> 
public partial class GenerateCheckCode : System.Web.UI.Page 
...{ 
    protected void Page_Load(object sender, EventArgs e) 
    ...{ 
        string chkCode = string.Empty; 
        //顏色列表,用于驗(yàn)證碼、噪線、噪點(diǎn) 
        Color[] color =...{ Color.Black, Color.Red, Color.Blue, Color.Green, Color.Orange, Color.Brown, Color.Brown, Color.DarkBlue }; 
        //字體列表,用于驗(yàn)證碼 
        string[] font =...{ "Times New Roman", "MS Mincho", "Book Antiqua", "Gungsuh", "PMingLiU", "Impact" }; 
        //驗(yàn)證碼的字符集,去掉了一些容易混淆的字符 
        char[] character =...{ '2', '3', '4', '5', '6', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'J', 'K', 'L', 'M', 'N', 'P', 'R', 'S', 'T', 'W', 'X', 'Y' }; 
        Random rnd = new Random(); 
        //生成驗(yàn)證碼字符串 
        for (int i = 0; i < 4; i++) 
        ...{ 
            chkCode += character[rnd.Next(character.Length)]; 
        } 
        Bitmap bmp = new Bitmap(100, 40); 
        Graphics g = Graphics.FromImage(bmp); 
        g.Clear(Color.White); 
        //畫噪線 
        for (int i = 0; i < 10; i++) 
        ...{ 
            int x1 = rnd.Next(100); 
            int y1 = rnd.Next(40); 
            int x2 = rnd.Next(100); 
            int y2 = rnd.Next(40); 
            Color clr = color[rnd.Next(color.Length)]; 
            g.DrawLine(new Pen(clr), x1, y1, x2, y2); 
        } 
        //畫驗(yàn)證碼字符串 
        for (int i = 0; i < chkCode.Length; i++) 
        ...{ 
            string fnt = font[rnd.Next(font.Length)]; 
            Font ft = new Font(fnt, 18); 
            Color clr = color[rnd.Next(color.Length)]; 
            g.DrawString(chkCode[i].ToString(), ft, new SolidBrush(clr), (float)i * 20 + 8, (float)8); 
        } 
        //畫噪點(diǎn) 
        for (int i = 0; i < 100; i++) 
        ...{ 
            int x = rnd.Next(bmp.Width); 
            int y = rnd.Next(bmp.Height); 
            Color clr = color[rnd.Next(color.Length)]; 
            bmp.SetPixel(x, y, clr); 
        } 
        //清除該頁輸出緩存,設(shè)置該頁無緩存 
        Response.Buffer = true; 
        Response.ExpiresAbsolute = System.DateTime.Now.AddMilliseconds(0); 
        Response.Expires = 0; 
        Response.CacheControl = "no-cache"; 
        Response.AppendHeader("Pragma", "No-Cache"); 
        //將驗(yàn)證碼圖片寫入內(nèi)存流,并將其以 "image/Png" 格式輸出 
        MemoryStream ms = new MemoryStream(); 
        try 
        ...{ 
            bmp.Save(ms, ImageFormat.Png); 
            Response.ClearContent(); 
            Response.ContentType = "image/Png"; 
            Response.BinaryWrite(ms.ToArray()); 
        } 
        finally 
        ...{ 
            //顯式釋放資源 
            bmp.Dispose(); 
            g.Dispose(); 
        } 
    } 

使用方法如下:
新建名為 GenerateCheckCode.aspx 的文件,將上述代碼拷貝到代碼文件 GenerateCheckCode.aspx.cs 
在需要驗(yàn)證碼的地方放置語句 <asp:Image ID="img1" runat="server" ImageUrl="~/GenerateCheckCode.aspx" /> 即可。
發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 卢龙县| 东山县| 梁平县| 甘谷县| 辽阳市| 双柏县| 林西县| 乌鲁木齐县| 分宜县| 贵溪市| 府谷县| 枞阳县| 南雄市| 邢台县| 曲松县| 隆昌县| 绵阳市| 饶阳县| 潢川县| 岳普湖县| 西宁市| 凌海市| 新津县| 柳河县| 汶上县| 黄山市| 泸水县| 义马市| 双柏县| 长寿区| 太康县| 阿巴嘎旗| 平谷区| 织金县| 涡阳县| 龙江县| 东山县| 稷山县| 尼勒克县| 满洲里市| 满洲里市|