验证码(网上找来的)

ContractedBlock.gif ExpandedBlockStart.gif Code
  1using System;
  2using System.Collections;
  3using System.Configuration;
  4using System.Data;
  5using System.Linq;
  6using System.Threading;
  7using System.Web;
  8using System.Web.Security;
  9using System.Web.UI;
 10using System.Web.UI.HtmlControls;
 11using System.Web.UI.WebControls;
 12using System.Web.UI.WebControls.WebParts;
 13using System.Xml.Linq;
 14using System.Drawing;
 15using System.Drawing.Imaging;
 16using System.Drawing.Drawing2D;
 17
 18public partial class CheckCode : System.Web.UI.Page
 19ExpandedBlockStart.gifContractedBlock.gif{
 20    private void Page_Load(object sender, System.EventArgs e)
 21ExpandedSubBlockStart.gifContractedSubBlock.gif    {
 22        string checkCode = CreateRandomCode(4);
 23        Session["CheckCode"= checkCode;
 24        CreateImage(checkCode);
 25    }

 26    private string CreateRandomCode(int codeCount)
 27ExpandedSubBlockStart.gifContractedSubBlock.gif    {
 28        string allChar = "1,2,3,4,5,6,7,8,9,A,B,C,D,E,F,G,H,I,J,K,L,M,N,P,Q,R,S,T,U,W,X,Y,Z,a,b,c,d,e,f,g,h,i,j,k,m,n,p,q,r,s,t,u,v,w,x,y,z";
 29        string[] allCharArray = allChar.Split(',');
 30        string randomCode = "";
 31        int temp = -1;
 32
 33        Random rand = new Random();
 34        for (int i = 0; i < codeCount; i++)
 35ExpandedSubBlockStart.gifContractedSubBlock.gif        {
 36            if (temp != -1)
 37ExpandedSubBlockStart.gifContractedSubBlock.gif            {
 38                rand = new Random(i * temp * ((int)DateTime.Now.Ticks));
 39            }

 40            int t = rand.Next(35);
 41            if (temp == t)
 42ExpandedSubBlockStart.gifContractedSubBlock.gif            {
 43                return CreateRandomCode(codeCount);
 44            }

 45            temp = t;
 46            randomCode += allCharArray[t];
 47        }

 48        return randomCode;
 49    }

 50    private void CreateImage(string checkCode)
 51ExpandedSubBlockStart.gifContractedSubBlock.gif    {
 52        int iwidth = (int)(checkCode.Length * 13);
 53        System.Drawing.Bitmap image = new System.Drawing.Bitmap(iwidth, 23);
 54        Graphics g = Graphics.FromImage(image);
 55        g.Clear(Color.White);
 56        //定义颜色
 57ExpandedSubBlockStart.gifContractedSubBlock.gif        Color[] c = { Color.Black, Color.Red, Color.DarkBlue, Color.Green, Color.Orange, Color.Brown, Color.DarkCyan, Color.Purple };
 58        //定义字体 
 59ExpandedSubBlockStart.gifContractedSubBlock.gif        string[] font = "Verdana""Microsoft Sans Serif""Comic Sans MS""Arial""宋体" };
 60        Random rand = new Random();
 61        //随机输出噪点
 62        for (int i = 0; i < 50; i++)
 63ExpandedSubBlockStart.gifContractedSubBlock.gif        {
 64            int x = rand.Next(image.Width);
 65            int y = rand.Next(image.Height);
 66            g.DrawRectangle(new Pen(Color.LightGray, 0), x, y, 11);
 67        }

 68
 69        //输出不同字体和颜色的验证码字符
 70        for (int i = 0; i < checkCode.Length; i++)
 71ExpandedSubBlockStart.gifContractedSubBlock.gif        {
 72            int cindex = rand.Next(7);
 73            int findex = rand.Next(5);
 74
 75            Font f = new System.Drawing.Font(font[findex], 10, System.Drawing.FontStyle.Bold);
 76            Brush b = new System.Drawing.SolidBrush(c[cindex]);
 77            int ii = 4;
 78            if ((i + 1% 2 == 0)
 79ExpandedSubBlockStart.gifContractedSubBlock.gif            {
 80                ii = 2;
 81            }

 82            g.DrawString(checkCode.Substring(i, 1), f, b, 3 + (i * 12), ii);
 83        }

 84        //画一个边框
 85        g.DrawRectangle(new Pen(Color.Black, 0), 00, image.Width - 1, image.Height - 3);
 86
 87        //输出到浏览器
 88        System.IO.MemoryStream ms = new System.IO.MemoryStream();
 89        image.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);
 90        HttpContext.Current.Response.ClearContent();
 91        //Response.ClearContent();
 92        HttpContext.Current.Response.ContentType = "image/Jpeg";
 93        HttpContext.Current.Response.BinaryWrite(ms.ToArray());
 94        g.Dispose();
 95        image.Dispose();
 96
 97    }

 98}

 99
100

调用形式:

 

ContractedBlock.gif ExpandedBlockStart.gif Code
1    <tr><td><strong>请输入验证码:</strong></td>
2        <td colspan="2"><asp:TextBox ID="CheckCode" runat="server" MaxLength ="4"   ></asp:TextBox></td>
3        <td colspan="1">
4            <asp:Image Runat="server" ID="ImageCheck" ImageUrl="~/CheckCode.aspx"></asp:Image>
5        </td>
6    </tr>
7    <tr><td colspan="1"></td><td colspan="2"><span style="color:Red">*验证码区别大小写</span></td>
8        <td><asp:LinkButton id="ChangeOther" runat="server" text="看不清换一张" CausesValidation="false"></asp:LinkButton> </td>
9    </tr>

转载于:https://www.cnblogs.com/ning-ning/archive/2008/11/03/1325233.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值