vs2005下彩色验证码的实现(可自由定义是否加入干扰点、验证码位数等验证码显示效果)...

转自:http://www.cnblogs.com/zbqy/archive/2007/04/29/731712.html
vs2005下彩色验证码的实现(可自由定义是否加入干扰点、验证码位数等验证码显示效果)

一、效果如下:
test.jpg
二、项目中添加一个类命名为(可任选)在此类中添加代码
None.gif using  System;
None.gif
using  System.Web;
None.gif
using  System.IO;
None.gif
using  System.Drawing;
None.gif
ExpandedBlockStart.gifContractedBlock.gif
/**/ /// <summary>
InBlock.gif
/// Class1 的摘要说明
ExpandedBlockEnd.gif
/// </summary>

None.gif public   class  zhangming
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif    
public class VerifyCode
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif
ContractedSubBlock.gifExpandedSubBlockStart.gif        
验证码长度(默认5个验证码的长度)#region 验证码长度(默认5个验证码的长度)
InBlock.gif        
int length = 5;
InBlock.gif        
public int Length
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif            
getdot.gifreturn length; }
ExpandedSubBlockStart.gifContractedSubBlock.gif            
setdot.gif{ length = value; }
ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif        
#endregion

InBlock.gif
ContractedSubBlock.gifExpandedSubBlockStart.gif        
验证码字体大小(默认10像素)#region 验证码字体大小(默认10像素)
InBlock.gif        
int fontSize = 10;
InBlock.gif        
public int FontSize
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif            
getdot.gifreturn fontSize; }
ExpandedSubBlockStart.gifContractedSubBlock.gif            
setdot.gif{ fontSize = value; }
ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif        
#endregion

InBlock.gif
ContractedSubBlock.gifExpandedSubBlockStart.gif        
边框补(默认1像素)#region 边框补(默认1像素)
InBlock.gif        
int padding = 1;
InBlock.gif        
public int Padding
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif            
getdot.gifreturn padding; }
ExpandedSubBlockStart.gifContractedSubBlock.gif            
setdot.gif{ padding = value; }
ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif        
#endregion

InBlock.gif
ContractedSubBlock.gifExpandedSubBlockStart.gif        
是否输出燥点(默认不输出)#region 是否输出燥点(默认不输出)
InBlock.gif        
bool chaos = true;
InBlock.gif        
public bool Chaos
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif            
getdot.gifreturn chaos; }
ExpandedSubBlockStart.gifContractedSubBlock.gif            
setdot.gif{ chaos = value; }
ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif        
#endregion

InBlock.gif
ContractedSubBlock.gifExpandedSubBlockStart.gif        
输出燥点的颜色(默认灰色)#region 输出燥点的颜色(默认灰色)
InBlock.gif        Color chaosColor 
= Color.LightGray;
InBlock.gif        
public Color ChaosColor
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif            
getdot.gifreturn chaosColor; }
ExpandedSubBlockStart.gifContractedSubBlock.gif            
setdot.gif{ chaosColor = value; }
ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif        
#endregion

InBlock.gif
ContractedSubBlock.gifExpandedSubBlockStart.gif        
自定义背景色(默认白色)#region 自定义背景色(默认白色)
InBlock.gif        Color backgroundColor 
= Color.White;
InBlock.gif        
public Color BackgroundColor
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif            
getdot.gifreturn backgroundColor; }
ExpandedSubBlockStart.gifContractedSubBlock.gif            
setdot.gif{ backgroundColor = value; }
ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif        
#endregion

InBlock.gif
ContractedSubBlock.gifExpandedSubBlockStart.gif        
自定义随机颜色数组#region 自定义随机颜色数组
ExpandedSubBlockStart.gifContractedSubBlock.gif        Color[] colors 
= dot.gif{Color.Black,Color.Red,Color.DarkBlue,Color.Green,Color.Orange,Color.Brown,Color.DarkCyan,Color.Purple};
InBlock.gif        
public Color[] Colors
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif            
getdot.gifreturn colors; }
ExpandedSubBlockStart.gifContractedSubBlock.gif            
setdot.gif{ colors = value; }
ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif        
#endregion

InBlock.gif
ContractedSubBlock.gifExpandedSubBlockStart.gif        
自定义字体数组#region 自定义字体数组
ExpandedSubBlockStart.gifContractedSubBlock.gif        
string[] fonts = dot.gif{"Arial""Georgia"};
InBlock.gif        
public string[] Fonts
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif            
getdot.gifreturn fonts; }
ExpandedSubBlockStart.gifContractedSubBlock.gif            
setdot.gif{ fonts = value; }
ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif        
#endregion

InBlock.gif
ContractedSubBlock.gifExpandedSubBlockStart.gif        
自定义随机码字符串序列(使用逗号分隔)#region 自定义随机码字符串序列(使用逗号分隔)
InBlock.gif        
string codeSerial = "0,1,2,3,4,5,6,7,8,9,a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z,A,B,C,D,E,F,G,H,I,J,K,L,M,N,O,P,Q,R,S,T,U,V,W,X,Y,Z";
InBlock.gif        
public string CodeSerial
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif            
getdot.gifreturn codeSerial; }
ExpandedSubBlockStart.gifContractedSubBlock.gif            
setdot.gif{ codeSerial = value; }
ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif        
#endregion

InBlock.gif
ContractedSubBlock.gifExpandedSubBlockStart.gif        
生成校验码图片#region 生成校验码图片
InBlock.gif        
public Bitmap CreateImageCode(string code)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
int fSize    = FontSize;
InBlock.gif            
int fWidth  = fSize + Padding;
InBlock.gif
InBlock.gif            
int imageWidth        = (int)(code.Length * fWidth) + 4 + Padding * 2;
InBlock.gif            
int imageHeight        = fSize *2 + Padding;
InBlock.gif
InBlock.gif            System.Drawing.Bitmap image 
= new System.Drawing.Bitmap(imageWidth, imageHeight);
InBlock.gif
InBlock.gif            Graphics g 
= Graphics.FromImage(image);
InBlock.gif
InBlock.gif            g.Clear(BackgroundColor);
InBlock.gif
InBlock.gif            Random rand 
= new Random();
InBlock.gif
InBlock.gif            
//给背景添加随机生成的燥点
InBlock.gif
            if(this.Chaos)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif
InBlock.gif                Pen pen 
= new Pen(ChaosColor, 0);
InBlock.gif                
int c = Length * 10;
InBlock.gif
InBlock.gif                
for(int i=0;i<c;i++)
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    
int x = rand.Next(image.Width);
InBlock.gif                    
int y = rand.Next(image.Height);
InBlock.gif
InBlock.gif                    g.DrawRectangle(pen, x, y, 
11);
ExpandedSubBlockEnd.gif                }

ExpandedSubBlockEnd.gif            }

InBlock.gif
InBlock.gif            
int left    = 0 , top = 0, top1 = 1, top2 = 1;
InBlock.gif
InBlock.gif            
int n1        = (imageHeight - FontSize - Padding * 2);
InBlock.gif            
int n2        = n1/4;
InBlock.gif            top1        
= n2;
InBlock.gif            top2        
= n2 *2;
InBlock.gif            
InBlock.gif            Font f;
InBlock.gif            Brush b;
InBlock.gif
InBlock.gif            
int cindex, findex;
InBlock.gif
InBlock.gif            
//随机字体和颜色的验证码字符
InBlock.gif
            for(int i=0; i<code.Length; i++)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                cindex 
= rand.Next(Colors.Length - 1);
InBlock.gif                findex 
= rand.Next(Fonts.Length - 1);
InBlock.gif                
InBlock.gif                f 
= new System.Drawing.Font(Fonts[findex], fSize, System.Drawing.FontStyle.Bold);
InBlock.gif                b 
= new System.Drawing.SolidBrush(Colors[cindex]);
InBlock.gif
InBlock.gif                
if( i%2 == 1 )
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    top 
= top2;
ExpandedSubBlockEnd.gif                }

InBlock.gif                
else
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    top 
= top1;
ExpandedSubBlockEnd.gif                }

InBlock.gif
InBlock.gif                left 
= i * fWidth;
InBlock.gif
InBlock.gif                g.DrawString(code.Substring(i,
1), f, b, left, top);
ExpandedSubBlockEnd.gif            }

InBlock.gif
InBlock.gif            
//画一个边框 边框颜色为Color.Gainsboro
InBlock.gif
            g.DrawRectangle(new Pen(Color.Gainsboro, 0), 00, image.Width - 1, image.Height - 1);
InBlock.gif            g.Dispose();
InBlock.gif            
return image;
ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif        
#endregion

InBlock.gif
ContractedSubBlock.gifExpandedSubBlockStart.gif        
将创建好的图片输出到页面#region 将创建好的图片输出到页面
InBlock.gif        
public void CreateImageOnPage(string code, HttpContext context)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            System.IO.MemoryStream ms 
= new System.IO.MemoryStream();
InBlock.gif            Bitmap image 
= this.CreateImageCode(code);
InBlock.gif
InBlock.gif            image.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);
InBlock.gif
InBlock.gif            context.Response.ClearContent();
InBlock.gif            context.Response.ContentType 
= "image/Jpeg";
InBlock.gif            context.Response.BinaryWrite(ms.GetBuffer());
InBlock.gif
InBlock.gif            ms.Close();
InBlock.gif            ms 
= null;
InBlock.gif            image.Dispose();
InBlock.gif            image 
= null;
ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif        
#endregion

