验证码生成文件:validate.aspx.cs
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.IO;
using System.Drawing;
using System.Drawing.Imaging;
using System.Drawing.Drawing2D;
public partial class validate : System.Web.UI.Page
{
private Bitmap validateimage;
private Graphics g;
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
string VNum = MakeValidateCode();
Session["VNum"] = VNum;
ValidateCode(VNum);
}
}
public void ValidateCode(string VNum)
{
validateimage = new Bitmap(60, 15, PixelFormat.Format24bppRgb);
g = Graphics.FromImage(validateimage);
g.FillRectangle(new LinearGradientBrush(new Point(0, 0), new Point(110, 20), Color.FromArgb(240, 255, 255, 255), Color.FromArgb(240, 255, 255, 255)), 0, 0, 200, 200);
g.DrawString(VNum, new Font("arial", 11), new SolidBrush(Color.Red), new PointF(6, 0));
g.Save();
MemoryStream ms = new MemoryStream();
validateimage.Save(ms, System.Drawing.Imaging.ImageFormat.Gif);
Response.ClearContent();
Response.ContentType = "image/bmp";
Response.BinaryWrite(ms.ToArray());
Response.End();
}
string MakeValidateCode()
{
char[] s = new char[]{'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 num = "";
Random r = new Random();
for (int i = 0; i < 4; i++)
{
num += s[r.Next(0, s.Length)].ToString();
}
return num;
}
}
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.IO;
using System.Drawing;
using System.Drawing.Imaging;
using System.Drawing.Drawing2D;
public partial class validate : System.Web.UI.Page
{
private Bitmap validateimage;
private Graphics g;
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
string VNum = MakeValidateCode();
Session["VNum"] = VNum;
ValidateCode(VNum);
}
}
public void ValidateCode(string VNum)
{
validateimage = new Bitmap(60, 15, PixelFormat.Format24bppRgb);
g = Graphics.FromImage(validateimage);
g.FillRectangle(new LinearGradientBrush(new Point(0, 0), new Point(110, 20), Color.FromArgb(240, 255, 255, 255), Color.FromArgb(240, 255, 255, 255)), 0, 0, 200, 200);
g.DrawString(VNum, new Font("arial", 11), new SolidBrush(Color.Red), new PointF(6, 0));
g.Save();
MemoryStream ms = new MemoryStream();
validateimage.Save(ms, System.Drawing.Imaging.ImageFormat.Gif);
Response.ClearContent();
Response.ContentType = "image/bmp";
Response.BinaryWrite(ms.ToArray());
Response.End();
}
string MakeValidateCode()
{
char[] s = new char[]{'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 num = "";
Random r = new Random();
for (int i = 0; i < 4; i++)
{
num += s[r.Next(0, s.Length)].ToString();
}
return num;
}
}
在页面中调用:
<img src="../validate.aspx" id="yzm" onclick="this.src='../validate.aspx?abc='+Math.random()" alt="图片看不清?点击重新得到验证码" style="cursor:hand;" />
注意:有一点要说明,当点击刷新时,validate.asp?后面一定要有一个参数,若没有这个无效的参数,程序会认为你请求的是同一个文件,因而不会刷新的。这里的参数abc是个随意的,只起改变请求文件的作用。