1、简单验证码:
<img src="ValidateCode.ashx?" id="vcode" onclick="ChangeCode()" />
<a href="javascript:ChangeCode();">看不清换一张</a>
<script type="text/javascript">
function ChangeCode() {
var src1 = document.getElementById("vcode").src;
document.getElementById("vcode").src = src1+1;
}
</script>
public void ProcessRequest(HttpContext context)
{
context.Response.ContentType = "text/plain";
Bitmap bm = new Bitmap(400, 120);
Graphics g = Graphics.FromImage(bm);
string src = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
string des = "";
Random r = new Random();
for (int i = 0; i < 6; i++)
{
des += src[r.Next(src.Length)];
}
g.Clear(Color.White);
g.DrawRectangle(new Pen(Color.Black), 10, 10, 380, 100);
g.DrawString(des, new Font("微软雅黑", 60), new SolidBrush(Color.Green), 0, 0);
bm.Save(context.Response.OutputStream, ImageFormat.Jpeg);
}
2、使用封装好的验证码操作类
ValidateCode
public void ProcessRequest(HttpContext context)
{
context.Response.ContentType = "text/plain";
ValidateCodeHelper vcode = new ValidateCodeHelper();
string strvcode = vcode.CreateValidateCode(6);
vcode.CreateValidateGraphic(strvcode, context);
}