刚做了个验证码控件

18.gif 看到很多第三方控件,感觉很不错,自己就写了验证码控件,可以自己设置很多属性,大概代码贴出来,要整个代码就给我邮件,很感谢指出不好的地方!
None.gifnamespace Check_Code
ExpandedBlockStart.gifContractedBlock.gif
dot.gif{
InBlock.gif    
using System;
InBlock.gif    
using System.Data;
InBlock.gif    
using System.Drawing;
InBlock.gif    
using System.Web;
InBlock.gif    
using System.Web.UI.WebControls;
InBlock.gif    
using System.Web.UI.HtmlControls;
InBlock.gif    
using System.Drawing.Imaging;
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif    
/**//// <summary>
InBlock.gif    
///        验证码控件
ExpandedSubBlockEnd.gif    
/// </summary>

InBlock.gif    public class Check_Code : System.Web.UI.UserControl
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{    
InBlock.gif        
protected System.Web.UI.WebControls.Image Image1;
InBlock.gif
ContractedSubBlock.gifExpandedSubBlockStart.gif        
图片私有变量 初始设置#region 图片私有变量 初始设置
InBlock.gif
InBlock.gif        
//验证码长度
InBlock.gif
        private int length=4;
InBlock.gif
InBlock.gif        
//图片清晰度
InBlock.gif
        private int fineness=95;
InBlock.gif
InBlock.gif        
//图片宽度
InBlock.gif
        private int width=60;
InBlock.gif
InBlock.gif        
//图片高度
InBlock.gif
        private int height=20;
InBlock.gif
InBlock.gif        
//字体家族名称
InBlock.gif
        private string fontfamily="Verdana";
InBlock.gif
InBlock.gif        
//字体大小
InBlock.gif
        private int size=12;
InBlock.gif
//        
InBlock.gif
//        //字体颜色
InBlock.gif
//        private string color="Blank";
InBlock.gif
InBlock.gif        
//字体样式
InBlock.gif
        private int style=3;
InBlock.gif
InBlock.gif        
//绘制起使坐标X
InBlock.gif
        private int x=0;
InBlock.gif    
InBlock.gif
InBlock.gif        
//绘制起使坐标Y
InBlock.gif
        private int y=0;
InBlock.gif
ExpandedSubBlockEnd.gif        
#endregion

InBlock.gif
ContractedSubBlock.gifExpandedSubBlockStart.gif        
图片基本属性#region 图片基本属性
InBlock.gif
InBlock.gif        
//验证码长度属性
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        }

InBlock.gif
InBlock.gif        
//图片清晰度属性
InBlock.gif
        public int Fineness
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif            
getdot.gifreturn fineness;}
ExpandedSubBlockStart.gifContractedSubBlock.gif            
setdot.gif{ fineness=value;}
ExpandedSubBlockEnd.gif        }

InBlock.gif        
InBlock.gif        
//图片宽度属性
InBlock.gif
        public int Width
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif            
getdot.gif{return width;}
ExpandedSubBlockStart.gifContractedSubBlock.gif            
setdot.gif{width=value;}
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
//图片高度属性
InBlock.gif
        public int Height
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif            
getdot.gif{return height;}
ExpandedSubBlockStart.gifContractedSubBlock.gif            
setdot.gif{height=value;}
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
//字体家族名称
InBlock.gif
        public string FontFamily
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif            
getdot.gif{return fontfamily;}
ExpandedSubBlockStart.gifContractedSubBlock.gif            
setdot.gif{fontfamily=value;}
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
//字体大小
InBlock.gif
        public int Size
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif            
getdot.gif{return size;}
ExpandedSubBlockStart.gifContractedSubBlock.gif            
setdot.gif{size=value;}
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif
//        //字体颜色
InBlock.gif
//        public string Color
InBlock.gif
//        {
InBlock.gif
//            get{return color;}
InBlock.gif
//            set{color=value;}
InBlock.gif
//        }
InBlock.gif
InBlock.gif        
//字体样式
InBlock.gif
        public int Style
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif            
getdot.gif{return style;}
ExpandedSubBlockStart.gifContractedSubBlock.gif            
setdot.gif{style=value;}
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
//绘制起使坐标X
InBlock.gif
        public int X
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif            
getdot.gif{return x;}
ExpandedSubBlockStart.gifContractedSubBlock.gif            
setdot.gif{x=value;}
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
//绘制起使坐标Y
InBlock.gif
        public int Y
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif            
getdot.gif{return y;}
ExpandedSubBlockStart.gifContractedSubBlock.gif            
setdot.gif{y=value;}
ExpandedSubBlockEnd.gif        }

InBlock.gif
ExpandedSubBlockEnd.gif        
#endregion

