//定义方法
public partial class VerificationCode : System.Web.UI.Page
{
string ImagePath = "images/validator.jpg";
string sValidator = "";
protected void Page_Load(object sender, EventArgs e)
{
validate();
}
public void validate()
{
if (Request.Params["Validator"] != null)
{
sValidator = Request.Params["Validator"].ToString();
}
///创建Bmp位图
Bitmap bitMapImage = new Bitmap(Server.MapPath(ImagePath));
Graphics graphicImage = Graphics.FromImage(bitMapImage);
///设置画笔的输出模式
graphicImage.SmoothingMode = SmoothingMode.AntiAlias;
///添加文本字符串
graphicImage.DrawString(sValidator, new Font("Arial", 20, FontStyle.Bold), SystemBrushes.WindowText, new Point(0, 0));
///设置图像输出的格式
Response.ContentType = "image/jpeg";
///保存数据流
bitMapImage.Save(Response.OutputStream, ImageFormat.Jpeg);
///释放占用的资源
graphicImage.Dispose();
bitMapImage.Dispose();
}
}
---------------------------------------------------------------------------------------------------------------------------------------------------------
//调用
public partial class login : System.Web.UI.Page
{
static string sValidator = "";
private readonly string sValidatorImageUrl = "VerificationCode.aspx?Validator=";
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
sValidator = GetRandomint();
ValidateImage.ImageUrl = sValidatorImageUrl + sValidator;
}
}
private String GetRandomint()
{
Random random = new Random();
return (random.Next(100000, 999999).ToString());
}