private
void
Page_Load(
object
sender, System.EventArgs e)
... {
// 在此处放置用户代码以初始化页面
//if(allfresh)
if(!this.IsPostBack)...{
this.valicode();
}
}
private void valicode()
... {
Session["rndcode"] = this.RndNum();
this.ValidateCode(Session["rndcode"].ToString());
}
public string RndNum()
... {
int j;
string choice = "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";
string[] result = choice.Split(new char[]...{','});
string strReturn = "";
Random rd = new Random(unchecked((int)DateTime.Now.Ticks));
for(int i = 0; i<4; i++)
...{
Random rd1 = new Random(rd.Next()*unchecked((int)DateTime.Now.Ticks));
j = rd1.Next(35);
rd = new Random(rd.Next()*unchecked((int)DateTime.Now.Ticks));
strReturn = strReturn + result[j].ToString();
}
return strReturn;
}
public void ValidateCode( string num)
... {
double height = (num.Length)*15;
int gheight = Convert.ToInt32(height);
Bitmap img = new Bitmap(gheight,21);
Graphics g = Graphics.FromImage(img);
g.DrawString(num,(new Font("幼圆",13)),(new SolidBrush(Color.Blue)),1,4);
img.Save(Server.MapPath("img")+"/q.png",ImageFormat.Png);
// Response.ClearContent();
// Response.ContentType = "image/Bmp";
// Response.BinaryWrite(ms.ToArray());
g.Dispose();
img.Dispose();
// Response.End();
}
... {
// 在此处放置用户代码以初始化页面
//if(allfresh)
if(!this.IsPostBack)...{
this.valicode();
}
}
private void valicode()
... {
Session["rndcode"] = this.RndNum();
this.ValidateCode(Session["rndcode"].ToString());
}
public string RndNum()
... {
int j;
string choice = "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";
string[] result = choice.Split(new char[]...{','});
string strReturn = "";
Random rd = new Random(unchecked((int)DateTime.Now.Ticks));
for(int i = 0; i<4; i++)
...{
Random rd1 = new Random(rd.Next()*unchecked((int)DateTime.Now.Ticks));
j = rd1.Next(35);
rd = new Random(rd.Next()*unchecked((int)DateTime.Now.Ticks));
strReturn = strReturn + result[j].ToString();
}
return strReturn;
}
public void ValidateCode( string num)
... {
double height = (num.Length)*15;
int gheight = Convert.ToInt32(height);
Bitmap img = new Bitmap(gheight,21);
Graphics g = Graphics.FromImage(img);
g.DrawString(num,(new Font("幼圆",13)),(new SolidBrush(Color.Blue)),1,4);
img.Save(Server.MapPath("img")+"/q.png",ImageFormat.Png);
// Response.ClearContent();
// Response.ContentType = "image/Bmp";
// Response.BinaryWrite(ms.ToArray());
g.Dispose();
img.Dispose();
// Response.End();
}