Font font = new System Drawing Font( Arial (System Drawing FontStyle Bold | System Drawing FontStyle Italic)) System Drawing Drawing D LinearGradientBrush brush = new System Drawing Drawing D LinearGradientBrush(new Rectangle( image Width image Height) Color Blue Color DarkRed f true) g DrawString(checkCode font brush ) //画图片的前景噪音点 for(int i= i<i++) { int x = random Next(image Width) int y = random Next(image Height) image SetPixel(x y Color FromArgb(random Next())) } //画图片的边框线 g DrawRectangle(new Pen(Color Silver) image Width image Height ) System IO MemoryStream ms = new System IO MemoryStream() image Save(ms System Drawing Imaging ImageFormat Gif) Response ClearContent() Response ContentType = image/Gif Response BinaryWrite(ms ToArray()) } finally { g Dispose() image Dispose() } } } 在你要显示验证码的窗体中添加一个image控件 并命名为imgCheckCode 用于显示验证码 再添加一个lable 并命名为lblMessage 用于显示错误信息 然后在该窗体的Page_Load中添加 this imgCheckCode ImageUrl = checkCode aspx 在登录页面的登录按钮的处理事件中使用以下代码判断验证码 private void btnLogin_Click(object sender System Web UI ImageClickEventArgs e) { if(Request Cookies[ checkCode ] == null) { lblMessage Text = 您的浏览器设置已被禁用 Cookies 您必须设置浏览器允许使用 Cookies 选项后才能使用本系统 lblMessage Visible = true return } if(String Compare(Request Cookies[ checkCode ] Value txtCheckCode Text true) != ) { lblMessage Text = 验证码错误 请输入正确的验证码 lblMessage Visible = true return } } 这样就能实现你所要的验证码验证了 lishixinzhi/Article/program/net/201311/13119
计算机验证码制作
方法/步骤
1/9分步阅读
在网页左侧有一个用户登录的界面,为防止恶意攻击,我们需要用户在登录的时候输入验证码,本节我们就讲述一下如何制作验证码图片
2/9
代码【<img id="Img1" src="image.aspx" alt="" width ="50px" height ="18px"/>】我们用图片标签,链接一个验证码页面
3/9
在【new】文件夹上单击右键,选择【新建项】
4/9
选择【Web窗体】命名为【image.aspx】点击【添加】
5/9
Web 窗体设计器生成的代码override protected void OnInit(EventArgs e){//// CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。//InitializeComponent() base.OnInit(e) }
/// <summary> /// 设计器支持所需的方法 - 不要使用代码编辑器修改/// 此方法的内容。/// </summary> private void InitializeComponent(){this.Load += new System.EventHandler(this.Page_Load) }
6/9
生成数字和字母的代码
【
private string CreateAuthStr(int len){int number StringBuilder code = new StringBuilder() string checkCode = String.Empty
Random random = new Random()
for (int i = 0i <leni++){number = random.Next()
if (number % 2 == 0){code.Append((char)('0' + (char)(number % 10))) }else{code.Append((char)('A' + (char)(number % 26))) }}
checkCode = code.ToString()
Response.Cookies.Add(new HttpCookie("CheckCode", checkCode))
return checkCode.ToString() }
】
7/9
为了防止被其他软件识别出验证码,我们需要增加图片的背景噪音线和前景噪音点
8/9
因为代码比较多,也特别复杂,天使直接附上代码,同学们直接用我的就行了
using Systemusing System.Datausing System.Configurationusing System.Collectionsusing System.Webusing System.Web.Securityusing System.Web.UIusing System.Web.UI.WebControlsusing System.Web.UI.WebControls.WebPartsusing System.Web.UI.HtmlControlsusing System.IOusing System.Drawingusing System.Drawing.Imagingusing System.Textusing System.Text.RegularExpressionspublic partial class _Default : System.Web.UI.Page {protected void Page_Load(object sender, EventArgs e){//this.CreateCheckCodeImage(CreateAuthStr(5)) this.CreateCheckCodeImage(CreateAuthStr(5, true))
}#region Web 窗体设计器生成的代码override protected void OnInit(EventArgs e){//// CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。//InitializeComponent() base.OnInit(e) }
/// <summary> /// 设计器支持所需的方法 - 不要使用代码编辑器修改/// 此方法的内容。/// </summary> private void InitializeComponent(){this.Load += new System.EventHandler(this.Page_Load) }#endregionprivate string CreateAuthStr(int len){int number StringBuilder code = new StringBuilder() string checkCode = String.Empty
Random random = new Random()
for (int i = 0i <leni++){number = random.Next()
if (number % 2 == 0){code.Append((char)('0' + (char)(number % 10))) }else{code.Append((char)('A' + (char)(number % 26))) }}
checkCode = code.ToString()
Response.Cookies.Add(new HttpCookie("CheckCode", checkCode))
return checkCode.ToString() }
private string CreateAuthStr(int len, bool OnlyNum){if (!OnlyNum){return CreateAuthStr(len) }int number string checkCode = String.Empty StringBuilder code = new StringBuilder()
Random random = new Random()
for (int i = 0i <leni++){number = random.Next() code.Append((char)('0' + (char)(number % 10))) }
checkCode = code.ToString()
Response.Cookies.Add(new HttpCookie("CheckCode", checkCode))
return checkCode.ToString() }
private void CreateCheckCodeImage(string checkCode){if (checkCode == null || checkCode.Trim() == String.Empty)return
Bitmap image = new Bitmap((int)Math.Ceiling((checkCode.Length * 12.5)), 22) Graphics g = Graphics.FromImage(image)
try{//生成随机生成器Random random = new Random()
//清空图片背景色g.Clear(Color.White)
//画图片的背景噪音线for (int i = 0i <25i++){int x1 = random.Next(image.Width) int x2 = random.Next(image.Width) int y1 = random.Next(image.Height) int y2 = random.Next(image.Height)
g.DrawLine(new Pen(Color.Silver), x1, y1, x2, y2) }
Font font = new System.Drawing.Font("Arial", 12, (System.Drawing.FontStyle.Bold | System.Drawing.FontStyle.Italic)) System.Drawing.Drawing2D.LinearGradientBrush brush = new System.Drawing.Drawing2D.LinearGradientBrush(new Rectangle(0, 0, image.Width, image.Height), Color.Blue, Color.DarkRed, 1.2f, true) g.DrawString(checkCode, font, brush, 2, 2)
//画图片的前景噪音点for (int i = 0i <100i++){int x = random.Next(image.Width) int y = random.Next(image.Height)
image.SetPixel(x, y, Color.FromArgb(random.Next())) }
//画图片的边框线g.DrawRectangle(new Pen(Color.Silver), 0, 0, image.Width - 1, image.Height - 1)
System.IO.MemoryStream ms = new System.IO.MemoryStream() image.Save(ms, System.Drawing.Imaging.ImageFormat.Gif) Response.ClearContent() Response.ContentType = "image/Gif" Response.BinaryWrite(ms.ToArray()) }finally{g.Dispose() image.Dispose() }}}
9/9
验证码已经生成,刷新一次自动生成一次
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)