InBlock.gif
ContractedSubBlock.gifExpandedSubBlockStart.gif        
判断符合范围#region 判断符合范围
InBlock.gif        
public void Checked_Or()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
if(Length>16||Length<4)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                Response.Write(
"<script>alert('验证码长度要在4-16之间')</script>");
ExpandedSubBlockEnd.gif            }

InBlock.gif
InBlock.gif            
if(Fineness>100||Fineness<0)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                Response.Write(
"<script>alert('验证码图片清晰度要在0-100之间')</script>");
ExpandedSubBlockEnd.gif            }

InBlock.gif            
InBlock.gif            
if(Width>200||Width<20)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                Response.Write(
"<script>alert('验证码图片宽度要在20-200px之间')</script>");
ExpandedSubBlockEnd.gif            }

InBlock.gif
InBlock.gif            
if(Height>100||Height<10)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                Response.Write(
"<script>alert('验证码图片高度要在10-100之间')</script>");
ExpandedSubBlockEnd.gif            }

InBlock.gif
InBlock.gif            
if(Size>72||Size<6)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                Response.Write(
"<script>alert('验证码字体大小要在6-72之间')</script>");
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

InBlock.gif        
ExpandedSubBlockEnd.gif        
#endregion

InBlock.gif
ContractedSubBlock.gifExpandedSubBlockStart.gif        
随机生成字母或数字 并保存到session#region 随机生成字母或数字 并保存到session
InBlock.gif
InBlock.gif        
public string RandowCode()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
string randowcode="";
InBlock.gif            Random rm
=new Random();
InBlock.gif            
for(int i=0;i<Length;i++)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
//A-Z
InBlock.gif
                int n=rm.Next(26);
InBlock.gif                randowcode
+=(char)(n+65);
ExpandedSubBlockEnd.gif            }

InBlock.gif            Session.Add(
"code",randowcode);
InBlock.gif            
return randowcode;
ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif        
#endregion

InBlock.gif
ContractedSubBlock.gifExpandedSubBlockStart.gif        
绘制验证码图片#region 绘制验证码图片
InBlock.gif        
public  void DrawCode(Bitmap bt,string Code)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
//获取绘制器对象 (笔)
InBlock.gif
            Graphics g=Graphics.FromImage(bt);
InBlock.gif            
//绘制字体
InBlock.gif
            Font font=new Font(this.FontFamily,this.Size,GetFontStyle());
InBlock.gif            
//绘制验证码图像
InBlock.gif
            g.DrawString(Code,font,Brushes.Red,X,Y);
InBlock.gif            
//保存验证码图片 准备输出
InBlock.gif
            bt.Save(Server.MapPath("img")+"\\Img.gif",ImageFormat.Gif);
InBlock.gif
ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif        
#endregion

InBlock.gif
ContractedSubBlock.gifExpandedSubBlockStart.gif        
字体样式#region 字体样式
InBlock.gif        
public FontStyle GetFontStyle()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
if(this.Style==1)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
return FontStyle.Bold;
ExpandedSubBlockEnd.gif            }

InBlock.gif            
else if(this.Style==2)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
return FontStyle.Italic;
ExpandedSubBlockEnd.gif            }

InBlock.gif            
else if(this.Style==3)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
return FontStyle.Bold | FontStyle.Italic;
ExpandedSubBlockEnd.gif            }

InBlock.gif            
else
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
return FontStyle.Regular;
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif        
#endregion

InBlock.gif
ContractedSubBlock.gifExpandedSubBlockStart.gif        
给图片设置随机干扰#region 给图片设置随机干扰
InBlock.gif        
public void DisturbBitmap(Bitmap bt)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            Random rm
=new Random();
InBlock.gif            
for(int i=0;i<bt.Width;i++)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
for(int j=0;j<bt.Height;j++)
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    
if(rm.Next(100)<this.Fineness)
ExpandedSubBlockStart.gifContractedSubBlock.gif                    
dot.gif{
InBlock.gif                        bt.SetPixel(i,j,Color.White);
ExpandedSubBlockEnd.gif                    }

ExpandedSubBlockEnd.gif                }

ExpandedSubBlockEnd.gif            }

InBlock.gif    
ExpandedSubBlockEnd.gif        }

InBlock.gif        
ExpandedSubBlockEnd.gif        
#endregion
 
InBlock.gif
InBlock.gif        
private void Page_Load(object sender, System.EventArgs e)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
//调用判断符合范围函数
InBlock.gif
            Checked_Or();
InBlock.gif            
//返回随机码
InBlock.gif
            string Code=RandowCode();
InBlock.gif            
//生成图片
InBlock.gif
            Bitmap bt=new Bitmap(Width,Height);
