兑现复杂的证验码

Default.aspx代码 复制代码
  1.   
  2. <%@ Page Language="C#" AutoEventWireup="true"  CodeFile="Default.aspx.cs" Inherits="_Default" %>   
  3.   
  4. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">   
  5.   
  6. <html xmlns="http://www.w3.org/1999/xhtml" >   
  7. <head runat="server">   
  8.     <title>无标题页</title>   
  9.        
  10.     <script language="javascript">   
  11.       //验证码:读取cookie中的值   
  12.         function readCookie()   
  13.         {   
  14.                
  15.             //判断是否存在cookie   
  16.             var isExist = document.cookie.indexOf("imgRandom=");   
  17.             //alert(isExist);   
  18.             if(isExist!=-1)   
  19.             {   
  20.                 //“imgRandom=”后面的等号的位置   
  21.                 var c=  document.cookie.indexOf("=",isExist)+1;   
  22.                 //获取等号后面的字符串,也就是所需要的验证码!   
  23.                 var str = document.cookie.substring(c)   
  24.                 //alert(document.cookie.substring(c));   
  25.                    
  26.                 if(document.getElementById("Image").value!=str)   
  27.                 {   
  28.                         
  29.                      document.getElementById("Image").select();   
  30.                   
  31.                      alert("验证码输入错误!")   
  32.                      return false;   
  33.                 }else   
  34.                 {   
  35.                  return true;   
  36.                 }   
  37.             }   
  38.         }   
  39.          
  40.         //onload = readCookie;   
  41.         //获取图片的src   
  42.         function imgSrc()   
  43.         {   
  44.               
  45.            
  46.             // 通过链接让图片中的src发生变化   
  47.            document.getElementById("imgRandom").src = document.getElementById("imgRandom").src + "?abc=" + Math.random();   
  48.   
  49.             
  50.         }   
  51.            
  52.        
  53.     
  54.     </script>   
  55. </head>   
  56. <body>   
  57.   
  58. <form name="vcode" action="Default.aspx" method="post" onsubmit=" readCookie()">   
  59.    验证码 :  <input  id="Image" type="text" name="checkImage" style="width: 72px; height:20px" /><span style="vertical-align:bottom"><img id="imgRandom" src="Randomimage.aspx" alt="" /></span><a href="javascript:imgSrc()">看不清换一张</a><br />   
  60.       
  61.    <input type="submit" value="确定" style="border:solid 1px black" />   
  62.    </form>   
  63. </body>   
  64. </html>  



Default.aspx.cs代码 复制代码
  1.   
  2. using System;   
  3. using System.Data;   
  4. using System.Configuration;   
  5. using System.Web;   
  6. using System.Web.Security;   
  7. using System.Web.UI;   
  8. using System.Web.UI.WebControls;   
  9. using System.Web.UI.WebControls.WebParts;   
  10. using System.Web.UI.HtmlControls;   
  11.   
  12. public partial class _Default : System.Web.UI.Page    
  13. {   
  14.     protected void Page_Load(object sender, EventArgs e)   
  15.     {   
  16.   
  17.     }   
  18. }  

 

Randomimage.aspx代码 复制代码
  1.   
  2. <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Randomimage.aspx.cs" Inherits="Randomimage" %>  


