标签:
log.aspx
<html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> <title></title> <script src="../../Scripts/jquery-1.4.1.min.js" type="text/javascript"></script>//
//可以使用微软自带的jqury.js 下面jquery-1.4.1.min.js版本之上的。或者在jquery官网上下载就可以。 <script type="text/javascript" language="javascript"> //重新获取验证字符 function changeImage() { //单击触发图片重载事件,完成图片验证码的更换 document.getElementById("img").src = document.getElementById("img").src + ‘?‘; //其中imgRandom为验证码图片的id。 } </script> </head> <body> <form id="form1" runat="server"> <div> <asp:TextBox ID="txtcode" runat="server"></asp:TextBox> <img src="ValidateCode.aspx" id="img" onclick="f_refreshtype()" width="50px"/> <a href="javascript:changeImage();">看不清?</a> <a href="#" onclick="img.src=‘ValidateCode.aspx?t=‘+(new Date().getTime());return false;">看不清,换一张?</a> <asp:Button ID="Button1" runat="server" Text="Button" onclick="Button1_Click" /> </div> </form> </body> </html>
log.aspx.cs
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; public partial class mb_yanzhengma_log : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { } protected void Button1_Click(object sender, EventArgs e) { string usercode = txtcode.Text.Trim(); if (usercode == Session["vcode"].ToString())//Session["vcode"] { Response.Write("OK,正确"); } else { Response.Write("验证码不符合"); } } }
新建ValidateCode.aspx页面
ValidateCode.aspx.cs
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; using System.Drawing; using System.Drawing.Imaging; public partial class mb_yanzhengma_ValidateCode : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { string validateCode = CreateValidateCode();//生成验证码 Bitmap bitmap = new Bitmap(imgWidth, imgHeight);//生成Bitmap图像 DisturbBitmap(bitmap); //图像背景 DrewValidateCode(bitmap, validateCode);//绘制验证码图像 bitmap.Save(Response.OutputStream, ImageFormat.Gif);//保存图像,等待输出 } private int codeLen = 4;//验证码长度 private int fineness = 85;//图片清晰度 private int imgWidth = 48;//图片宽度 private int imgHeight = 24;//图片高度 private string fontFamily = "Times New Roman";//字体名称 private int fontSize = 14;//字体大小 //private int fontStyle = 0;//字体样式 private int posX = 0;//绘制起始坐标X private int posY = 0;//绘制坐标Y private string CreateValidateCode() //生成验证码 { string validateCode = ""; Random random = new Random();// 随机数对象 for (int i = 0; i < codeLen; i++)//循环生成每位数值 { int n = random.Next(10);//数字 validateCode += n.ToString(); } Session["vcode"] = validateCode;//保存验证码 这Session是在前台调用的。 return validateCode;// 返回验证码 } private void DisturbBitmap(Bitmap bitmap)//图像背景 { Random random = new Random();//通过随机数生成 for (int i = 0; i < bitmap.Width; i++)//通过循环嵌套,逐个像素点生成 { for (int j = 0; j < bitmap.Height; j++) { if (random.Next(90) <= this.fineness) bitmap.SetPixel(i, j, Color.LightGray); } } } private void DrewValidateCode(Bitmap bitmap, string validateCode)//绘制验证码图像 { Graphics g = Graphics.FromImage(bitmap);//获取绘制器对象 Font font = new Font(fontFamily, fontSize, FontStyle.Bold);//设置绘制字体 g.DrawString(validateCode, font, Brushes.Black, posX, posY);//绘制验证码图像 } }
标签:
原文地址:http://www.cnblogs.com/huichao1314/p/5480968.html