[转] ASP.NET1.1(C#)中验证码产生的原理及应用
生活随笔
收集整理的這篇文章主要介紹了
[转] ASP.NET1.1(C#)中验证码产生的原理及应用
小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
實(shí)現(xiàn)原理:
通過(guò)隨機(jī)函數(shù)產(chǎn)生驗(yàn)證碼元素,將數(shù)值型的驗(yàn)證碼元素轉(zhuǎn)換成字符型然后再連成字符串,將驗(yàn)證碼字符串寫(xiě)入Cookie以供驗(yàn)證時(shí)調(diào)用。
通過(guò)后臺(tái)動(dòng)態(tài)繪制位圖的方法,繪制一個(gè)指定大小的位圖,然后在空白位圖畫(huà)出底紋、驗(yàn)證碼字體、和邊框。
實(shí)現(xiàn)代碼:
(1)Login.aspx(登錄頁(yè)前臺(tái))
<%@?Page?language="c#"?Codebehind="Login.aspx.cs"?AutoEventWireup="false"?Inherits="Validator.Login"?%>
<!DOCTYPE?HTML?PUBLIC?"-//W3C//DTD?HTML?4.0?Transitional//EN"?>
<HTML>
????<HEAD>
????????<title>Login</title>
????????<meta?name="GENERATOR"?Content="Microsoft?Visual?Studio?.NET?7.1">
????????<meta?name="CODE_LANGUAGE"?Content="C#">
????????<meta?name="vs_defaultClientScript"?content="JavaScript">
????????<meta?name="vs_targetSchema"?content="http://schemas.microsoft.com/intellisense/ie5">
????</HEAD>
????<body?MS_POSITIONING="GridLayout">
????????<form?id="Form1"?method="post"?runat="server">
????????????<asp:button?id="Button1"?style="Z-INDEX:?101;?LEFT:?128px;?POSITION:?absolute;?TOP:?64px"?runat="server"
????????????????Width="96px"?Text="提交"></asp:button>
????????????<IMG?src="CheckCode.aspx">
????????????<asp:label?id="lblMessage"?style="Z-INDEX:?102;?LEFT:?16px;?POSITION:?absolute;?TOP:?128px"
????????????????runat="server"></asp:label>
????????????<asp:textbox?id="txtCheckCode"?style="Z-INDEX:?103;?LEFT:?16px;?POSITION:?absolute;?TOP:?64px"
????????????????runat="server"?Width="88px"></asp:textbox>
????????</form>
????</body>
</HTML>
(2)Login.aspx.cs(登錄頁(yè)后臺(tái))
using?System;
using?System.Collections;
using?System.ComponentModel;
using?System.Data;
using?System.Drawing;
using?System.Web;
using?System.Web.SessionState;
using?System.Web.UI;
using?System.Web.UI.WebControls;
using?System.Web.UI.HtmlControls;
namespace?Validator
{
????/**////?<summary>
????///?Login?的摘要說(shuō)明。
????///?</summary>
????public?class?Login?:?System.Web.UI.Page
????{
????????protected?System.Web.UI.WebControls.Button?Button1;
????????protected?System.Web.UI.WebControls.Label?lblMessage;
????????protected?System.Web.UI.WebControls.TextBox?txtCheckCode;
????
????????private?void?Page_Load(object?sender,?System.EventArgs?e)
????????{
????????????//?在此處放置用戶代碼以初始化頁(yè)面
????????}
????????Web?窗體設(shè)計(jì)器生成的代碼#region?Web?窗體設(shè)計(jì)器生成的代碼
????????override?protected?void?OnInit(EventArgs?e)
????????{
????????????//
????????????//?CODEGEN:?該調(diào)用是?ASP.NET?Web?窗體設(shè)計(jì)器所必需的。
????????????//
????????????InitializeComponent();
????????????base.OnInit(e);
????????}
????????
????????/**////?<summary>
????????///?設(shè)計(jì)器支持所需的方法?-?不要使用代碼編輯器修改
????????///?此方法的內(nèi)容。
????????///?</summary>
????????private?void?InitializeComponent()
????????{????
????????????this.Button1.Click?+=?new?System.EventHandler(this.Button1_Click);
????????????this.Load?+=?new?System.EventHandler(this.Page_Load);
????????}
????????#endregion
????????private?void?Button1_Click(object?sender,?System.EventArgs?e)
????????{
????????????if(Request.Cookies["CheckCode"]?==?null)?
????????????{
????????????????lblMessage.Text?=?"您的瀏覽器設(shè)置已被禁用?Cookies,您必須設(shè)置瀏覽器允許使用?Cookies?選項(xiàng)后才能使用本系統(tǒng)。";
????????????????lblMessage.Visible?=?true;
????????????????return;
????????????}
????????????if(String.Compare(Request.Cookies["CheckCode"].Value,?txtCheckCode.Text,?false)?!=?0)?//參數(shù)為false時(shí)為區(qū)分大小寫(xiě)
????????????{
????????????????lblMessage.Text?=?"驗(yàn)證碼錯(cuò)誤,請(qǐng)輸入正確的驗(yàn)證碼。";
????????????????lblMessage.Visible?=?true;
????????????????return;
????????????}?
????????????else?
????????????{
????????????????lblMessage.Text?=?"通過(guò)驗(yàn)證";
????????????????lblMessage.Visible?=?true;
????????????????return;
????????????}
????????}
????}
}
(3) CheckCode.aspx(驗(yàn)證頁(yè)前臺(tái))
<%@?Page?language="c#"?Codebehind="CheckCode.aspx.cs"?AutoEventWireup="false"?Inherits="Validator.CheckCode"?%>
<!DOCTYPE?HTML?PUBLIC?"-//W3C//DTD?HTML?4.0?Transitional//EN"?>?
<html>
??<head>
????<title>CheckCode</title>
????<meta?name="GENERATOR"?Content="Microsoft?Visual?Studio?.NET?7.1">
????<meta?name="CODE_LANGUAGE"?Content="C#">
????<meta?name=vs_defaultClientScript?content="JavaScript">
????<meta?name=vs_targetSchema?content="http://schemas.microsoft.com/intellisense/ie5">
??</head>
??<body?MS_POSITIONING="GridLayout">
????
????<form?id="Form1"?method="post"?runat="server">
????????<FONT?face="宋體"></FONT>
????</form>
????
??</body>
</html>
(4)CheckCode.aspx.cs(驗(yàn)證頁(yè)后臺(tái))
using?System;
using?System.Collections;
using?System.ComponentModel;
using?System.Data;
using?System.Drawing;
using?System.Web;
using?System.Web.SessionState;
using?System.Web.UI;
using?System.Web.UI.WebControls;
using?System.Web.UI.HtmlControls;
namespace?Validator
{
????/**////?<summary>
????///?CheckCode?的摘要說(shuō)明。
????///?</summary>
????public?class?CheckCode?:?System.Web.UI.Page
????{
????????private?void?Page_Load(object?sender,?System.EventArgs?e)
????????{
????????????//?在此處放置用戶代碼以初始化頁(yè)面
????????????this.CreateCheckCodeImage(GenerateCheckCode());
????????}
????????Web?窗體設(shè)計(jì)器生成的代碼#region?Web?窗體設(shè)計(jì)器生成的代碼
????????override?protected?void?OnInit(EventArgs?e)
????????{
????????????//
????????????//?CODEGEN:?該調(diào)用是?ASP.NET?Web?窗體設(shè)計(jì)器所必需的。
????????????//
????????????InitializeComponent();
????????????base.OnInit(e);
????????}
????????
????????/**////?<summary>
????????///?設(shè)計(jì)器支持所需的方法?-?不要使用代碼編輯器修改
????????///?此方法的內(nèi)容。
????????///?</summary>
????????private?void?InitializeComponent()
????????{????
????????????this.Load?+=?new?System.EventHandler(this.Page_Load);
????????}
????????#endregion
????????private?string?GenerateCheckCode()
????????{??????????????????????
????????????int?number;??????????????????????
????????????char?code;??????????????????????
????????????string?checkCode?=?String.Empty;???????????????????????????????????
????????????System.Random?random?=?new?Random();???????????????????????
????????????for(int?i=0;?i<5;?i++)??????????????????????
????????????{???//隨機(jī)產(chǎn)生一個(gè)整數(shù)?????????????????????????????
????????????????number?=?random.Next();?????????????????
????????????????//如果隨機(jī)數(shù)是偶數(shù)?取余選擇從[0-9]?????????????????????????????
????????????????if(number?%?2?==?0)????????????????????????????????????
????????????????????code?=?(char)('0'?+?(char)(number?%?10));?????????????????????????????
????????????????else?????????????????????????????
????????????????//如果隨機(jī)數(shù)是奇數(shù)?選擇從[A-Z]????????????????????????????????????
????????????????????code?=?(char)('A'?+?(char)(number?%?26));??????????????????????????????
????????????????????checkCode?+=?code.ToString();??????????????????????
????????????}???????????????????????
????????????Response.Cookies.Add(new?HttpCookie("CheckCode",?checkCode));???????????????????????
????????????return?checkCode;???????????????
????????}?????????
????????//建立一個(gè)隨機(jī)圖形???????????????
????????private?void?CreateCheckCodeImage(string?checkCode)???????????????
????????{??????????????????????
????????????if(checkCode?==?null?||?checkCode.Trim()?==?String.Empty)?????????????????????????????
????????????????return;?????????????
????????????//建立一個(gè)位圖文件?確立長(zhǎng)寬???????????????????????????????????????
????????????System.Drawing.Bitmap?image?=?new?System.Drawing.Bitmap((int)Math.Ceiling((checkCode.Length?*?12.5)),?22);??????????????????????
????????????Graphics?g?=?Graphics.FromImage(image);???????????????????????
????????????try??????????????????????
????????????{?????????????????????????????
????????????????//生成隨機(jī)生成器?????????????????????????????
????????????????Random?random?=?new?Random();??????????????????????????????
????????????????//清空?qǐng)D片背景色?????????????????????????????
????????????????g.Clear(Color.White);??????????????????????????????
????????????????//畫(huà)圖片的背景噪音點(diǎn)?????????????????????????????
????????????????for(int?i=0;?i<60;?i++)?????????????????????????????
????????????????{????????????????????????????????????
????????????????????int?x?=?random.Next(image.Width);????????????????????????????????????
????????????????????int?y?=?random.Next(image.Height);?????????????????????????????????????
????????????????????image.SetPixel(x,?y,?Color.FromArgb(random.Next()));?????????????????????????????
????????????????}?????????????????
????????????????//把產(chǎn)生的隨機(jī)數(shù)以字體的形式寫(xiě)入畫(huà)面?????????????????????????????
????????????????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);??????????????????????????????
????????????????//畫(huà)圖片的邊框線?????????????????????????????
????????????????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();??????????????????????
????????????}???????????????
????????}
????}
}
主要函數(shù)分析:
1、通過(guò)隨機(jī)函數(shù)(Random)先產(chǎn)生驗(yàn)證碼組成元素(這里為五位) 并將其轉(zhuǎn)換為字符串(屬性為只讀),完成后寫(xiě)入“Cookie”中去以供驗(yàn)證時(shí)調(diào)用。
2、將驗(yàn)證碼字符串寫(xiě)入圖形:
(1)建立一個(gè)位圖文件確定長(zhǎng)和寬:
System.Drawing.Bitmap image = new System.Drawing.Bitmap((int)Math.Ceiling((checkCode.Length * 12.5)), 22);
(a)System.Drawing. [C#] Bitmap(int width,int height);
(b)double Math.Ceiling (double a):返回大于或等于指定數(shù)字的最小整數(shù)。
(2)畫(huà)圖片的背景噪音點(diǎn)(60個(gè)):
for(int i=0; i<60; i++)? {
int x = random.Next(image.Width);
int y = random.Next(image.Height);
image.SetPixel(x, y, Color.FromArgb(random.Next()));
}
(a) public virtual int Next(int maxValue);返回一個(gè)小于所指定最大值的非負(fù)隨機(jī)數(shù)。 參數(shù):maxValue-要生成的隨機(jī)數(shù)的上限。maxValue 必須大于或等于零。
(b) image.SetPixel(int x,int y,Color color); 參數(shù): x-要設(shè)置的像素的 x 坐標(biāo);y-要設(shè)置的像素的 y 坐標(biāo);color-Color 結(jié)構(gòu),它表示要分配給指定像素的顏色。
(c) Color.FromArgb(int argb) 參數(shù):argb-指定 32 位 ARGB 值的值。
(3)把產(chǎn)生的隨機(jī)數(shù)以字體的形式寫(xiě)入位圖:Graphics.DrawString(string s,Font font,Brush brush,float x,float y);
參數(shù):s-要繪制的字符串;font-Font 對(duì)象,它定義字符串的文本格式;
brush-Brush 對(duì)象,它確定所繪制文本的顏色和紋理;
x-所繪制文本的左上角的 x 坐標(biāo);
y-所繪制文本的左上角的 y 坐標(biāo)。(在指定位置并且用指定的 Brush 和 Font 對(duì)象繪制指定的文本字符串)
(4) 畫(huà)圖片的邊框線: public void DrawRectangle(Pen pen, int x, int y, int width, int height);繪制由坐標(biāo)對(duì)、寬度和高度指定的矩形。
參數(shù):pen-Pen 對(duì)象,它確定矩形的顏色、寬度和樣式;
x-要繪制的矩形的左上角的 x 坐標(biāo);
y-要繪制的矩形的左上角的 y 坐標(biāo);
width-要繪制的矩形的寬度;height-要繪制的矩形的高度。
(5) 將圖片以二進(jìn)制流的方式輸出加上格式并可顯示出來(lái)。
通過(guò)隨機(jī)函數(shù)產(chǎn)生驗(yàn)證碼元素,將數(shù)值型的驗(yàn)證碼元素轉(zhuǎn)換成字符型然后再連成字符串,將驗(yàn)證碼字符串寫(xiě)入Cookie以供驗(yàn)證時(shí)調(diào)用。
通過(guò)后臺(tái)動(dòng)態(tài)繪制位圖的方法,繪制一個(gè)指定大小的位圖,然后在空白位圖畫(huà)出底紋、驗(yàn)證碼字體、和邊框。
實(shí)現(xiàn)代碼:
(1)Login.aspx(登錄頁(yè)前臺(tái))
<%@?Page?language="c#"?Codebehind="Login.aspx.cs"?AutoEventWireup="false"?Inherits="Validator.Login"?%>
<!DOCTYPE?HTML?PUBLIC?"-//W3C//DTD?HTML?4.0?Transitional//EN"?>
<HTML>
????<HEAD>
????????<title>Login</title>
????????<meta?name="GENERATOR"?Content="Microsoft?Visual?Studio?.NET?7.1">
????????<meta?name="CODE_LANGUAGE"?Content="C#">
????????<meta?name="vs_defaultClientScript"?content="JavaScript">
????????<meta?name="vs_targetSchema"?content="http://schemas.microsoft.com/intellisense/ie5">
????</HEAD>
????<body?MS_POSITIONING="GridLayout">
????????<form?id="Form1"?method="post"?runat="server">
????????????<asp:button?id="Button1"?style="Z-INDEX:?101;?LEFT:?128px;?POSITION:?absolute;?TOP:?64px"?runat="server"
????????????????Width="96px"?Text="提交"></asp:button>
????????????<IMG?src="CheckCode.aspx">
????????????<asp:label?id="lblMessage"?style="Z-INDEX:?102;?LEFT:?16px;?POSITION:?absolute;?TOP:?128px"
????????????????runat="server"></asp:label>
????????????<asp:textbox?id="txtCheckCode"?style="Z-INDEX:?103;?LEFT:?16px;?POSITION:?absolute;?TOP:?64px"
????????????????runat="server"?Width="88px"></asp:textbox>
????????</form>
????</body>
</HTML>
(2)Login.aspx.cs(登錄頁(yè)后臺(tái))
using?System;
using?System.Collections;
using?System.ComponentModel;
using?System.Data;
using?System.Drawing;
using?System.Web;
using?System.Web.SessionState;
using?System.Web.UI;
using?System.Web.UI.WebControls;
using?System.Web.UI.HtmlControls;
namespace?Validator
{
????/**////?<summary>
????///?Login?的摘要說(shuō)明。
????///?</summary>
????public?class?Login?:?System.Web.UI.Page
????{
????????protected?System.Web.UI.WebControls.Button?Button1;
????????protected?System.Web.UI.WebControls.Label?lblMessage;
????????protected?System.Web.UI.WebControls.TextBox?txtCheckCode;
????
????????private?void?Page_Load(object?sender,?System.EventArgs?e)
????????{
????????????//?在此處放置用戶代碼以初始化頁(yè)面
????????}
????????Web?窗體設(shè)計(jì)器生成的代碼#region?Web?窗體設(shè)計(jì)器生成的代碼
????????override?protected?void?OnInit(EventArgs?e)
????????{
????????????//
????????????//?CODEGEN:?該調(diào)用是?ASP.NET?Web?窗體設(shè)計(jì)器所必需的。
????????????//
????????????InitializeComponent();
????????????base.OnInit(e);
????????}
????????
????????/**////?<summary>
????????///?設(shè)計(jì)器支持所需的方法?-?不要使用代碼編輯器修改
????????///?此方法的內(nèi)容。
????????///?</summary>
????????private?void?InitializeComponent()
????????{????
????????????this.Button1.Click?+=?new?System.EventHandler(this.Button1_Click);
????????????this.Load?+=?new?System.EventHandler(this.Page_Load);
????????}
????????#endregion
????????private?void?Button1_Click(object?sender,?System.EventArgs?e)
????????{
????????????if(Request.Cookies["CheckCode"]?==?null)?
????????????{
????????????????lblMessage.Text?=?"您的瀏覽器設(shè)置已被禁用?Cookies,您必須設(shè)置瀏覽器允許使用?Cookies?選項(xiàng)后才能使用本系統(tǒng)。";
????????????????lblMessage.Visible?=?true;
????????????????return;
????????????}
????????????if(String.Compare(Request.Cookies["CheckCode"].Value,?txtCheckCode.Text,?false)?!=?0)?//參數(shù)為false時(shí)為區(qū)分大小寫(xiě)
????????????{
????????????????lblMessage.Text?=?"驗(yàn)證碼錯(cuò)誤,請(qǐng)輸入正確的驗(yàn)證碼。";
????????????????lblMessage.Visible?=?true;
????????????????return;
????????????}?
????????????else?
????????????{
????????????????lblMessage.Text?=?"通過(guò)驗(yàn)證";
????????????????lblMessage.Visible?=?true;
????????????????return;
????????????}
????????}
????}
}
(3) CheckCode.aspx(驗(yàn)證頁(yè)前臺(tái))
<%@?Page?language="c#"?Codebehind="CheckCode.aspx.cs"?AutoEventWireup="false"?Inherits="Validator.CheckCode"?%>
<!DOCTYPE?HTML?PUBLIC?"-//W3C//DTD?HTML?4.0?Transitional//EN"?>?
<html>
??<head>
????<title>CheckCode</title>
????<meta?name="GENERATOR"?Content="Microsoft?Visual?Studio?.NET?7.1">
????<meta?name="CODE_LANGUAGE"?Content="C#">
????<meta?name=vs_defaultClientScript?content="JavaScript">
????<meta?name=vs_targetSchema?content="http://schemas.microsoft.com/intellisense/ie5">
??</head>
??<body?MS_POSITIONING="GridLayout">
????
????<form?id="Form1"?method="post"?runat="server">
????????<FONT?face="宋體"></FONT>
????</form>
????
??</body>
</html>
(4)CheckCode.aspx.cs(驗(yàn)證頁(yè)后臺(tái))
using?System;
using?System.Collections;
using?System.ComponentModel;
using?System.Data;
using?System.Drawing;
using?System.Web;
using?System.Web.SessionState;
using?System.Web.UI;
using?System.Web.UI.WebControls;
using?System.Web.UI.HtmlControls;
namespace?Validator
{
????/**////?<summary>
????///?CheckCode?的摘要說(shuō)明。
????///?</summary>
????public?class?CheckCode?:?System.Web.UI.Page
????{
????????private?void?Page_Load(object?sender,?System.EventArgs?e)
????????{
????????????//?在此處放置用戶代碼以初始化頁(yè)面
????????????this.CreateCheckCodeImage(GenerateCheckCode());
????????}
????????Web?窗體設(shè)計(jì)器生成的代碼#region?Web?窗體設(shè)計(jì)器生成的代碼
????????override?protected?void?OnInit(EventArgs?e)
????????{
????????????//
????????????//?CODEGEN:?該調(diào)用是?ASP.NET?Web?窗體設(shè)計(jì)器所必需的。
????????????//
????????????InitializeComponent();
????????????base.OnInit(e);
????????}
????????
????????/**////?<summary>
????????///?設(shè)計(jì)器支持所需的方法?-?不要使用代碼編輯器修改
????????///?此方法的內(nèi)容。
????????///?</summary>
????????private?void?InitializeComponent()
????????{????
????????????this.Load?+=?new?System.EventHandler(this.Page_Load);
????????}
????????#endregion
????????private?string?GenerateCheckCode()
????????{??????????????????????
????????????int?number;??????????????????????
????????????char?code;??????????????????????
????????????string?checkCode?=?String.Empty;???????????????????????????????????
????????????System.Random?random?=?new?Random();???????????????????????
????????????for(int?i=0;?i<5;?i++)??????????????????????
????????????{???//隨機(jī)產(chǎn)生一個(gè)整數(shù)?????????????????????????????
????????????????number?=?random.Next();?????????????????
????????????????//如果隨機(jī)數(shù)是偶數(shù)?取余選擇從[0-9]?????????????????????????????
????????????????if(number?%?2?==?0)????????????????????????????????????
????????????????????code?=?(char)('0'?+?(char)(number?%?10));?????????????????????????????
????????????????else?????????????????????????????
????????????????//如果隨機(jī)數(shù)是奇數(shù)?選擇從[A-Z]????????????????????????????????????
????????????????????code?=?(char)('A'?+?(char)(number?%?26));??????????????????????????????
????????????????????checkCode?+=?code.ToString();??????????????????????
????????????}???????????????????????
????????????Response.Cookies.Add(new?HttpCookie("CheckCode",?checkCode));???????????????????????
????????????return?checkCode;???????????????
????????}?????????
????????//建立一個(gè)隨機(jī)圖形???????????????
????????private?void?CreateCheckCodeImage(string?checkCode)???????????????
????????{??????????????????????
????????????if(checkCode?==?null?||?checkCode.Trim()?==?String.Empty)?????????????????????????????
????????????????return;?????????????
????????????//建立一個(gè)位圖文件?確立長(zhǎng)寬???????????????????????????????????????
????????????System.Drawing.Bitmap?image?=?new?System.Drawing.Bitmap((int)Math.Ceiling((checkCode.Length?*?12.5)),?22);??????????????????????
????????????Graphics?g?=?Graphics.FromImage(image);???????????????????????
????????????try??????????????????????
????????????{?????????????????????????????
????????????????//生成隨機(jī)生成器?????????????????????????????
????????????????Random?random?=?new?Random();??????????????????????????????
????????????????//清空?qǐng)D片背景色?????????????????????????????
????????????????g.Clear(Color.White);??????????????????????????????
????????????????//畫(huà)圖片的背景噪音點(diǎn)?????????????????????????????
????????????????for(int?i=0;?i<60;?i++)?????????????????????????????
????????????????{????????????????????????????????????
????????????????????int?x?=?random.Next(image.Width);????????????????????????????????????
????????????????????int?y?=?random.Next(image.Height);?????????????????????????????????????
????????????????????image.SetPixel(x,?y,?Color.FromArgb(random.Next()));?????????????????????????????
????????????????}?????????????????
????????????????//把產(chǎn)生的隨機(jī)數(shù)以字體的形式寫(xiě)入畫(huà)面?????????????????????????????
????????????????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);??????????????????????????????
????????????????//畫(huà)圖片的邊框線?????????????????????????????
????????????????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();??????????????????????
????????????}???????????????
????????}
????}
}
主要函數(shù)分析:
1、通過(guò)隨機(jī)函數(shù)(Random)先產(chǎn)生驗(yàn)證碼組成元素(這里為五位) 并將其轉(zhuǎn)換為字符串(屬性為只讀),完成后寫(xiě)入“Cookie”中去以供驗(yàn)證時(shí)調(diào)用。
2、將驗(yàn)證碼字符串寫(xiě)入圖形:
(1)建立一個(gè)位圖文件確定長(zhǎng)和寬:
System.Drawing.Bitmap image = new System.Drawing.Bitmap((int)Math.Ceiling((checkCode.Length * 12.5)), 22);
(a)System.Drawing. [C#] Bitmap(int width,int height);
(b)double Math.Ceiling (double a):返回大于或等于指定數(shù)字的最小整數(shù)。
(2)畫(huà)圖片的背景噪音點(diǎn)(60個(gè)):
for(int i=0; i<60; i++)? {
int x = random.Next(image.Width);
int y = random.Next(image.Height);
image.SetPixel(x, y, Color.FromArgb(random.Next()));
}
(a) public virtual int Next(int maxValue);返回一個(gè)小于所指定最大值的非負(fù)隨機(jī)數(shù)。 參數(shù):maxValue-要生成的隨機(jī)數(shù)的上限。maxValue 必須大于或等于零。
(b) image.SetPixel(int x,int y,Color color); 參數(shù): x-要設(shè)置的像素的 x 坐標(biāo);y-要設(shè)置的像素的 y 坐標(biāo);color-Color 結(jié)構(gòu),它表示要分配給指定像素的顏色。
(c) Color.FromArgb(int argb) 參數(shù):argb-指定 32 位 ARGB 值的值。
(3)把產(chǎn)生的隨機(jī)數(shù)以字體的形式寫(xiě)入位圖:Graphics.DrawString(string s,Font font,Brush brush,float x,float y);
參數(shù):s-要繪制的字符串;font-Font 對(duì)象,它定義字符串的文本格式;
brush-Brush 對(duì)象,它確定所繪制文本的顏色和紋理;
x-所繪制文本的左上角的 x 坐標(biāo);
y-所繪制文本的左上角的 y 坐標(biāo)。(在指定位置并且用指定的 Brush 和 Font 對(duì)象繪制指定的文本字符串)
(4) 畫(huà)圖片的邊框線: public void DrawRectangle(Pen pen, int x, int y, int width, int height);繪制由坐標(biāo)對(duì)、寬度和高度指定的矩形。
參數(shù):pen-Pen 對(duì)象,它確定矩形的顏色、寬度和樣式;
x-要繪制的矩形的左上角的 x 坐標(biāo);
y-要繪制的矩形的左上角的 y 坐標(biāo);
width-要繪制的矩形的寬度;height-要繪制的矩形的高度。
(5) 將圖片以二進(jìn)制流的方式輸出加上格式并可顯示出來(lái)。
總結(jié)
以上是生活随笔為你收集整理的[转] ASP.NET1.1(C#)中验证码产生的原理及应用的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 关于开源软件的书籍一定要尽量提供搭配好的
- 下一篇: 郁闷!绝望!感动!