温馨提示:代码在线浏览功能只能做为源码浏览参考,不能展示项目的全部,如果想更进一步了解该代码请下载:三层班级留言本源码
当前文件路径:ClassSay/Class/MAC.aspx.cs

1using System; 2
using System.Collections; 3
using System.ComponentModel; 4
using System.Data; 5
using System.Drawing; 6
using System.Web; 7
using System.Web.SessionState; 8
using System.Web.UI; 9
using System.Web.UI.WebControls; 10
using System.Web.UI.HtmlControls; 11
using System.Drawing.Imaging; 12
using System.Drawing.Drawing2D; 13
14
public partial class MAC : System.Web.UI.Page 15
{ 16
private void Page_Load(object sender, System.EventArgs e) 17
{ 18
// 在此处放置用户代码以初始化页面 19
if (!Page.IsPostBack) 20
{ 21
this.GetImg(this.GetRandomCode(4)); 22
} 23
} 24
25
26
//获得随机码 27
private string GetRandomCode(int count) 28
{ 29
string strReturn = null; 30
string strAll = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"; 31
Random rnd = new Random(); 32
for (int i = 0; i < count; i++) 33
{ 34
strReturn += strAll[rnd.Next(strAll.Length)].ToString(); 35
} 36
37
return strReturn; 38
} 39
40
//生成图片 41
private void GetImg(string code) 42
{ 43
Bitmap bitmap = new Bitmap(60, 20);//定义一个画板 44
Graphics g = Graphics.FromImage(bitmap);//在画板上定义绘图的实例 45
46
Rectangle rc = new Rectangle(0, 0, 60, 20);//定义一个矩形 47
48
g.FillRectangle(new SolidBrush(Color.WhiteSmoke), rc);//填充矩形 49
g.DrawString(code, new Font("宋体", 16), new LinearGradientBrush( 50
rc, Color.Green, Color.Red, LinearGradientMode.Horizontal), rc);//在矩形内画出字符串 51
52
bitmap.Save(Response.OutputStream, System.Drawing.Imaging.ImageFormat.Jpeg);//将图片显示出来 53
54
Session["ValidateCode"] = code;//将字符串保存到Session中,以便需要时进行验证 55
56
g.Dispose(); 57
bitmap.Dispose(); 58
} 59
60
61
Web 窗体设计器生成的代码 80
} 81





}
}