ASP.NET自己写的验证码控件(源代码)
2012-08-28 17:34
405 查看
1,创建一个ASP.NET的服务器控件
2,在服务器控件里面创建一个类,赋值如下
3,生成之后直接在网页拖动使用
4,调用 bool b = this.ValidateNumber1.CheckSn(this.TextBox1.Text);就OK
5,这个只是比较基础的自己弄的玩玩,不过也过得去
--------------------------------源码----------------------------------------
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.IO;
namespace ValidateNumber
{
public class ValidateNumber:Control
{
public void CreateCode() {
//1,定义验证码的颜色,早点,和显示的字符
//颜色列表,用于验证码,噪点,噪线
Color[] color = { Color.Black, Color.Red, Color.Black, Color.Green, Color.Orange, Color.Brown, Color.DarkBlue };
//字体列表,用于验证码
string[] font = { "Times New Roman", "MS Mincho", "Book Antiqua", "Gungsuh", "PMingLiu", "Impact" };
//验证码的字符集,去掉了一些容易混淆的字符
char[] character = { '2', '3', '4', '5', '6', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'J', 'K', 'L', 'M', 'N', 'P', 'R', 'S', 'T', 'W', 'X', 'Y' };
Random rnd = new Random();
//2,随即生成和保存验证码
string chkCode = "";
//生成验证字符串
for (int i = 0; i < 4; i++)
{
chkCode += character[rnd.Next(character.Length)];
}
//保存验证码采用cookie模式
HttpResponse resp = this.Page.Response;
resp.Cookies["validateCookie"].Value = chkCode;
resp.Cookies["validateCookie"].Expires = DateTime.Now.AddMinutes(2);
//3,绘制噪点,噪线
Bitmap bmp = new Bitmap(100, 30);//长100排序,宽30px的图片
Graphics g = Graphics.FromImage(bmp);
g.Clear(Color.White);
//画五条噪线
for (int i = 0; i < 5; i++)
{
//随机生成噪线的起点和终点
int x1 = rnd.Next(100);
int y1 = rnd.Next(30);
int x2 = rnd.Next(100);
int y2 = rnd.Next(30);
//随机生成噪线的颜色
Color clr = color[rnd.Next(color.Length)];
//画出噪线
g.DrawLine(new Pen(clr), x1, y1, x2, y2);
}
//4,画噪点
for (int i = 0; i < 100; 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);
}
//5,画验证字符串
for (int i = 0; i < chkCode.Length; i++)
{
string fnt = font[rnd.Next(font.Length)];
Font ft = new Font(fnt, 16);
Color clr = color[rnd.Next(color.Length)];
g.DrawString(chkCode[i].ToString(), ft, new SolidBrush(clr), (float)i * 20 + 20, (float)6);
}
//6,将验证码图片写入内存流,并将其以image/Png格式输出
MemoryStream ms = new MemoryStream();//内存流
try
{
bmp.Save(ms, System.Drawing.Imaging.ImageFormat.Png);
resp.ClearContent();
resp.ContentType = "image/Png";
resp.BinaryWrite(ms.ToArray());
resp.Flush();
resp.End();
}
finally
{
//释放资源
bmp.Dispose();
g.Dispose();
}
}
//7,实现验证方法
public bool CheckSn(string sn) {
return (sn.ToUpper() == this.Page.Request.Cookies["validateCookie"].Value.ToString().ToUpper());
}
//8,创建验证码
protected override void OnInit(EventArgs e)
{
base.OnInit(e);
//开发环境设计视图
if (this.DesignMode) {
return;
}
//如果不是验证码请求
string str=this.Page.Request.QueryString["_ImageTag"];
if (str == "1") {
this.CreateCode();//生成验证码
}
}
protected override void Render(HtmlTextWriter writer)
{
if (!this.DesignMode) { //设计视图
writer.Write("<img border=\"1\" src=\"{0}\" style='width:100px height:30px'>",this.Page.Request.Path+"?_ImageTag=1");
}else{
writer.Write("验证码控件");
}
}
}
}
2,在服务器控件里面创建一个类,赋值如下
3,生成之后直接在网页拖动使用
4,调用 bool b = this.ValidateNumber1.CheckSn(this.TextBox1.Text);就OK
5,这个只是比较基础的自己弄的玩玩,不过也过得去
--------------------------------源码----------------------------------------
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.IO;
namespace ValidateNumber
{
public class ValidateNumber:Control
{
public void CreateCode() {
//1,定义验证码的颜色,早点,和显示的字符
//颜色列表,用于验证码,噪点,噪线
Color[] color = { Color.Black, Color.Red, Color.Black, Color.Green, Color.Orange, Color.Brown, Color.DarkBlue };
//字体列表,用于验证码
string[] font = { "Times New Roman", "MS Mincho", "Book Antiqua", "Gungsuh", "PMingLiu", "Impact" };
//验证码的字符集,去掉了一些容易混淆的字符
char[] character = { '2', '3', '4', '5', '6', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'J', 'K', 'L', 'M', 'N', 'P', 'R', 'S', 'T', 'W', 'X', 'Y' };
Random rnd = new Random();
//2,随即生成和保存验证码
string chkCode = "";
//生成验证字符串
for (int i = 0; i < 4; i++)
{
chkCode += character[rnd.Next(character.Length)];
}
//保存验证码采用cookie模式
HttpResponse resp = this.Page.Response;
resp.Cookies["validateCookie"].Value = chkCode;
resp.Cookies["validateCookie"].Expires = DateTime.Now.AddMinutes(2);
//3,绘制噪点,噪线
Bitmap bmp = new Bitmap(100, 30);//长100排序,宽30px的图片
Graphics g = Graphics.FromImage(bmp);
g.Clear(Color.White);
//画五条噪线
for (int i = 0; i < 5; i++)
{
//随机生成噪线的起点和终点
int x1 = rnd.Next(100);
int y1 = rnd.Next(30);
int x2 = rnd.Next(100);
int y2 = rnd.Next(30);
//随机生成噪线的颜色
Color clr = color[rnd.Next(color.Length)];
//画出噪线
g.DrawLine(new Pen(clr), x1, y1, x2, y2);
}
//4,画噪点
for (int i = 0; i < 100; 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);
}
//5,画验证字符串
for (int i = 0; i < chkCode.Length; i++)
{
string fnt = font[rnd.Next(font.Length)];
Font ft = new Font(fnt, 16);
Color clr = color[rnd.Next(color.Length)];
g.DrawString(chkCode[i].ToString(), ft, new SolidBrush(clr), (float)i * 20 + 20, (float)6);
}
//6,将验证码图片写入内存流,并将其以image/Png格式输出
MemoryStream ms = new MemoryStream();//内存流
try
{
bmp.Save(ms, System.Drawing.Imaging.ImageFormat.Png);
resp.ClearContent();
resp.ContentType = "image/Png";
resp.BinaryWrite(ms.ToArray());
resp.Flush();
resp.End();
}
finally
{
//释放资源
bmp.Dispose();
g.Dispose();
}
}
//7,实现验证方法
public bool CheckSn(string sn) {
return (sn.ToUpper() == this.Page.Request.Cookies["validateCookie"].Value.ToString().ToUpper());
}
//8,创建验证码
protected override void OnInit(EventArgs e)
{
base.OnInit(e);
//开发环境设计视图
if (this.DesignMode) {
return;
}
//如果不是验证码请求
string str=this.Page.Request.QueryString["_ImageTag"];
if (str == "1") {
this.CreateCode();//生成验证码
}
}
protected override void Render(HtmlTextWriter writer)
{
if (!this.DesignMode) { //设计视图
writer.Write("<img border=\"1\" src=\"{0}\" style='width:100px height:30px'>",this.Page.Request.Path+"?_ImageTag=1");
}else{
writer.Write("验证码控件");
}
}
}
}
相关文章推荐
- ASP.net验证码控件(附源代码)
- ASP.net验证码控件(附源代码)
- 打造自己的asp.net验证控件
- ASP.NET 2.0 Login控件添加验证码
- asp.net验证控件气泡提示效果源代码
- 常量,字段,构造方法 调试 ms 源代码 一个C#二维码图片识别的Demo 近期ASP.NET问题汇总及对应的解决办法 c# chart控件柱状图,改变柱子宽度 使用C#创建Windows服务 C#服务端判断客户端socket是否已断开的方法 线程 线程池 Task .NET 单元测试的利剑——模拟框架Moq
- [原创]共享一款ASP.NET验证码控件(附详细用法)
- asp.net扩展Login控件,添加图文验证码
- 『Asp.Net 组件』Asp.Net 服务器组件 内嵌JS:让自己的控件动起来
- ASP.NET验证码控件--WebValidates
- ASP.NET AJAX客户端编程之旅(六)——来做一个自己的客户端控件(3)
- 共享一款ASP.NET验证码控件(附详细用法)转
- C#实现的ASP.NET全能型验证码控件(09.11.22) (10.01.27最新修改)
- asp.net 验证码控件 ASP.net 2.0 开启多个使用图片验证码
- ASP.NET C#验证码控件使用
- 【Asp.net之旅】--因自己定义控件注冊而引发的思考
- 自己做ASP.NET客户端控件
- ASp.Net自定义验证码控件
- ASP.NET网站中的验证码源代码
- ASP.NET 2.0 下的验证码控件