#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++)
{
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));
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
{
//生成隨機(jī)生成器
random random = new random();
//清空?qǐng)D片背景色
g.clear(color.white);
//畫(huà)圖片的背景噪音線
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);
//畫(huà)圖片的前景噪音點(diǎn)
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()));
}
//畫(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();
}
}
}
假如以上驗(yàn)證碼生成器頁(yè)面名為:checkcode.aspx,那么在登錄頁(yè)面中使用“<img>” 這個(gè) html 元素來(lái)顯示生成的驗(yàn)證碼圖片:<img src="checkcode.aspx">
在登錄頁(yè)面的登錄按鈕的處理事件中使用以下代碼判斷驗(yàn)證碼:
private void btnlogin_click(object sender, system.web.ui.imageclickeventargs 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, true) != 0)
{
lblmessage.text = "驗(yàn)證碼錯(cuò)誤,請(qǐng)輸入正確的驗(yàn)證碼。";
lblmessage.visible = true;
return;
}
/******其他代碼******/
}
新聞熱點(diǎn)
疑難解答
圖片精選