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 验证码控件的更多相关文章

  1. asp.net分页控件

    一.说明 AspNetPager.dll这个分页控件主要用于asp.net webform网站,现将整理代码如下 二.代码 1.首先在测试页面Default.aspx页面添加引用 <%@ Reg ...

  2. asp.net ajax控件tab扩展,极品啊,秒杀其它插件

    说明:asp.net ajax控件tab要设置width和height,而且在线文本编辑器放能够放入tab中,也必须是asp.net的控件型在线文本,例如fckeditor,下面是我设置好的配置. & ...

  3. javascript获取asp.net服务器端控件的值

    代码如下: <%@ Page Language="C#" CodeFile="A.aspx.cs" Inherits="OrderManage_ ...

  4. ASP.NET控件<ASP:Button /> html控件<input type="button">区别联系

    ASP.NET控件<ASP:Button />-------html控件<input type="button">杨中科是这么说的:asp和input是一样 ...

  5. ux.form.field.Verify 验证码控件

    //验证码控件 Ext.define('ux.form.field.Verify', { extend: 'Ext.container.Container', alias: ['widget.fiel ...

  6. [ASP.NET]asp.net Repeater控件的使用方法

    asp.net Repeater控件的使用方法 -- : 4770人阅读 评论() 收藏 举报 asp.netserveraspdatasetdeletexhtml 今天学习了,Repeater控件 ...

  7. 关于ASP.net TextBox控件的失去焦点后触发其它事件

    编写人:CC阿爸 2015-2-02 今天在这里,我想与大家一起分享如何处理的ASP.net TextBox控件的失去焦点后触发其它事件的问题,在此做个小结,以供参考.有兴趣的同学,可以一同探讨与学习 ...

  8. 浅谈ASP.NET报表控件

    OWC似乎使用者居多,但看见有网友在帖中抱怨OWC在使用时需要许可证书,于是将其排除,我可不想BOSS在看报表时弹出一个“没有许可证书”的窗口. 接着找到了ComponentOne的Web chart ...

  9. 要后台控制前台的的CSS样式,我们可以加入ASP.NET Literal 控件

    ASP.NET Literal 控件,用于在页面上显示文本.此文本是可编程的. 我用它来制作了 ) { this.LtdMemberPromotion7.Text = "<style ...

随机推荐

  1. [转]MVC5 - ASP.NET Identity登录原理 - Claims-based认证和OWIN

    本文转自:http://www.cnblogs.com/jesse2013/p/aspnet-identity-claims-based-authentication-and-owin.html 在M ...

  2. 解决子元素设置margin-top,效果到父元素上的问题

    有时当我们设置子元素的margin-top,但是却发现子元素没有出现上外边距的效果,反而是父元素出现了上外边距的效果. 这种问题的解决方法如下: 1.给父元素加边框. 2.给父元素设置padding- ...

  3. hadoopfs: 未找到命令...

    https://zhidao.baidu.com/question/240817305095236244.html 学习hadoop测试http://blog.csdn.net/thinkpadshi ...

  4. sqlServer、MySql批量操作插件

    SqlServer: 1.EntityFramework.BulkInsert  .只适用于SqlServer.要想实现MySql批量插入,得自己实现一些方法,注册驱动之类,暂时没能力. 2.z.en ...

  5. [LeetCode] Flip Game 翻转游戏

    You are playing the following Flip Game with your friend: Given a string that contains only these tw ...

  6. [LeetCode] Integer to Roman 整数转化成罗马数字

    Given an integer, convert it to a roman numeral. Input is guaranteed to be within the range from 1 t ...

  7. 基于C/S架构的3D对战网络游戏C++框架 _06搭建C/S架构的基本通信框架(尚未写完会重新编辑后再发出)

    本系列博客主要是以对战游戏为背景介绍3D对战网络游戏常用的开发技术以及C++高级编程技巧,有了这些知识,就可以开发出中小型游戏项目或3D工业仿真项目. 笔者将分为以下三个部分向大家介绍(每日更新): ...

  8. Markdown语法

    概述 Markdown的理念是能让文档更容易读.写和随 改. 它用简洁的语法代替排版,格式语法只涵盖纯文 本可以涵盖的范围. 不在 Markdown 涵盖范围之内的标签,都可以直接在文档里面用 HTM ...

  9. delphi 取硬盘号

    function GetVolumeID: string; var vVolumeNameBuffer: ..] of Char; vVolumeSerialNumber: DWORD; vMaxim ...

  10. NPOI的操作

    public async Task<MemoryStream> ExportExcel(IList<fuquestionbank> _list, string pId, str ...