Randomimage.aspx.cs代码 复制代码
  1.   
  2. using System;   
  3. using System.Data;   
  4. using System.Configuration;   
  5. using System.Collections;   
  6. using System.Web;   
  7. using System.Web.Security;   
  8. using System.Drawing;   
  9. using System.IO;   
  10.   
  11.   
  12. public partial class Randomimage :System.Web.UI.Page   
  13. {   
  14.     public void Page_Load(object sender, EventArgs e)   
  15.     {   
  16.         string random = this.random();   
  17.         /*   
  18.          * 将随机数字添加到cookie中   
  19.          */   
  20.           
  21.         //添加cookie   
  22.         //imgCookie.Value = random;   
  23.         //this.Response.Cookies.Add(imgCookie);   
  24.         //this.Response.Write(imgCookie.Value);   
  25.   
  26.   
  27.         /*   
  28.          * 图片加载的时候,在图片上写随机数   
  29.          */   
  30.         int width = Convert.ToInt32(random.Length * 25);    
  31.         using (Bitmap image = new Bitmap(width, 40))   
  32.         {   
  33.             //应用画板,在画板上书写数字   
  34.             using (Graphics gp = Graphics.FromImage(image))   
  35.             {   
  36.                 //用字符串的形式书写数字   
  37.                 //gp.DrawString(random, new Font("宋体"24), Brushes.Red, new PointF(20.0F, 10.0F));   
  38.                 gp.Clear(Color.White);   
  39.                 Font font = new Font("隶书"24, FontStyle.Bold);   
  40.                 System.Drawing.Drawing2D.LinearGradientBrush brush =   
  41.                 new System.Drawing.Drawing2D.LinearGradientBrush(new Rectangle(00, image.Width, image.Height), Color.DarkOrchid, Color.Blue, 3.5f, true);   
  42.                 gp.DrawString(random, font, brush, 32);   
  43.                 this.drawLine(gp, image);   
  44.   
  45.             }   
  46.             //显示图片   
  47.             //image.Save(this.Response.OutputStream, System.Drawing.Imaging.ImageFormat.Jpeg);   
  48.             MemoryStream ms = new MemoryStream();   
  49.             image.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);   
  50.               
  51.             //输出图片   
  52.             Response.ClearContent();   
  53.             Response.ContentType = "text/html";   
  54.             Response.BinaryWrite(ms.ToArray());   
  55.         }   
  56.         HttpCookie imgCookie = null;   
  57.         if (this.Request.Cookies["imgRandom"] == null)   
  58.         {   
  59.             imgCookie = new HttpCookie("imgRandom");   
  60.             imgCookie.Value = random;   
  61.             this.Response.Cookies.Add(imgCookie);   
  62.             //this.Response.Redirect("image.aspx");   
  63.         }   
  64.         else   
  65.         {   
  66.             imgCookie = new HttpCookie("imgRandom");   
  67.             imgCookie.Value = random;   
  68.             this.Response.Cookies.Add(imgCookie);   
  69.         }   
  70.     }   
  71.     /*   
  72.      * 随机数字的获取   
  73.      */   
  74.     public string random()   
  75.     {   
  76.         //string number = "";   
  77.         //Random random = new Random();   
  78.   
  79.         //for (int i = 0; i < 4; i++)   
  80.         //{   
  81.         //    number += (random.Next(010)).ToString();   
  82.         //}   
  83.         string guid = Guid.NewGuid().ToString();   
  84.         string rtuStr = Guid.NewGuid().ToString().Substring(14);   
  85.             
  86.             
  87.         return rtuStr;   
  88.     }   
  89.     private void drawPoint(Bitmap img)   
  90.     {   
  91.         Random ran = new Random();   
  92.         int col = ran.Next();//在一次的图片中杂店颜色相同   
  93.         for (int i = 0; i < 100; i++)   
  94.         {   
  95.             int x = ran.Next(img.Width);   
  96.             int y = ran.Next(img.Height);   
  97.             img.SetPixel(x, y, Color.FromArgb(col));   
  98.         }   
  99.     }   
  100.     private void drawLine(Graphics gfc, Bitmap img)   
  101.     {   
  102.         Random ran = new Random();   
  103.         //选择画10条线,也可以增加,也可以不要线,只要随机杂点即可   
  104.         for (int i = 0; i < 10; i++)   
  105.         {   
  106.             int x1 = ran.Next(img.Width);   
  107.             int y1 = ran.Next(img.Height);   
  108.             int x2 = ran.Next(img.Width);   
  109.             int y2 = ran.Next(img.Height);   
  110.             gfc.DrawLine(new Pen(Color.Silver), x1, y1, x2, y2); //注意画笔一定要浅颜色,否则验证码看不清楚   
  111.         }   
  112.     }   
  113. }  

本文来源:
我的异常网
Java Exception
Dotnet Exception
Oracle Exception

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值