1.建立ValidateCode.aspx页面cs代码
public
class
ValidateCode : System.Web.UI.Page
{
private void Page_Load(object sender, System.EventArgs e)
{
//如果要在页面a.aspx生成验证码,则在该页面添加一个图片控件,假设命名为:Image1,然后在page_Load事件中写如下代码:
//ImageButton1.ImageUrl = "ValidateCode.aspx";
//这样就可以生成验证码了,ValidateCode.aspx页面可以随便放在哪里,不过要注意Image1.src 要写对,同级可以直接写ValidateCode.aspx,上一级写../ValidateCode.aspx,很方便吧。
if(!IsPostBack)
{
//RndNum是一个自定义函数
//这里的数字4代表显示的是4位的验证字符串!
//string VNum=RndNum(4);
string VNum=GenerateRandom(4);
Session["VNum"] = VNum;
Validate_Code(VNum);
}
}
Web Form Designer generated code#region Web Form Designer generated code
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: This call is required by the ASP.NET Web Form Designer.
//
InitializeComponent();
base.OnInit(e);
}
/**//// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.Load += new System.EventHandler(this.Page_Load);
}
#endregion
private void Validate_Code(string VNum)
{
int Gheight=(int)(VNum.Length * 11.5);
//gheight为图片宽度,根据字符长度自动更改图片宽度
System.Drawing.Bitmap Img = new System.Drawing.Bitmap(Gheight,20);
Graphics g = Graphics.FromImage(Img);
g.DrawString(VNum,new System.Drawing.Font("Arial",10),new System.Drawing.SolidBrush(Color.Red),3,3);
//在矩形内绘制字串(字串,字体,画笔颜色,左上x.左上y)
System.IO.MemoryStream ms=new System.IO.MemoryStream();
Img.Save(ms,System.Drawing.Imaging.ImageFormat.Png);
Response.ClearContent(); //需要输出图象信息 要修改HTTP头
Response.ContentType="image/Png";
Response.BinaryWrite(ms.ToArray());
g.Dispose();
Img.Dispose();
Response.End();
}
private static char[] constant=
{
'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'
};
public static string GenerateRandom(int Length)
{
System.Text.StringBuilder newRandom = new System.Text.StringBuilder(62);
Random rd= new Random();
for(int i=0;i<Length;i++)
{
newRandom.Append(constant[rd.Next(62)]);
}
return newRandom.ToString();
}
}
2.建立演示页面Login.aspx,html代码
{
private void Page_Load(object sender, System.EventArgs e)
{
//如果要在页面a.aspx生成验证码,则在该页面添加一个图片控件,假设命名为:Image1,然后在page_Load事件中写如下代码:
//ImageButton1.ImageUrl = "ValidateCode.aspx";
//这样就可以生成验证码了,ValidateCode.aspx页面可以随便放在哪里,不过要注意Image1.src 要写对,同级可以直接写ValidateCode.aspx,上一级写../ValidateCode.aspx,很方便吧。
if(!IsPostBack)
{
//RndNum是一个自定义函数
//这里的数字4代表显示的是4位的验证字符串!
//string VNum=RndNum(4);
string VNum=GenerateRandom(4);
Session["VNum"] = VNum;
Validate_Code(VNum);
}
}
Web Form Designer generated code#region Web Form Designer generated code
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: This call is required by the ASP.NET Web Form Designer.
//
InitializeComponent();
base.OnInit(e);
}
/**//// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.Load += new System.EventHandler(this.Page_Load);
}
#endregion
private void Validate_Code(string VNum)
{
int Gheight=(int)(VNum.Length * 11.5);
//gheight为图片宽度,根据字符长度自动更改图片宽度
System.Drawing.Bitmap Img = new System.Drawing.Bitmap(Gheight,20);
Graphics g = Graphics.FromImage(Img);
g.DrawString(VNum,new System.Drawing.Font("Arial",10),new System.Drawing.SolidBrush(Color.Red),3,3);
//在矩形内绘制字串(字串,字体,画笔颜色,左上x.左上y)
System.IO.MemoryStream ms=new System.IO.MemoryStream();
Img.Save(ms,System.Drawing.Imaging.ImageFormat.Png);
Response.ClearContent(); //需要输出图象信息 要修改HTTP头
Response.ContentType="image/Png";
Response.BinaryWrite(ms.ToArray());
g.Dispose();
Img.Dispose();
Response.End();
}
private static char[] constant=
{
'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'
};
public static string GenerateRandom(int Length)
{
System.Text.StringBuilder newRandom = new System.Text.StringBuilder(62);
Random rd= new Random();
for(int i=0;i<Length;i++)
{
newRandom.Append(constant[rd.Next(62)]);
}
return newRandom.ToString();
}
}
<
HTML
>
< HEAD >
< title > Login </ title >
< meta name ="GENERATOR" Content ="Microsoft Visual Studio .NET 7.1" >
< meta name ="CODE_LANGUAGE" Content ="C#" >
< meta name ="vs_defaultClientScript" content ="JavaScript" >
< meta name ="vs_targetSchema" content ="http://schemas.microsoft.com/intellisense/ie5" >
</ HEAD >
< body >
< form id ="Form1" method ="post" runat ="server" >
< table align ="center" cellSpacing ="0" cellPadding ="0" width ="100%" border ="0" height ="100%" >
< colgroup >
< col width ="30%" >
</ col >
< col width ="40%" >
</ col >
< col width ="30%" >
</ col >
</ colgroup >
< tr >
< td ></ td >
< td valign ="middle" >
< TABLE id ="Table1" align ="center" cellSpacing ="0" cellPadding ="0" width ="100%" border ="0" >
< colgroup >
< col width ="20%" >
</ col >
< col width ="35%" >
</ col >
< col width ="25%" >
</ col >
< col width ="20%" >
</ col >
</ colgroup >
< TR >
< TD align ="right" > LoginName </ TD >
< TD >
< asp:TextBox id ="txtLoginName" runat ="server" Width ="100%" ></ asp:TextBox ></ TD >
< td ></ td >
< td ></ td >
</ TR >
< TR >
< TD align ="right" > Password </ TD >
< TD >
< asp:TextBox id ="txtPassword" runat ="server" Width ="100%" ></ asp:TextBox ></ TD >
< td ></ td >
< td ></ td >
</ TR >
< TR >
< TD align ="right" > ValidateCode </ TD >
< TD >
< asp:TextBox id ="txtValidateCode" runat ="server" Width ="100%" ></ asp:TextBox ></ TD >
< td >< IMG alt ="" src ="Pages/Wonderful/Form/ValidateCode.aspx" ></ td >
< td ></ td >
</ TR >
< TR >
< TD ></ TD >
< TD >
< asp:Button id ="Login" runat ="server" Text ="Login" ></ asp:Button >
< asp:Button id ="Reset" runat ="server" Text ="Reset" ></ asp:Button ></ TD >
< td ></ td >
< td ></ td >
</ TR >
</ TABLE >
</ td >
< td ></ td >
</ tr >
</ table >
</ form >
</ body >
</ HTML >
3.Login页面的cs代码
< HEAD >
< title > Login </ title >
< meta name ="GENERATOR" Content ="Microsoft Visual Studio .NET 7.1" >
< meta name ="CODE_LANGUAGE" Content ="C#" >
< meta name ="vs_defaultClientScript" content ="JavaScript" >
< meta name ="vs_targetSchema" content ="http://schemas.microsoft.com/intellisense/ie5" >
</ HEAD >
< body >
< form id ="Form1" method ="post" runat ="server" >
< table align ="center" cellSpacing ="0" cellPadding ="0" width ="100%" border ="0" height ="100%" >
< colgroup >
< col width ="30%" >
</ col >
< col width ="40%" >
</ col >
< col width ="30%" >
</ col >
</ colgroup >
< tr >
< td ></ td >
< td valign ="middle" >
< TABLE id ="Table1" align ="center" cellSpacing ="0" cellPadding ="0" width ="100%" border ="0" >
< colgroup >
< col width ="20%" >
</ col >
< col width ="35%" >
</ col >
< col width ="25%" >
</ col >
< col width ="20%" >
</ col >
</ colgroup >
< TR >
< TD align ="right" > LoginName </ TD >
< TD >
< asp:TextBox id ="txtLoginName" runat ="server" Width ="100%" ></ asp:TextBox ></ TD >
< td ></ td >
< td ></ td >
</ TR >
< TR >
< TD align ="right" > Password </ TD >
< TD >
< asp:TextBox id ="txtPassword" runat ="server" Width ="100%" ></ asp:TextBox ></ TD >
< td ></ td >
< td ></ td >
</ TR >
< TR >
< TD align ="right" > ValidateCode </ TD >
< TD >
< asp:TextBox id ="txtValidateCode" runat ="server" Width ="100%" ></ asp:TextBox ></ TD >
< td >< IMG alt ="" src ="Pages/Wonderful/Form/ValidateCode.aspx" ></ td >
< td ></ td >
</ TR >
< TR >
< TD ></ TD >
< TD >
< asp:Button id ="Login" runat ="server" Text ="Login" ></ asp:Button >
< asp:Button id ="Reset" runat ="server" Text ="Reset" ></ asp:Button ></ TD >
< td ></ td >
< td ></ td >
</ TR >
</ TABLE >
</ td >
< td ></ td >
</ tr >
</ table >
</ form >
</ body >
</ HTML >
public
class
Login : System.Web.UI.Page
{
protected System.Web.UI.WebControls.TextBox txtLoginName;
protected System.Web.UI.WebControls.TextBox txtPassword;
protected System.Web.UI.WebControls.TextBox txtValidateCode;
protected System.Web.UI.WebControls.Button Login;
protected System.Web.UI.WebControls.Button Reset;
protected System.Web.UI.WebControls.TextBox TextBox1;
private void Page_Load(object sender, System.EventArgs e)
{
if(Session["VNum"]!=null && Session["VNum"].ToString()==txtValidateCode.Text)
{
//验证码正确
}
}
Web Form Designer generated code#region Web Form Designer generated code
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: This call is required by the ASP.NET Web Form Designer.
//
InitializeComponent();
base.OnInit(e);
}
/**//// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.Load += new System.EventHandler(this.Page_Load);
}
#endregion
}
{
protected System.Web.UI.WebControls.TextBox txtLoginName;
protected System.Web.UI.WebControls.TextBox txtPassword;
protected System.Web.UI.WebControls.TextBox txtValidateCode;
protected System.Web.UI.WebControls.Button Login;
protected System.Web.UI.WebControls.Button Reset;
protected System.Web.UI.WebControls.TextBox TextBox1;
private void Page_Load(object sender, System.EventArgs e)
{
if(Session["VNum"]!=null && Session["VNum"].ToString()==txtValidateCode.Text)
{
//验证码正确
}
}
Web Form Designer generated code#region Web Form Designer generated code
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: This call is required by the ASP.NET Web Form Designer.
//
InitializeComponent();
base.OnInit(e);
}
/**//// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.Load += new System.EventHandler(this.Page_Load);
}
#endregion
}