---
title: "一个生成验证字符或数字的增强类(downmoon)(基于.net1.1)及调用示例"
author: "netxdiy"
date: "2007-01-19T13:59:00Z"
tags: []
slug: "downmoon-net1-1"
draft: false
lastmod: "2012-09-27T11:33:38Z"
id: 1687141
url: "https://netxdiy.iteye.com/blog/1687141"
---

![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)usingSystem;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)usingSystem.Drawing;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)usingSystem.Drawing.Imaging;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)usingSystem.Web.UI;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)usingSystem.Drawing.Drawing2D;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)usingSystem.IO;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)usingSystem.Text;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)usingSystem.Collections.Specialized;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)namespaceVegnet.Common.Framework.Util  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)/\*\*////&lt;summary&gt;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)///生成验证码的类  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)///&lt;/summary&gt;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)调用示例#region调用示例  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)//\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*调用示例\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\**  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)\*作者downnmoon(邀月)2006年11月\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\**  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)//呈现页面后台  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)privatevoidgetNumbers(intlen)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)ValidateNumber.CurrentLength=len;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)ValidateNumberAndChar.CurrentNumber=ValidateNumberAndChar.CreateValidateNumber(ValidateNumberAndChar.CurrentLength);  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)stringnumber=ValidateNumberAndChar.CurrentNumber;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)ValidateNumberAndChar.CreateValidateGraphic(this,number);  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)HttpCookiea=newHttpCookie("VegnetValidNumber2",number);  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)Response.Cookies.Add(a);  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)}  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)//验证页面前台  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)&lt;asp:TextBoxid="TextBox3"runat="server"&gt;&lt;/asp:TextBox&gt;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)&lt;asp:Labelid="Label3"runat="server"&gt;&lt;/asp:Label&gt;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)&lt;asp:Buttonid="Button3"runat="server"Text="验证"&gt;&lt;/asp:Button&gt;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)&lt;IMGsrc="DispValid.aspx"&gt;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)//验证页面后台  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)privatevoidButton3\_Click(objectsender,System.EventArgse)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)HttpCookieCollectioncookies=Request.Cookies;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)stringtmp=cookies\["VegnetValidNumber2"].Value;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)this.Label3.Text=(tmp.Trim().ToLower()==TextBox3.Text.Trim().ToLower())?"正确":"无效";  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)}  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*/  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)#endregion  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)publicclassValidateNumberAndChar  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)publicValidateNumberAndChar()  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)}  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)/\*\*////&lt;summary&gt;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)///验证码的最大长度  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)///&lt;/summary&gt;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)publicstaticintMaxLength  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)get...{return10;}  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)}  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)/\*\*////&lt;summary&gt;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)///验证码的最小长度  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)///&lt;/summary&gt;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)publicstaticintMinLength  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)get...{return1;}  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)}  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)/\*\*////&lt;summary&gt;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)///验证码的前缀  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)///&lt;/summary&gt;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)publicstaticstringBaseString  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)get  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)return"VegnetValidNumber";  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)}  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)}  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)/\*\*////&lt;summary&gt;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)///当前字符串长度，主要是为了更换图片时用  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)///&lt;/summary&gt;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)privatestaticint\_currentLength=5;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)publicstaticintCurrentLength  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)get  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)return\_currentLength;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)}  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)set  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)\_currentLength=value;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)}  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)}  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)/\*\*////&lt;summary&gt;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)///当前字符串，主要是为了更换图片时用  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)///&lt;/summary&gt;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)privatestaticstring\_currentNumber="";  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)publicstaticstringCurrentNumber  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)get  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)if(\_currentNumber!="")  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)return\_currentNumber;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)}  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)else  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)returnCreateValidateNumber(CurrentLength);  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)}  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)}  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)set  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)\_currentNumber=value;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)}  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)}  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)/\*\*////&lt;summary&gt;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)///合法字符列表  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)///&lt;/summary&gt;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)privatestaticstringstrLetters  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)get  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)return"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890";  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)}  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)}  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)/\*\*////&lt;summary&gt;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)///生成验证码  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)///&lt;/summary&gt;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)///&lt;paramname="length"&gt;指定验证码的长度&lt;/param&gt;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)///&lt;returns&gt;&lt;/returns&gt;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)publicstaticstringCreateValidateNumber(intlength)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)int\[]randMembers=newint\[length];  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)int\[]validateNums=newint\[length];  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)stringvalidateNumberStr="";  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)//生成起始序列值  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)intseekSeek=unchecked((int)DateTime.Now.Ticks);  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)RandomseekRand=newRandom(seekSeek);  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)intbeginSeek=(int)seekRand.Next(0,Int32.MaxValue-length\*10000);  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)int\[]seeks=newint\[length];  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)for(inti=0;i&lt;length;i++)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)beginSeek+=10000;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)seeks\[i]=beginSeek;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)}  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)//生成随机数字  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)for(inti=0;i&lt;length;i++)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)Randomrand=newRandom(seeks\[i]);  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)intpownum=1\*(int)Math.Pow(10,length);  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)randMembers\[i]=rand.Next(pownum,Int32.MaxValue);  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)}  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)//抽取随机数字  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)for(inti=0;i&lt;length;i++)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)stringnumStr=randMembers\[i].ToString();  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)intnumLength=numStr.Length;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)Randomrand=newRandom();  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)intnumPosition=rand.Next(0,numLength-1);  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)validateNums\[i]=Int32.Parse(numStr.Substring(numPosition,1));  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)}  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)//生成验证码  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)for(inti=0;i&lt;length;i++)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)validateNumberStr+=validateNums\[i].ToString();  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)}  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)returnvalidateNumberStr;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)}  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)/\*\*////&lt;summary&gt;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)///生成验证码（依据合法字符列表随机生成)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)///&lt;/summary&gt;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)///&lt;paramname="length"&gt;&lt;/param&gt;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)///&lt;returns&gt;&lt;/returns&gt;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)publicstaticstringCreateValidateChar(intlength)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)StringBuilders=newStringBuilder();  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)//生成起始序列值  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)intseekSeek=unchecked((int)DateTime.Now.Ticks);  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)RandomseekRand=newRandom(seekSeek);  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)intbeginSeek=(int)seekRand.Next(0,Int32.MaxValue-length\*10000);  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)int\[]seeks=newint\[length];  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)for(inti=0;i&lt;length;i++)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)beginSeek+=10000;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)seeks\[i]=beginSeek;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)}  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)//将随机生成的字符串绘制到图片上  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)for(inti=0;i&lt;length;i++)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)Randomr=newRandom(seeks\[i]);  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)s.Append(strLetters.Substring(r.Next(0,strLetters.Length-1),1));  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)}  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)returns.ToString();  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)}  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)/\*\*////&lt;summary&gt;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)///创建验证码的图片  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)///&lt;/summary&gt;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)///&lt;paramname="containsPage"&gt;要输出到的page对象&lt;/param&gt;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)///&lt;paramname="validateNum"&gt;验证码&lt;/param&gt;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)publicstaticvoidCreateValidateGraphic(PagecontainsPage,stringvalidateNum)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)Bitmapimage=newBitmap((int)Math.Ceiling(validateNum.Length\*15),25);  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)Graphicsg=Graphics.FromImage(image);  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)try  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)//生成随机生成器  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)Randomrandom=newRandom();  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)//清空图片背景色  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)g.Clear(Color.White);  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)//画图片的干扰线  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)for(inti=0;i&lt;25;i++)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)intx1=random.Next(image.Width);  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)intx2=random.Next(image.Width);  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)inty1=random.Next(image.Height);  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)inty2=random.Next(image.Height);  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)g.DrawLine(newPen(Color.GhostWhite),x1,y1,x2,y2);  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)}  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)Fontfont=newFont("Arial",16,(FontStyle.Bold|FontStyle.Italic));  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)LinearGradientBrushbrush=newLinearGradientBrush(newRectangle(0,0,image.Width,image.Height),  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)Color.Blue,Color.DarkRed,1.2f,true);  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)g.DrawString(validateNum,font,brush,3,2);  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)//画图片的前景干扰点  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)for(inti=0;i&lt;100;i++)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)intx=random.Next(image.Width);  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)inty=random.Next(image.Height);  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)image.SetPixel(x,y,Color.FromArgb(random.Next()));  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)}  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)//画图片的边框线  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)g.DrawRectangle(newPen(Color.Silver),0,0,image.Width-1,image.Height-1);  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)//保存图片数据  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)MemoryStreamstream=newMemoryStream();  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)image.Save(stream,ImageFormat.Jpeg);  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)//输出图片  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)containsPage.Response.Clear();  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)containsPage.Response.ContentType="image/jpeg";  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)containsPage.Response.BinaryWrite(stream.ToArray());  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)}  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)finally  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)g.Dispose();  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)image.Dispose();  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)}  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)}  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)/\*\*////&lt;summary&gt;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)///得到验证码图片的长度  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)///&lt;/summary&gt;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)///&lt;paramname="validateNumLength"&gt;验证码的长度&lt;/param&gt;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)///&lt;returns&gt;&lt;/returns&gt;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)publicstaticintGetImageWidth(intvalidateNumLength)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)return(int)(validateNumLength\*15);  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)}  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)/\*\*////&lt;summary&gt;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)///得到验证码的高度  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)///&lt;/summary&gt;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)///&lt;returns&gt;&lt;/returns&gt;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)publicstaticdoubleGetImageHeight()  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)return25;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)}  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)生成不重复的字条串#region生成不重复的字条串  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)privatestringRandomKey(intb,inte)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)returnDateTime.Now.ToString("yyyyMMdd-HHmmss-fff-")+this.getRandomID(b,e);  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)}  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)privateintgetRandomID(intminValue,intmaxValue)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)Randomri=newRandom(unchecked((int)DateTime.Now.Ticks));  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)intk=ri.Next(minValue,maxValue);  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)returnk;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)}  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)privatestringGuidString  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)get  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)returnGuid.NewGuid().ToString();  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)}  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)}  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)#endregion  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)}  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)}