验证码-WebVcode
验证码的实现
<img src="../Common/WebVcode.aspx" title="看不清?点此更换" alt="看不清?点此更换" onclick="this.src='/Common/WebVcode.aspx?a='+Math.random();" />
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
Session["Vcode"] = CreateImage(, VerificationType.Numeric);
}
}
/// <summary>
/// 验证码的类型
/// </summary>
public enum VerificationType
{
/// <summary>
/// 只有数字
/// </summary>
Numeric,
/// <summary>
/// 数字和英文字符
/// </summary>
NumericAndChar,
/// <summary>
/// 中文字符
/// </summary>
ChineseChar
}
/// <summary>
/// 生成一个随机文字图片,保存在 Session["code1"]
/// </summary>
/// <param name="count">图片中字的个数</param>
/// <returns>生成的文字</returns>
public string CreateImage(int count, VerificationType type)
{
string ValidCode = GenCode(count, type);
switch (type)
{
case VerificationType.Numeric:
CreateCheckCodeImage(ValidCode, 13.5);
break;
case VerificationType.NumericAndChar:
CreateCheckCodeImage(ValidCode, );
break;
case VerificationType.ChineseChar:
CreateCheckCodeImage(ValidCode, 22.5);
break;
default:
break;
}
return ValidCode;
}
/// <summary>
/// 产生随机字符串
/// </summary>
/// <param name="num">随机出几个字符</param>
/// <returns>随机出的字符串</returns>
private string GenCode(int num, VerificationType type)
{
string str = string.Empty;
switch (type)
{
case VerificationType.Numeric:
str = "";
break;
case VerificationType.NumericAndChar:
str = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
break;
case VerificationType.ChineseChar:
break;
default:
str = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
break;
}
char[] chastr = str.ToCharArray();
string code = "";
Random rd = new Random();
int i;
for (i = ; i < num; i++)
{
code += str.Substring(rd.Next(, str.Length), );
}
return code;
}
/// <summary>
/// 生成图片(增加背景噪音线、前景噪音点)
/// </summary>
/// <param name="checkCode">随机出字符串</param>
private void CreateCheckCodeImage(string checkCode, double codeWidth)
{
if (checkCode.Trim() == "" || checkCode == null)
return;
System.Drawing.Bitmap image = new System.Drawing.Bitmap((int)(checkCode.Length * codeWidth), );
Graphics g = Graphics.FromImage(image);
try
{
//生成随机生成器
Random random = new Random();
//清空图片背景色
g.Clear(Color.White);
//画图片的背景噪音线
int i;
for (i = ; i < ; i++)
{
int x1 = random.Next(image.Width);
int x2 = random.Next(image.Width);
int y1 = random.Next(image.Height);
int y2 = random.Next(image.Height);
g.DrawLine(new Pen(Color.Silver), x1, y1, x2, y2);
}
Font font = new System.Drawing.Font("Verdana", , (System.Drawing.FontStyle.Bold));
System.Drawing.Drawing2D.LinearGradientBrush brush = new System.Drawing.Drawing2D.LinearGradientBrush(new Rectangle(, , image.Width, image.Height), Color.Blue, Color.DarkRed, 1.2F, true);
g.DrawString(checkCode, font, brush, , , new System.Drawing.StringFormat());
//画图片的前景噪音点
g.DrawRectangle(new Pen(Color.Silver), , , image.Width - , image.Height - );
System.IO.MemoryStream ms = new System.IO.MemoryStream();
image.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);
HttpContext.Current.Response.ExpiresAbsolute = System.DateTime.Now.AddMilliseconds();
//清除该页输出缓存,设置该页无缓存
Response.Buffer = true;
Response.ExpiresAbsolute = System.DateTime.Now.AddMilliseconds();
Response.Expires = ;
Response.CacheControl = "no-cache";
Response.AppendHeader("Pragma", "No-Cache");
HttpContext.Current.Response.ClearContent();
HttpContext.Current.Response.ContentType = "image/JPEG";
HttpContext.Current.Response.BinaryWrite(ms.ToArray());
g.Dispose();
image.Dispose();
}
catch
{
g.Dispose();
image.Dispose();
}
}
验证码-WebVcode的更多相关文章
- .net点选验证码实现思路分享
哈哈好久没冒泡了,最进看见点选验证码有点意思,所以想自己写一个. 先上效果图 如果你被这个效果吸引了就请继续看下去. 贴代码前先说点思路: 1.要有一个汉字库,并按字形分类.(我在数据库里是安部首分类 ...
- 【探索】无形验证码 —— PoW 算力验证
先来思考一个问题:如何写一个能消耗对方时间的程序? 消耗时间还不简单,休眠一下就可以了: Sleep(1000) 这确实消耗了时间,但并没有消耗 CPU.如果对方开了变速齿轮,这瞬间就能完成. 不过要 ...
- TODO:Laravel增加验证码
TODO:Laravel增加验证码1. 先聊聊验证码是什么,有什么作用?验证码(CAPTCHA)是"Completely Automated Public Turing test to te ...
- PHP-解析验证码类--学习笔记
1.开始 在 网上看到使用PHP写的ValidateCode生成验证码码类,感觉不错,特拿来分析学习一下. 2.类图 3.验证码类部分代码 3.1 定义变量 //随机因子 private $char ...
- 随手记_C#验证码
前言 最近在网上偶然看见一个验证码,觉得很有意思,于是搜了下,是使用第三方实现的,先看效果: 总体来说效果还是可以的,官方提供的SDK也比较详细,可配置性很高.在这里在简单啰嗦几句使用方式: 使用步骤 ...
- WPF做12306验证码点击效果
一.效果 和12306是一样的,运行一张图上点击多个位置,横线以上和左边框还有有边框位置不允许点击,点击按钮输出坐标集合,也就是12306登陆的时候,需要向后台传递的参数. 二.实现思路 1.获取验证 ...
- 零OCR基础6行代码实现C#验证码识别
这两天因为工作需要,要到某个网站采集信息,一是要模拟登陆,二是要破解验证码,本想用第三方付费打码,但是想想网上免费的代码也挺多的,于是乎准备从网上撸点代码下来,谁知道,撸了好多个都不行,本人以前也没接 ...
- ASP.NET中画图形验证码
context.Response.ContentType = "image/jpeg"; //生成随机的中文验证码 string yzm = "人口手大小多少上中下男女天 ...
- asp.net mvc 验证码
效果图 验证码类 namespace QJW.VerifyCode { //用法: //public FileContentResult CreateValidate() //{ // Validat ...
随机推荐
- DataSnap侦听端口动态设置问题
使用DataSnap做服务器,端口设置为可配置,然后在程序中动态指定: procedure TscUPower.DataModuleCreate(Sender: TObject); begin dst ...
- TXT
ANDRIOD: 192.168.199.119 data50803360 zc_3floor kk4836kk kahuna kk1626kk
- python 类实例化,修改属性值
class User(object): def __init__(self, first_name, last_name, login_attempts): self.first_name = fir ...
- MyBatis 一对一(OneToOne)__SELECT
1.创建SQL脚本: CREATE TABLE t_person( id int(3) not null auto_increment, name varchar(20) default null ...
- python开发线程:死锁和递归锁&信号量&定时器&线程queue&事件evevt
一 死锁现象与递归锁 进程也有死锁与递归锁,在进程那里忘记说了,放到这里一切说了额 所谓死锁: 是指两个或两个以上的进程或线程在执行过程中,因争夺资源而造成的一种互相等待的现象,若无外力作用,它们都将 ...
- JavaScript笔记——面向对象与原型
JavaScript也是一门面向对象的语言.面向对象的语言有一个标志,那就是类的概念,而通过类可以创建任意多个具有相同属性和方法的对象.但是,JavaScript竟然没有class,因此它的面向对象也 ...
- C# IP地址去掉端口号
string Ip1 = "192.168.0.199:7777"; string Ip2 = Ip1.Remove(Ip1.IndexOf(':'));
- DIKW模型与数据工程
DIKW 体系 DIKW体系是关于数据.信息.知识及智慧的体系,可以追溯至托马斯·斯特尔那斯·艾略特所写的诗--<岩石>.在首段,他写道:“我们在哪里丢失了知识中的智慧?又在哪里丢失了信息 ...
- Python基础学习七 Excel操作
python操作excel,python操作excel使用xlrd.xlwt和xlutils模块, xlrd模块是读取excel的,xlwt模块是写excel的,xlutils是用来修改excel的. ...
- 3-1 Git下载与安装
https://desktop.github.com/