温馨提示:代码在线浏览功能只能做为源码浏览参考,如果想更进一步了解该代码请下载:无忧之源招聘系统及Log4Net源码
当前文件:
51Job/App_Code/CommonComponent/ValidateCode.cs[2K,2009-6-12 11:31:08],打开代码结构图
51Job/App_Code/CommonComponent/ValidateCode.cs[2K,2009-6-12 11:31:08],打开代码结构图1using System; 2
using System.IO; 3
using System.Drawing; 4
using System.Drawing.Imaging; 5
using System.Text; 6
7
namespace HRManager.CommonComponent 8
{ 9
/// <summary> 10
/// 生成验证码图象类 11
/// </summary> 12
public class ValidateCode 13
{ 14
private const int CODELEN = 4; //4位验证码 15
16
public string code; //生成的验证码 17
public string imgFilePath ; //生成的图象路径 18
public string imgFileName; //生成的图象文件名 19
20
/// <summary> 21
/// 构造函数,得到保存图象的路径 22
/// </summary> 23
public ValidateCode(string imgFilePath) 24
{ 25
this.imgFilePath = imgFilePath; 26
} 27
28
/// <summary> 29
/// 析构函数,删除生成的图象文件 30
/// </summary> 31
~ValidateCode() 32
{ 33
File.Delete(this.imgFilePath+this.imgFileName); //使用File的Delete静态方法,删除临时图象 34
} 35
36
/// <summary> 37
/// 生成验证码图象文件,并存储在服务器中 38
/// </summary> 39
public void GenerateCodeImage() 40
{ 41
this.GetCode(); 42
Bitmap img = null; 43
Graphics g = null; 44
45
int gHeight = code.Length * 12; 46
img = new Bitmap(gHeight, 25); 47
g = Graphics.FromImage(img); 48
49
g.Clear(Color.AliceBlue); //背景颜色 50
Font font = new Font("Arial Black", 10); //文字字体 51
SolidBrush brush = new SolidBrush(Color.Black); //文字颜色 52
53
g.DrawString(this.code, font, brush, 3, 3); 54
55
this.imgFileName = this.code + ".Png"; 56
img.Save(this.imgFilePath+this.imgFileName, ImageFormat.Png); 57
58
g.Dispose(); 59
img.Dispose(); 60
} 61
62
/// <summary> 63
/// 生成随即的四位字母、数字混合串 64
/// </summary> 65
/// <returns>四位字母、数字混合串</returns> 66
public void GetCode() 67
{ 68
char[] allChars = new char[]{ 69
'0','1','2','3','4','5','6','7','8','9', 70
'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', 71
'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' 72
}; 73
74
Random rand = new Random(); 75
76
for (int i = 0; i < CODELEN; i++) 77
{ 78
rand = new Random((i+1) * (int)DateTime.Now.Ticks); 79
this.code+= allChars[rand.Next(allChars.Length - 1)]; 80
} 81
} 82
} 83
}






}