?? validatecode.cs
字號:
using System;
using System.IO;
using System.Drawing;
using System.Drawing.Imaging;
using System.Text;
namespace HRManager.CommonComponent
{
/// <summary>
/// 生成驗證碼圖象類
/// </summary>
public class ValidateCode
{
private const int CODELEN = 4; //4位驗證碼
public string code; //生成的驗證碼
public string imgFilePath ; //生成的圖象路徑
public string imgFileName; //生成的圖象文件名
/// <summary>
/// 構造函數,得到保存圖象的路徑
/// </summary>
public ValidateCode(string imgFilePath)
{
this.imgFilePath = imgFilePath;
}
/// <summary>
/// 析構函數,刪除生成的圖象文件
/// </summary>
~ValidateCode()
{
File.Delete(this.imgFilePath+this.imgFileName); //使用File的Delete靜態方法,刪除臨時圖象
}
/// <summary>
/// 生成驗證碼圖象文件,并存儲在服務器中
/// </summary>
public void GenerateCodeImage()
{
this.GetCode();
Bitmap img = null;
Graphics g = null;
int gHeight = code.Length * 12;
img = new Bitmap(gHeight, 25);
g = Graphics.FromImage(img);
g.Clear(Color.AliceBlue); //背景顏色
Font font = new Font("Arial Black", 10); //文字字體
SolidBrush brush = new SolidBrush(Color.Black); //文字顏色
g.DrawString(this.code, font, brush, 3, 3);
this.imgFileName = this.code + ".Png";
img.Save(this.imgFilePath+this.imgFileName, ImageFormat.Png);
g.Dispose();
img.Dispose();
}
/// <summary>
/// 生成隨即的四位字母、數字混合串
/// </summary>
/// <returns>四位字母、數字混合串</returns>
public void GetCode()
{
char[] allChars = new char[]{
'0','1','2','3','4','5','6','7','8','9',
'a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z',
'A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z'
};
Random rand = new Random();
for (int i = 0; i < CODELEN; i++)
{
rand = new Random((i+1) * (int)DateTime.Now.Ticks);
this.code+= allChars[rand.Next(allChars.Length - 1)];
}
}
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -