ASP.NET 验证码控件
public class ValidateCode : WebControl
{
/// <summary>
/// 默认构造函数,暴露的属性接口
/// </summary>
public ValidateCode()
{
this.CharCount = ;
this.Width = ;
this.Height = ;
CodeColor = Color.White;
} /// <summary>
/// 设置参数
/// </summary>
/// <param name="CharCount"></param>
/// <param name="Width"></param>
/// <param name="Height"></param>
/// <param name="color"></param>
public ValidateCode(int CharCount, int Width, int Height,Color color)
{
this.CharCount = CharCount;
this.Width = Width;
this.Height = Height;
this.CodeColor = color;
}
/// <summary>
/// 字符个数
/// </summary>
public int CharCount
{
get;
set;
}
/// <summary>
/// 图标矩形宽度
/// </summary>
public new int Width
{
set;
get;
}
/// <summary>
/// 图标矩形高度
/// </summary>
public new int Height
{
set;
get;
} /// <summary>
/// Code背景色
/// </summary>
public Color CodeColor
{
get;
set;
} /// <summary>
/// 验证验证码是否正确
/// </summary>
/// <param name="sn"></param>
/// <returns></returns>
public bool checkCode(string sn)
{
return (sn.ToUpper() == this.Page.Request.Cookies["validateCookie"].Values["ChkCode"].ToString().ToUpper());//读取Cookie
}
protected override void OnInit(EventArgs e)
{
base.OnInit(e);
if (this.DesignMode)
return;
string str = this.Page.Request.QueryString["_xuImageTag"]; if (str != "")
return;
HttpResponse resp = this.Page.Response;
string chkCode = string.Empty; //颜色列表,用于验证码、噪线、噪点
Color[] color = { Color.Red, Color.Black, Color.Blue, Color.Green, Color.Orange }; string[] font = { "宋体", "Times New Roman", "MS Mincho", "楷体", "隶书", "微软雅黑","Calibri" };
char[] character ={'','','','','','','','','','','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'};
Random rnd = new Random();
for (int i = ; i < this.CharCount; i++)
{
chkCode += character[rnd.Next(character.Length)];
} resp.Cookies["validateCookie"].Values["ChkCode"] =chkCode;//验证码写入Cookie Bitmap bmp = new Bitmap(this.Width,this.Height);
Graphics g = Graphics.FromImage(bmp);
g.Clear(this.CodeColor); //画噪线
for (int i = ; i < ; i++)
{
int x1 = rnd.Next(this.Width);
int y1 = rnd.Next(this.Height);
int x2 = rnd.Next(this.Width);
int y2 = rnd.Next(this.Height);
Color clr = color[rnd.Next(color.Length)];
g.DrawLine(new Pen(clr), x1, y1, x2, y2);
}
//画验证字符串
for (int i = ; i < chkCode.Length; i++)
{
string fnt = font[rnd.Next(font.Length)];
Font ft = new Font(fnt, );
Color clr = color[rnd.Next(color.Length)];
g.DrawString(chkCode[i].ToString(), ft, new SolidBrush(clr), (float)i * this.Width/chkCode.Length++, ((float)this.Height)/);
} for (int i = ; i < (this.Width*this.Height)/; i++)
{
int x = rnd.Next(bmp.Width);
int y = rnd.Next(bmp.Height);
Color clr = color[rnd.Next(color.Length)];
bmp.SetPixel(x, y, clr);
} //将验证图片写入内存流,并将以image/Png格式输出
MemoryStream ms = new MemoryStream();
try
{
bmp.Save(ms, ImageFormat.Png);
resp.ClearContent();
resp.ContentType = "image/Png";
resp.BinaryWrite(ms.ToArray());
resp.Flush();
resp.End();
}
finally
{
bmp.Dispose();
g.Dispose();
} }
protected override void Render(HtmlTextWriter writer)
{ if (!this.DesignMode)
{
writer.Write("<img border=\"0\" src=\"{0}\">", this.Page.Request.Path + "?_xuImageTag=1");
}
else
{
writer.Write("验证码");
}
base.Render(writer);
}
}
ASP.NET 验证码控件的更多相关文章
- asp.net分页控件
一.说明 AspNetPager.dll这个分页控件主要用于asp.net webform网站,现将整理代码如下 二.代码 1.首先在测试页面Default.aspx页面添加引用 <%@ Reg ...
- asp.net ajax控件tab扩展,极品啊,秒杀其它插件
说明:asp.net ajax控件tab要设置width和height,而且在线文本编辑器放能够放入tab中,也必须是asp.net的控件型在线文本,例如fckeditor,下面是我设置好的配置. & ...
- javascript获取asp.net服务器端控件的值
代码如下: <%@ Page Language="C#" CodeFile="A.aspx.cs" Inherits="OrderManage_ ...
- ASP.NET控件<ASP:Button /> html控件<input type="button">区别联系
ASP.NET控件<ASP:Button />-------html控件<input type="button">杨中科是这么说的:asp和input是一样 ...
- ux.form.field.Verify 验证码控件
//验证码控件 Ext.define('ux.form.field.Verify', { extend: 'Ext.container.Container', alias: ['widget.fiel ...
- [ASP.NET]asp.net Repeater控件的使用方法
asp.net Repeater控件的使用方法 -- : 4770人阅读 评论() 收藏 举报 asp.netserveraspdatasetdeletexhtml 今天学习了,Repeater控件 ...
- 关于ASP.net TextBox控件的失去焦点后触发其它事件
编写人:CC阿爸 2015-2-02 今天在这里,我想与大家一起分享如何处理的ASP.net TextBox控件的失去焦点后触发其它事件的问题,在此做个小结,以供参考.有兴趣的同学,可以一同探讨与学习 ...
- 浅谈ASP.NET报表控件
OWC似乎使用者居多,但看见有网友在帖中抱怨OWC在使用时需要许可证书,于是将其排除,我可不想BOSS在看报表时弹出一个“没有许可证书”的窗口. 接着找到了ComponentOne的Web chart ...
- 要后台控制前台的的CSS样式,我们可以加入ASP.NET Literal 控件
ASP.NET Literal 控件,用于在页面上显示文本.此文本是可编程的. 我用它来制作了 ) { this.LtdMemberPromotion7.Text = "<style ...
随机推荐
- PS切图(一)
Photoshop界面设置 新建文件Ctrl+N,其中[预设]-[web],[宽度]-[1920],高度不定.[背景内容]-[透明],也可存储为预设. 移动设置(V) 建议不勾选[自动选择],选择[图 ...
- Java监听器
监听器 1.概念 监听器:主要是用来监听特定对象的创建,属性的变化的!,本质上却是一个实现特定接口的普通java类! 对象分为自己创建自己使用的,和别人创建自己用的,自己创建的不需要监听,值需要取监听 ...
- jsp基础
1.1动态页面 动态页面的优势: 1.交互性:网页会根据用户的要求和选而动态改变和显示内容; 2.自动更新:无需改变页面代码,便会自动更新的页面内容; 3.随机性:当不同的时间,不同的人访问同一网址时 ...
- @RenderSection,@RenderPage,@RenderBody介绍
在MVC的模板页中会用到上面三个东西,那么今天就简单归纳下各有什么作用 1.@RenderSection 用法 对CSS或JS部分模块的预留定义 例如模板页定义了@RenderSection(&quo ...
- iOS学习-圆形进度条
效果: #import <UIKit/UIKit.h> @interface HsProfitRatePieWidgets : UIView { UILabel *_textLabel; ...
- FragmentTabHost简单保存状态的方法
private View rootView;@Overridepublic View onCreateView(LayoutInflater inflater, ViewGroup container ...
- maven-过滤不打入包的文件
在使用maven打包时,有时有些测试文件,或者配置都希望打入到架包中 此时就需要使用将不用的文件过滤,maven有很方便的过滤插件.因工作时间,暂不讨论.本次讨论一个非常简单除暴的方法,通过配置ecl ...
- ijkplayer demo效果图
如下截图所示,用https://github.com/Bilibili/ijkplayer.git源码编译生成的ijkplayer app效果图,是这样的吗?有没有朋友指点下?
- Android View 的事件体系
android 系统虽然提供了很多基本的控件,如Button.TextView等,但是很多时候系统提供的view不能满足我们的需求,此时就需要我们根据自己的需求进行自定义控件.这些控件都是继承自Vie ...
- 制作宅基腐主页 && 制作个人简历--材料:BootStrap
宅基腐主页 主页分为四大板块:导航栏,轮播图,主页内容,相关信息. 导航栏:<nav>标签,class="navbar",navbar-inverse为反色,navba ...