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 ...
随机推荐
- JAVA中使用FTPClient实现文件上传下载实例代码
一.上传文件 原理就不介绍了,大家直接看代码吧 ? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 ...
- 主席树——求静态区间第k大
例题:poj2104 http://poj.org/problem?id=2104 讲解:http://blog.sina.com.cn/s/blog_6022c4720102w03t.html ht ...
- [LeetCode] Find All Duplicates in an Array 找出数组中所有重复项
Given an array of integers, 1 ≤ a[i] ≤ n (n = size of array), some elements appear twice and others ...
- [LeetCode] Valid Word Abbreviation 验证单词缩写
Given a non-empty string s and an abbreviation abbr, return whether the string matches with the give ...
- JavaScript模板引擎artTemplate.js——引入子模板
之前的例子都是单一结构的对象,如果遇到复杂对象结构,我们可以通过引入子模板来实现html的渲染. 依旧以之前的数据作为例子: <div id="content">< ...
- 讲座:Influence maximization on big social graph
Influence maximization on big social graph Fanju PPT链接: social influence booming of online social ne ...
- CentOS升级openssl
才设置了http2,结果蓝狗说我网站不安全,检测一下发现openssl有漏洞,于是准备升级一下openssl 检测网站: www.ssllabs.com/ssltest/analyze.html # ...
- Redis 常用操作
import org.junit.Before;import org.junit.Test;import redis.clients.jedis.Jedis;import java.util.Set; ...
- Android必学——AsyncTask
第一章 AsyncTask的基本构成 为是么要异步任务 1)Android单线程模型 2)耗时操作放在非主线程中执行 AsyncTask为何而生 1)子线程中跟新UI 2)封装.简化异步操作 pub ...
- android GPS定位源码 地图显示位置源码 有用到的小伙伴自提取
package com.jasgroup.cn.amhdeam; import java.io.IOException; import java.util.Iterator; import andro ...