實現代碼
代碼如下:
/// <summary>
/// 生成驗證碼圖片,保存session名稱VerificationCode
/// </summary>
public static void CreateVerificationCode()
{
int number;
string checkCode = string.Empty;
//隨機數種子
Random randoms = new Random();
for (int i = 0; i < 4; i++) //校驗碼長度為4
{
//隨機的整數
number = randoms.Next();
//字符從0-9,A-Z中隨機產生,對應的ASCII碼分別為
//48-57,65-90
number = number % 36;
if (number < 10)
{
number += 48;
}
else
{
number += 55;
}
checkCode += ((char)number).ToString();
}
//在session中保存校驗碼
System.Web.HttpContext.Current.Session["VerificationCode"] = checkCode;
//若校驗碼為空,則直接返回
if (checkCode == null || checkCode.Trim() == String.Empty)
{
return;
}
//根據校驗碼的長度確定輸出圖片的長度
System.Drawing.Bitmap image = new System.Drawing.Bitmap(55, 20);//(int)Math.Ceiling(Convert.ToDouble(checkCode.Length * 15))
//創建Graphics對象
Graphics g = Graphics.FromImage(image);
try
新聞熱點
疑難解答
圖片精選