功能说明:
生成六位随机图片验证码(包括数字 大小写字母 部分汉字)
-------------------------调用代码---------------------------
-----------------------------------------------------------------
------------code.aspx.cs文件代码-----------------------
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Drawing.Imaging;
using System.IO;
namespace Bino.WebGB
{
///
/// code 的摘要说明。
///
public class code : System.Web.UI.Page
{
private void Page_Load(object sender, System.EventArgs e)
{
// 在此处放置用户代码以初始化页面
Response.Cache.SetNoStore();
string strNum = Rndnum();
Session["Num"] = strNum;
ValidateCode(strNum);
}
#region Web 窗体设计器生成的代码
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
//
InitializeComponent();
base.OnInit(e);
}
///
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
///
private void InitializeComponent()
{
this.Load += new System.EventHandler(this.Page_Load);
}
#endregion
#region 生成验证码
public string Rndnum()
{
int j1;
string strChoice="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,中,长,沙,深,圳,方,广,德,网,络,东,法,家,人,科,技,游,戏,战,国,英,雄,抗";
string [] strResult = strChoice.Split(new Char [] {','});
string strReturn = "";
Random rnd = new Random(unchecked((int)DateTime.Now.Ticks));
for(int i=0;i<6;i++)
{
Random rnd1 = new Random(rnd.Next()*unchecked((int)DateTime.Now.Ticks));
j1 = rnd.Next(85);
rnd = new Random(rnd.Next()*unchecked((int)DateTime.Now.Ticks));
strReturn = strReturn + strResult[j1].ToString();
}
return strReturn;
}
public void ValidateCode(string vnum)
{
MemoryStream ms = new MemoryStream();
double Height = (vnum.Length) * 15;
int gHeight = Convert.ToInt32(Height);
Bitmap img = new Bitmap(gHeight,20);
Graphics g = Graphics.FromImage(img);
g.DrawString(vnum,(new Font("宋体",13)),(new SolidBrush(Color.Blue)),1,4);
img.Save(ms,ImageFormat.Png);
Response.ClearContent();
Response.ContentType = "image/Bmp";
Response.BinaryWrite(ms.ToArray());
g.Dispose();
Response.End();
}
#endregion
}
}
--------------------------------------------------------------------