生成驗證碼

2021-05-11 13:12:24 字數 2718 閱讀 3047

生成驗證碼的類:

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.drawing2d;

public partial class yanzheng : system.web.ui.page

/// /// 生成隨機校驗碼字串

/// /// ///

/// 生成的隨機校驗碼字串

public string generatecheckcode()

//    else

//   

//    strcode += ((char)number).tostring();

//}在cookie中儲存校驗碼或session中

}/// /// 根據校驗碼輸出

/// ///

/// 產生的隨機校驗碼

public void createcheckcodeimage(string checkcode)

//根據校驗碼的長度確定輸出的長度

system.drawing.bitmap image = new system.drawing.bitmap((int)math.ceiling(decimal.parse((checkcode.length * 15) + "")), 20);

//建立graphics物件

system.drawing.graphics g = system.drawing.graphics.fromimage(image);

try//輸出中校驗碼的字型:12號arial,粗斜體

system.drawing.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 system.drawing.rectangle(0, 0, image.width, image.height), system.drawing.color.blue, system.drawing.color.purple, 1.2f, true);

g.drawstring(checkcode, font, brush, 2, 3);

//畫的前景噪音點50個

for (int i = 0; i < 50; i++)

//畫的邊框線

g.drawrectangle(new system.drawing.pen(system.drawing.color.saddlebrown), 0, 0, image.width - 1, image.height - 1);

//建立記憶體流用於輸出

using (system.io.memorystream ms = new system.io.memorystream())

}finally}}

前端**:

function show()

驗證輸入是否正確:

//判斷驗證碼輸入是否正確(不區分大小寫)

public static bool check(string input, string yanzheng)

else}}

return flag;}}

驗證碼一(驗證碼生成)

根據手機好查詢密碼 return type description code for i 0 i 6 i 4位驗證碼也可以用rand 1000,9999 直接生成 將生成的驗證碼寫入session,備驗證時用 session start session verify num code 建立,定義顏色...

生成驗證碼

在此處放置使用者 以初始化頁面 bitmap image new bitmap int math.ceiling validatenum.length 12.5 22 graphics g graphics.fromimage image tryfinally region web 窗體設計器生成的...

驗證碼生成

from captcha.image import imagecaptcha pip install captcha import numpy as np from pil import image import random import sys import os if not os.path....