温馨提示:代码在线浏览功能只能做为源码浏览参考,如果想更进一步了解该代码请下载:高校教师档案管理系统项目源码
当前文件:
TeacherFileProject/App_Code/checkCode.cs,打开代码结构图
TeacherFileProject/App_Code/checkCode.cs,打开代码结构图1using System; 2
using System.Data; 3
using System.Configuration; 4
using System.Web; 5
using System.Web.Security; 6
using System.Web.UI; 7
using System.Web.UI.WebControls; 8
using System.Web.UI.WebControls.WebParts; 9
using System.Web.UI.HtmlControls; 10
using System.Drawing; 11
/// <summary> 12
/// checkCode 的摘要说明 13
/// </summary> 14
public class checkCode 15
{ 16
public checkCode() 17
{ 18
// 19
// TODO: 在此处添加构造函数逻辑 20
// 21
} 22
public static void DrawImage() 23
{ 24
checkCode img = new checkCode(); 25
HttpContext.Current.Session["CheckCode"] = img.RndNum(4); 26
img.checkCodes(HttpContext.Current.Session["CheckCode"].ToString()); 27
} 28
/// <summary> 29
/// 生成验证图片 30
/// </summary> 31
/// <param name="checkCode">验证字符</param> 32
private void checkCodes(string checkCode) 33
{ 34
int iwidth = (int)(checkCode.Length * 13); 35
System.Drawing.Bitmap image = new System.Drawing.Bitmap(iwidth, 23); 36
Graphics g = Graphics.FromImage(image); 37
g.Clear(Color.White); 38
//定义颜色 39
Color[] c = { Color.Black, Color.Red, Color.DarkBlue, Color.Green, Color.Orange, Color.Brown, Color.DarkCyan, Color.Purple }; 40
//定义字体 41
string[] font = { "Verdana", "Microsoft Sans Serif", "Comic Sans MS", "Arial", "宋体" }; 42
Random rand = new Random(); 43
//随机输出噪点 44
for (int i = 0; i < 50; i++) 45
{ 46
int x = rand.Next(image.Width); 47
int y = rand.Next(image.Height); 48
g.DrawRectangle(new Pen(Color.LightGray, 0), x, y, 1, 1); 49
} 50
51
//输出不同字体和颜色的验证码字符 52
for (int i = 0; i < checkCode.Length; i++) 53
{ 54
int cindex = rand.Next(7); 55
int findex = rand.Next(5); 56
57
Font f = new System.Drawing.Font(font[findex], 10, System.Drawing.FontStyle.Bold); 58
Brush b = new System.Drawing.SolidBrush(c[cindex]); 59
int ii = 4; 60
if ((i + 1) % 2 == 0) 61
{ 62
ii = 2; 63
} 64
g.DrawString(checkCode.Substring(i, 1), f, b, 3 + (i * 12), ii); 65
} 66
//画一个边框 67
g.DrawRectangle(new Pen(Color.Black, 0), 0, 0, image.Width - 1, image.Height - 1); 68
69
//输出到浏览器 70
System.IO.MemoryStream ms = new System.IO.MemoryStream(); 71
image.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg); 72
HttpContext.Current.Response.ClearContent(); 73
//Response.ClearContent(); 74
HttpContext.Current.Response.ContentType = "image/Jpeg"; 75
HttpContext.Current.Response.BinaryWrite(ms.ToArray()); 76
g.Dispose(); 77
image.Dispose(); 78
} 79
80
/// <summary> 81
/// 生成随机的字母 82
/// </summary> 83
/// <param name="VcodeNum">生成字母的个数</param> 84
/// <returns>string</returns> 85
private string RndNum(int VcodeNum) 86
{ 87
string Vchar = "0,1,2,3,4,5,6,7,8,9"; 88
string[] VcArray = Vchar.Split(','); 89
string VNum = ""; //由于字符串很短,就不用StringBuilder了 90
int temp = -1; //记录上次随机数值,尽量避免生产几个一样的随机数 91
92
//采用一个简单的算法以保证生成随机数的不同 93
Random rand = new Random(); 94
for (int i = 1; i < VcodeNum + 1; i++) 95
{ 96
if (temp != -1) 97
{ 98
rand = new Random(i * temp * unchecked((int)DateTime.Now.Ticks)); 99
} 100
int t = rand.Next(VcArray.Length); 101
if (temp != -1 && temp == t) 102
{ 103
return RndNum(VcodeNum); 104
} 105
temp = t; 106
VNum += VcArray[t]; 107
} 108
return VNum; 109
} 110
111
} 112








