温馨提示:代码在线浏览功能只能做为源码浏览参考,如果想更进一步了解该代码请下载:文件在线管理系统0.30源码
当前文件:
FileManager/WebSite/Client/getCode.aspx.cs,打开代码结构图
FileManager/WebSite/Client/getCode.aspx.cs,打开代码结构图1using System; 2
using System.Data; 3
using System.Collections; 4
using System.Drawing; 5
6
public partial class getCode : System.Web.UI.Page 7
{ 8
protected void Page_Load(object sender, EventArgs e) 9
{ 10
this.CreateCheckCodeImage(GenerateCheckCode()); 11
} 12
13
private string GenerateCheckCode() 14
{ 15
int number; 16
char code; 17
string checkCode = String.Empty; 18
19
System.Random random = new Random(); 20
21
for (int i = 0; i < 4; i++) 22
{ 23
number = random.Next(); 24
25
// if (number % 2 == 0) 26
code = (char)('0' + (char)(number % 10)); 27
// else 28
// code = (char)('A' + (char)(number % 26)); 29
30
checkCode += code.ToString(); 31
} 32
33
//Response.Cookies.Add(new HttpCookie("CheckCode", checkCode)); 34
Session["CheckCode"] = checkCode; 35
36
return checkCode; 37
} 38
39
private void CreateCheckCodeImage(string checkCode) 40
{ 41
if (checkCode == null || checkCode.Trim() == String.Empty) 42
return; 43
44
System.Drawing.Bitmap image = new System.Drawing.Bitmap((int)Math.Ceiling((checkCode.Length * 13.5)), 22); 45
Graphics g = Graphics.FromImage(image); 46
47
try 48
{ 49
//生成随机生成器 50
Random random = new Random(); 51
52
//清空图片背景色 53
g.Clear(Color.White); 54
55
//画图片的背景噪音线 56
for (int i = 0; i < 10; i++) 57
{ 58
int x1 = random.Next(image.Width); 59
int x2 = random.Next(image.Width); 60
int y1 = random.Next(image.Height); 61
int y2 = random.Next(image.Height); 62
63
g.DrawLine(new Pen(Color.Silver), x1, y1, x2, y2); 64
} 65
66
Font font = new System.Drawing.Font("Arial", 12, (System.Drawing.FontStyle.Bold | System.Drawing.FontStyle.Italic)); 67
System.Drawing.Drawing2D.LinearGradientBrush brush = new System.Drawing.Drawing2D.LinearGradientBrush(new Rectangle(0, 0, image.Width, image.Height), Color.Blue, Color.DarkRed, 1.2f, true); 68
g.DrawString(checkCode, font, brush, 2, 2); 69
70
//画图片的前景噪音点 71
for (int i = 0; i < 10; i++) 72
{ 73
int x = random.Next(image.Width); 74
int y = random.Next(image.Height); 75
76
image.SetPixel(x, y, Color.FromArgb(random.Next())); 77
} 78
79
//画图片的边框线 80
g.DrawRectangle(new Pen(Color.Silver), 0, 0, image.Width - 1, image.Height - 1); 81
82
System.IO.MemoryStream ms = new System.IO.MemoryStream(); 83
image.Save(ms, System.Drawing.Imaging.ImageFormat.Gif); 84
85
//history back 不重复 86
Response.Cache.SetNoStore(); 87
Response.ClearContent(); 88
Response.ContentType = "image/Gif"; 89
Response.BinaryWrite(ms.ToArray()); 90
} 91
finally 92
{ 93
g.Dispose(); 94
image.Dispose(); 95
} 96
} 97
} 98





}
}