InBlock.gif
ContractedSubBlock.gifExpandedSubBlockStart.gif        
生成随机字符码#region 生成随机字符码
InBlock.gif        
public string CreateVerifyCode(int codeLen)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
if(codeLen == 0)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                codeLen 
= Length;
ExpandedSubBlockEnd.gif            }

InBlock.gif
InBlock.gif            
string[] arr = CodeSerial.Split(',');
InBlock.gif
InBlock.gif            
string code = "";
InBlock.gif
InBlock.gif            
int randValue = -1;
InBlock.gif
InBlock.gif            Random rand 
= new Random(unchecked((int)DateTime.Now.Ticks));
InBlock.gif
InBlock.gif            
for(int i=0; i<codeLen; i++)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                randValue 
= rand.Next(0, arr.Length -1);
InBlock.gif
InBlock.gif                code 
+= arr[randValue];
ExpandedSubBlockEnd.gif            }

InBlock.gif
InBlock.gif            
return code;
ExpandedSubBlockEnd.gif        }

InBlock.gif        
public string CreateVerifyCode()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
return CreateVerifyCode(0);
ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif        
#endregion

InBlock.gif
ExpandedSubBlockEnd.gif    }

ExpandedBlockEnd.gif}

三、新建GetImageCode.aspx页面前台添加代码(实际前台代码无内容)
None.gif < body >
None.gif    
< form id = " form1 "  runat = " server " >
None.gif    
< div >
None.gif    
None.gif    
</ div >
None.gif    
</ form >
None.gif
</ body >

四、添加GetImageCode.aspx.cs后台代码
None.gif using  System;
None.gif
using  System.Data;
None.gif
using  System.Configuration;
None.gif
using  System.Collections;
None.gif
using  System.Web;
None.gif
using  System.Web.Security;
None.gif
using  System.Web.UI;
None.gif
using  System.Web.UI.WebControls;
None.gif
using  System.Web.UI.WebControls.WebParts;
None.gif
using  System.Web.UI.HtmlControls;
None.gif
using  System.IO;
None.gif
using  System.Drawing;
None.gif
None.gif
public  partial  class  Default4 : System.Web.UI.Page
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
ContractedSubBlock.gifExpandedSubBlockStart.gif    
验证码长度(默认5个验证码的长度)#region 验证码长度(默认5个验证码的长度)
InBlock.gif    
int length = 5;
ExpandedSubBlockEnd.gif    
#endregion

InBlock.gif
ContractedSubBlock.gifExpandedSubBlockStart.gif    
验证码字体大小(默认10像素)#region 验证码字体大小(默认10像素)
InBlock.gif    
int fontSize = 10;
ExpandedSubBlockEnd.gif    
#endregion

InBlock.gif
ContractedSubBlock.gifExpandedSubBlockStart.gif    
边框补(默认1像素)#region 边框补(默认1像素)
InBlock.gif    
int padding = 1;
ExpandedSubBlockEnd.gif    
#endregion

InBlock.gif
ContractedSubBlock.gifExpandedSubBlockStart.gif    
是否输出燥点(默认输出)#region 是否输出燥点(默认输出)
InBlock.gif    
bool chaos = true;
ExpandedSubBlockEnd.gif    
#endregion

InBlock.gif
ContractedSubBlock.gifExpandedSubBlockStart.gif    
输出燥点的颜色(默认灰色)#region 输出燥点的颜色(默认灰色)
InBlock.gif    Color chaosColor 
= System.Drawing.Color.Pink;
ExpandedSubBlockEnd.gif    
#endregion

InBlock.gif
ContractedSubBlock.gifExpandedSubBlockStart.gif    
自定义背景色(默认白色)#region 自定义背景色(默认白色)
InBlock.gif    Color backgroundColor 
= Color.White;
ExpandedSubBlockEnd.gif    
#endregion

InBlock.gif
ContractedSubBlock.gifExpandedSubBlockStart.gif    
自定义随机颜色数组#region 自定义随机颜色数组
ExpandedSubBlockStart.gifContractedSubBlock.gif    Color[] colors 
= dot.gif{ Color.Black, Color.Red, Color.DarkBlue, Color.Green, Color.Orange, Color.Brown, Color.DarkCyan, Color.Purple };
ExpandedSubBlockEnd.gif    
#endregion

InBlock.gif
ContractedSubBlock.gifExpandedSubBlockStart.gif    
自定义字体数组#region 自定义字体数组
ExpandedSubBlockStart.gifContractedSubBlock.gif    
string[] fonts = dot.gif"Arial""Georgia" };
ExpandedSubBlockEnd.gif    
#endregion

InBlock.gif
ContractedSubBlock.gifExpandedSubBlockStart.gif    
自定义随机码字符串序列(使用逗号分隔)#region 自定义随机码字符串序列(使用逗号分隔)
InBlock.gif    
string codeSerial = "0,1,2,3,4,5,6,7,8,9,a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z";
ExpandedSubBlockEnd.gif    
#endregion

InBlock.gif
InBlock.gif    
protected void Page_Load(object sender, EventArgs e)
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        zhangming.VerifyCode v 
= new zhangming.VerifyCode();
InBlock.gif
InBlock.gif        v.Length 
= this.length;
InBlock.gif
InBlock.gif        v.FontSize 
= this.fontSize;
InBlock.gif
InBlock.gif        v.Chaos 
= this.chaos;
InBlock.gif
InBlock.gif        v.BackgroundColor 
= this.backgroundColor;
InBlock.gif
InBlock.gif        v.ChaosColor 
= this.chaosColor;
InBlock.gif
InBlock.gif        v.CodeSerial 
= this.codeSerial;
InBlock.gif
InBlock.gif        v.Colors 
= this.colors;
InBlock.gif
InBlock.gif        v.Fonts 
= this.fonts;
InBlock.gif
InBlock.gif        v.Padding 
= this.padding;
InBlock.gif
InBlock.gif        
string code = v.CreateVerifyCode();                //取随机码
InBlock.gif

InBlock.gif        v.CreateImageOnPage(code, 
this.Context);        // 输出图片
InBlock.gif
InBlock.gif        
//Session["CheckCode"] = code.ToLower();            // 使用Session["CheckCode"]取验证码的值
InBlock.gif
        Response.Cookies.Add(new HttpCookie("CheckCode", code.ToLower()));// 使用Cookies取验证码的值
InBlock.gif

InBlock.gif    
ExpandedSubBlockEnd.gif    }

ExpandedBlockEnd.gif}

None.gif

五、验证码的使用(在Test.aspx页面加入<asp:Image id="Image1" runat="server" ImageUrl="~/GetVerifyCode.aspx"></asp:Image>)即实现验证码

None.gif < form id = " form1 "  runat = " server " >
None.gif    
< div >
None.gif        
< asp:TextBox ID = " TextBox1 "  runat = " server " ></ asp:TextBox >
None.gif
& nbsp;  & nbsp;
None.gif
< asp:Image id = " Image1 "  runat = " server "  ImageUrl = " GetImageCode.aspx " ></ asp:Image >
None.gif        
& nbsp;  & nbsp;
None.gif        
< asp:Button ID = " Button1 "  runat = " server "  OnClick = " Button1_Click "
None.gif            Text
= " Button "   />
None.gif
< br  />
None.gif        
< asp:Label ID = " Label1 "  runat = " server " ></ asp:Label ></ div >
None.gif    
</ form >

六、Test.aspx.cs后台代码

None.gif protected   void  Page_Load( object  sender, EventArgs e)
ExpandedBlockStart.gifContractedBlock.gif    
dot.gif {
InBlock.gif        
if (!Page.IsPostBack)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
//Session["CheckCode"] = "";
InBlock.gif
            Response.Cookies.Add(new HttpCookie("CheckCode"""));
ExpandedSubBlockEnd.gif        }

ExpandedBlockEnd.gif    }

None.gif
protected   void  Button1_Click( object  sender, EventArgs e)
ExpandedBlockStart.gifContractedBlock.gif    
dot.gif {
InBlock.gif
InBlock.gif        
if (Request.Cookies["CheckCode"== null)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            Label1.Text 
= "您的浏览器设置已被禁用 Cookies,您必须设置浏览器允许使用 Cookies 选项后才能使用本系统。";
InBlock.gif            Label1.Visible 
= true;
InBlock.gif            
return;
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
if (String.Compare(Request.Cookies["CheckCode"].Value, TextBox1.Text.ToString().Trim(), true!= 0)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            Label1.Text 
= "验证码错误,请输入正确的验证码。";
InBlock.gif            Label1.Visible 
= true;
InBlock.gif            
return;
ExpandedSubBlockEnd.gif        }

InBlock.gif        
else
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            Label1.Text 
= "验证码OK。";
InBlock.gif            Label1.Visible 
= true;
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif
InBlock.gif        
InBlock.gif
ExpandedBlockEnd.gif    }

转载于:https://www.cnblogs.com/jenner-lin/archive/2007/05/28/762486.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值