它的後台cs文件代碼如下
PageLoad
private void Page_Load(object sender
{
string checkCode = CreateRandomCode(
Session[
CreateImage(checkCode)
}
其中CreateRandomCode是自定義的函數
private string CreateRandomCode(int codeCount)
{
string allChar =
string[] allCharArray = allChar
string randomCode =
int temp =
Random rand = new Random()
for(int i =
{
if(temp !=
{
rand = new Random(i*temp*((int)DateTime
}
int t = rand
if(temp == t)
{
return CreateRandomCode(codeCount)
}
temp = t;
randomCode += allCharArray[t];
}
return randomCode;
}
CreateImage也是一個自定義的函數
private void CreateImage(string checkCode)
{
int iwidth = (int)(checkCode
System
Graphics g = Graphics
Font f = new System
Brush b = new System
//g
g
g
Pen blackPen = new Pen(Color
Random rand = new Random()
for (int i=
{
int y = rand
g
}
System
image
Response
Response
Response
g
image
}
//g
g
這兩種方法都可以改變生成圖片的背景顏色
下面那個for循環用來生成一些隨機的水平線
在需要用到驗證碼的頁面添加一個<asp:Image>控件即可
<asp:Image Runat=
From:http://tw.wingwit.com/Article/program/net/201311/13349.html