第一步生成驗證碼
新增一個validatecode.aspx頁面
在validatecode.aspx.cs的代碼如下
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.drawing.imaging;
public partial class validatecode : system.web.ui.page
{ //該頁面將用于生成驗證碼圖片
protected void page_load(object sender, eventargs e)
{ //調用函數將驗證碼生成圖片
this.createcheckcodeimage(generatecheckcode());
}
private string generatecheckcode()
{ //產生五位的隨機字符串
int number;
char code;
string checkcode = string.empty;
system.random random = new random();
for (int i = 0; i < 5; i++)
{
number = random.next();
if (number % 2 == 0)
code = (char)('0' + (char)(number % 10));
else
code = (char)('a' + (char)(number % 26));
checkcode += code.tostring();
}
//response.cookies.add(new httpcookie("checkcode", checkcode));
session["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 < 25; 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.silver), 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();
}
}
}
第二步測試驗證碼
建立test.aspx
form id="form1" runat="server">
<div>
<asp:textbox id="textbox1" runat="server"></asp:textbox>
<asp:button id="button1" runat="server" text="button" onclick="button1_click" /><br />
<asp:image id="image1" runat="server" imageurl="~/validatecode.aspx" />//該圖片將用于顯示驗證碼
</div>
</form>
test.aspx.cs代碼
protected void page_load(object sender, eventargs e)
{
}
protected void button1_click(object sender, eventargs e)
{
if (this.textbox1.text.tostring().trim() == session["checkcode"].tostring()) { response.write("<script lauguage='javascript'>alert('驗證成功');</script>"); }
}
新聞熱點
疑難解答
圖片精選