验证码-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 ...
随机推荐
- show-overflow-tooltip 宽度设置
设置样式:不能放在scoped中 <style lang="scss"> .el-tooltip__popper{max-width:80%}</style> ...
- ORACLE常识
1. ORACLE中查看表中的外键来源于哪些表 select cl.table_name from user_cons_columns cl left join user_constraints c ...
- 【转】使用JMeter 完成常用的压力测试(一)
本文介绍了 JMeter 相关的基本概念.并以 JMeter 为例,介绍了使用它来完成最常用的三种类型服务器,即 Web服务器.数据库服务器和消息中间件,压力测试的方法.步骤以及注意事项. 讲到测试, ...
- HDU 1878 欧拉回路(无向图的欧拉回路)
欧拉回路 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submis ...
- 杂项:UN-HTML
ylbtech-杂项:HTML 1.返回顶部 2.返回顶部 3.返回顶部 4.返回顶部 5.返回顶部 6.返回顶部 7.返回顶部 8.返回顶部 9.返回顶部 1 ...
- 1118 Birds in Forest
题意: 思路:并查集模板题. 代码: #include <cstdio> #include <algorithm> using namespace std; ; int fat ...
- composer 发布自己的开源软件
首先创建一个github项目. 在项目中,创建一个composer.json文件. { "name": "jiqing9006/valid", "de ...
- Java学习之App开发公司手机端设想
背景:最近在学JAVA,看到JAVA做各种APP,而公司软件主要是做家居设计,使用者多是设计师和家具门店,很难让大部分非专业人士接触到我们的产品,由于设计复杂且占用资源较多不太可能用APP实现网站设计 ...
- <%@ page import=""%>的用法
转自:https://blog.csdn.net/huihui870311/article/details/455642111 <jsp:directive.page import=" ...
- Maven整合SSM框架——详细整合教程(Spring+SpringMVC+MyBatis)
使用SSM(Spring.SpringMVC和Mybatis)已经有三个多月了,项目在技术上已经没有什么难点了,基于现有的技术就可以实现想要的功能,当然肯定有很多可以改进的地方.之前没有记录SSM整合 ...