InBlock.gif            
//给图片设置干扰
InBlock.gif
            DisturbBitmap(bt);
InBlock.gif            
//绘制验证码图片
InBlock.gif
            DrawCode(bt,Code);
InBlock.gif
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
InBlock.gif
ContractedSubBlock.gifExpandedSubBlockStart.gif        
Web 窗体设计器生成的代码#region Web 窗体设计器生成的代码
InBlock.gif        
override protected void OnInit(EventArgs e)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
//
InBlock.gif            
// CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
InBlock.gif            
//
InBlock.gif
            InitializeComponent();
InBlock.gif            
base.OnInit(e);
ExpandedSubBlockEnd.gif        }

InBlock.gif        
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
///        设计器支持所需的方法 - 不要使用代码编辑器
InBlock.gif        
///        修改此方法的内容。
ExpandedSubBlockEnd.gif        
/// </summary>

InBlock.gif        private void InitializeComponent()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
this.Load += new System.EventHandler(this.Page_Load);
InBlock.gif
ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif        
#endregion

ExpandedSubBlockEnd.gif    }

ExpandedBlockEnd.gif}

None.gif

转载于:https://www.cnblogs.com/hyh11/archive/2005/09/27/245263.html

.Net 验证码控件 操作简单,使用方便参数使用有详细介绍 拖入即可应用!!!!!! 添加到工具箱的步骤 1.将 Vincent.AutoAuthCode.dll 添加到项目的 Bin文件目录下 2.右键点击 Bin 目录 选择添加引用 找到添加的 Bin文件目录下 Vincent.AutoAuthCode.dll 3.在工具栏中,右键点击 常规 选项卡 再点击 选择项 4.在弹出的对话框中的右下方 点击 浏览 找到 Bin文件目录下 Vincent.AutoAuthCode.dll 打开 点击 确定 在 工具箱 的 常规选项 看到 AutcCode 控件 直接拖到要使用验证码的页面位置 即可。 页面的点击事件会触发验证,无需后台代码验证 一、控件特点: 1、使用方便,只需要从工具栏直接拖到页面即可自动验证,零代码使用。 2、自动完成客户端以及服务器的验证码验证,Ajax验证,随用户输入即时 验证并友好提示。 3、可自定义验证码图片外观。 4、有水平方向垂直方向两种方式选择。 5、有数字、小写字母、大写字母三种及任意两种组合、三种组合字符选择。 6、兼容IE及FireFox。 二、使用说明。 1、属性。 IsMatch:指示用户输入的验证码是否正确 TextControlWidth:文本框控件宽度 NextImgText:提示更换图片信息,该提示信息同时显示于图片上面及图片左边 IsShowNextImgText:是否在图片左边显示提示更换图片信息 EnableNextImgText:是否充许换一张图片 ErrorShowType:验证码错误显示方式 CodeStringType:验证码字符类型,组合枚举值,例如CodeStringTypes.Number|CodeStringTypes.LowerLetter CodeStringLength:验证码字符长度,最少为4 ImageType:验证码图像类型 IsMatchCase:验证码验证时是否区分大小写 LayoutDirection":控件各部分排列方向,排列的部分包括文本框、图片、"换一张图片"文本 EnableClientValidate:是否使用客户端脚本验证,验证内容包括是否为空、长度是否正确 ImageStyle:验证码图像样式 如: TextControlWidth="90px" 设置 输入验证码的文本框宽度 其中ImageStyle为复类属性,其公开属性如下: ImageStyle-ImgBgColor:图片背景色 ImageStyle-ImgNoiseColor:图片噪声颜色 ImageStyle-ImgBorderColor"图片边框颜色 ImageStyle-TextColor1:文本颜色 ImageStyle-TextColor2:文本颜色2(如果文本为单色则无效) ImageStyle-TextFontSize:文本字体大小,以像素(pix)为单位,验证码图像大小根据此而变化,如果ImgSize大于由该值指定的大小,则图像大小为ImgSize ImageStyle-ImgSize:验证码图像大小,以像素(pix)为单位,如果TextFontSize指定的大小大于该值,则图像大小为TextFontSize指定的大小 ImageStyle-Width:验证码图像大小的宽度,以像素(pix)为单位,如果TextFontSize指定的大小大于该值,则图像大小为TextFontSize指定的大小 ImageStyle-Height:验证码图像大小的高度,以像素(pix)为单位,如果TextFontSize指定的大小大于该值,则图像大小为TextFontSize指定的大小 如: ImageStyle-Width=70 设置验证码图像大小的宽度 (值必须是整数) ImageStyle-ImgBgColor=#1D3647 图片背景色 (值不要添加单、双引号 [''," "])
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值