温馨提示:代码在线浏览功能只能做为源码浏览参考,如果想更进一步了解该代码请下载:ajax奥运留言本v2.0源码
当前文件:
OlympicBook20/App_Code/BaseClass.cs[3K,2009-6-12 11:51:24],打开代码结构图
OlympicBook20/App_Code/BaseClass.cs[3K,2009-6-12 11:51:24],打开代码结构图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
using System.IO; 12
/// <summary> 13
/// BaseClass 的摘要说明 14
/// </summary> 15
public class BaseClass 16
{ 17
public BaseClass() 18
{ 19
// 20
// TODO: 在此处添加构造函数逻辑 21
// 22
} 23
public string RandomNum(int n) 24
{ 25
string strchar = "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"; 26
string[] VcArray = strchar.Split(','); 27
string VNum = ""; 28
int temp = -1; 29
Random rand = new Random(); 30
for (int i = 1; i < n + 1; i++) 31
{ 32
if (temp != -1) 33
{ 34
rand = new Random(i * temp * unchecked((int)DateTime.Now.Ticks)); 35
36
} 37
int t = rand.Next(61); 38
if (temp != -1 && temp == t) 39
{ 40
return RandomNum(n); 41
} 42
temp = t; 43
VNum += VcArray[t]; 44
} 45
return VNum; 46
} 47
public void CheckCodes(string CheckCode) 48
{ 49
Random rand = new Random(); 50
int iwidth = (int)(CheckCode.Length * 15); 51
System.Drawing.Bitmap image = new System.Drawing.Bitmap(iwidth, 22); 52
Graphics g = Graphics.FromImage(image); 53
g.Clear(Color.White); 54
//画图片的背景噪音线20条 55
for (int i = 0; i < 20; i++) 56
{ 57
int x1 = rand.Next(image.Width); 58
int x2 = rand.Next(image.Width); 59
int y1 = rand.Next(image.Height); 60
int y2 = rand.Next(image.Height); 61
g.DrawLine(new Pen(Color.Silver), x1, x2, y1, y2); 62
} 63
//定义颜色 64
Color[] c = { Color.Black,Color.Red,Color.DarkBlue,Color.Green,Color.Orange,Color.Brown,Color.DarkCyan,Color.Purple,Color.YellowGreen }; 65
//定义字体 66
string[] font = { "Verdana", "Microsoft Sans Serif", "Comic Sans MS", "Arial", "宋体","新宋体" }; 67
68
//随机输出噪点 69
for (int i = 0; i < 50; i++) 70
{ 71
int x = rand.Next(image.Width); 72
int y = rand.Next(image.Height); 73
g.DrawRectangle(new Pen(Color.LightGray, 0), x, y, 1, 1); 74
} 75
76
//输出不同字体和颜色的验证码字符 77
for (int i = 0; i < CheckCode.Length; i++) 78
{ 79
int cindex = rand.Next(7); 80
int findex = rand.Next(6); 81
82
Font f = new System.Drawing.Font(font[findex], 12, System.Drawing.FontStyle.Bold); 83
Brush b = new System.Drawing.SolidBrush(c[cindex]); 84
int ii = 4; 85
if ((i + 1) % 2 == 0) 86
{ 87
ii = 2; 88
} 89
g.DrawString(CheckCode.Substring(i, 1), f, b, 3 + (i * 12), ii); 90
} 91
//画一个边框 92
93
g.DrawRectangle(new Pen(Color.Black, 0), 0, 0, image.Width - 1, image.Height - 1); 94
95
//输出到浏览器 96
System.IO.MemoryStream ms = new System.IO.MemoryStream(); 97
image.Save(ms, System.Drawing.Imaging.ImageFormat.Gif); 98
HttpContext.Current.Response.ClearContent(); 99
//Response.ClearContent(); 100
HttpContext.Current.Response.ContentType = "image/Png"; 101
HttpContext.Current.Response.BinaryWrite(ms.ToArray()); 102
g.Dispose(); 103
image.Dispose(); 104
} 105
public void MessageBoxs(string TxtMessages) 106
{ 107
//Page.ClientScript.RegisterStartupScript(this.GetType(),"alert","<script language=javascript>alert('"+TxtMessages+"');</script>"); 108
} 109
} 110









