C# mvc 验证码2
public class ValidateCode
{
/// <summary>
/// 產生圖形驗證碼。
/// </summary>
/// <param name="Code">傳出驗證碼。</param>
/// <param name="CodeLength">驗證碼字元數。</param>
/// <param name="Width"></param>
/// <param name="Height"></param>
/// <param name="FontSize"></param>
/// <returns></returns>
public static byte[] CreateValidateGraphic(out String Code, int CodeLength, int Width, int Height, int FontSize)
{
String sCode = String.Empty;
//顏色列表,用於驗證碼、噪線、噪點
Color[] oColors ={
System.Drawing.Color.Black,
System.Drawing.Color.Red,
System.Drawing.Color.Blue,
System.Drawing.Color.Green,
System.Drawing.Color.Orange,
System.Drawing.Color.Brown,
System.Drawing.Color.Brown,
System.Drawing.Color.DarkBlue
};
//字體列表,用於驗證碼
string[] oFontNames = { "Times New Roman", "MS Mincho", "Book Antiqua", "Gungsuh", "PMingLiU", "Impact" };
//驗證碼的字元集,去掉了一些容易混淆的字元
char[] oCharacter = {
'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 oRnd = new Random();
Bitmap oBmp = null;
Graphics oGraphics = null;
int N1 = 0;
System.Drawing.Point oPoint1 = default(System.Drawing.Point);
System.Drawing.Point oPoint2 = default(System.Drawing.Point);
string sFontName = null;
Font oFont = null;
Color oColor = default(Color);
//生成驗證碼字串
for (N1 = 0; N1 <= CodeLength - 1; N1++)
{
sCode += oCharacter[oRnd.Next(oCharacter.Length)];
}
oBmp = new Bitmap(Width, Height);
oGraphics = Graphics.FromImage(oBmp);
oGraphics.Clear(System.Drawing.Color.White);
try
{
for (N1 = 0; N1 <= 4; N1++)
{
//畫噪線
oPoint1.X = oRnd.Next(Width);
oPoint1.Y = oRnd.Next(Height);
oPoint2.X = oRnd.Next(Width);
oPoint2.Y = oRnd.Next(Height);
oColor = oColors[oRnd.Next(oColors.Length)];
oGraphics.DrawLine(new Pen(oColor), oPoint1, oPoint2);
}
float spaceWith = 0, dotX = 0, dotY = 0;
if (CodeLength != 0)
{
spaceWith = (Width - FontSize * CodeLength - 10) / CodeLength;
}
for (N1 = 0; N1 <= sCode.Length - 1; N1++)
{
//畫驗證碼字串
sFontName = oFontNames[oRnd.Next(oFontNames.Length)];
oFont = new Font(sFontName, FontSize, FontStyle.Italic);
oColor = oColors[oRnd.Next(oColors.Length)];
dotY = (Height - oFont.Height) / 2 + 2;//中心下移2像素
dotX = Convert.ToSingle(N1) * FontSize + (N1 + 1) * spaceWith;
oGraphics.DrawString(sCode[N1].ToString(), oFont, new SolidBrush(oColor), dotX, dotY);
}
for (int i = 0; i <= 30; i++)
{
//畫噪點
int x = oRnd.Next(oBmp.Width);
int y = oRnd.Next(oBmp.Height);
Color clr = oColors[oRnd.Next(oColors.Length)];
oBmp.SetPixel(x, y, clr);
}
Code = sCode;
//保存图片数据
MemoryStream stream = new MemoryStream();
oBmp.Save(stream, ImageFormat.Jpeg);
//输出图片流
return stream.ToArray();
}
finally
{
oGraphics.Dispose();
}
}
}
//2.2图片流以图片的形式响应到页面
public class ValidateCodeController : Controller
{
public ActionResult GetImg()
{
int width = ConverterHelper.ObjToInt(Request.Params["width"], 100);
int height = ConverterHelper.ObjToInt(Request.Params["height"], 40);
int fontsize = ConverterHelper.ObjToInt(Request.Params["fontsize"], 20);
string code = string.Empty;
byte[] bytes = ValidateCode.CreateValidateGraphic(out code, 4, width, height, fontsize);
SessionHelper.SetValiCode(code);
return File(bytes, @"image/jpeg");
}
}
//2.3页面显示及刷新(img+js)
<img id="GL_StandardCode" style="cursor: pointer;"
src="@Url.Action("GetImg", "ValidateCode")?t=@DateTime.Now.Ticks"
title="看不清,点击换一张" />
$("#GL_StandardCode").click(function () {
var newSrc = "@Url.Action("GetImg", "ValidateCode")" + "?t=" + (new Date()).getTime();
this.src=newSrc;
return false;
});
//2.4登录时判断SESSION值
string pCode = Request.Params["GL_CodeInput"];
string sCode = SessionHelper.GetValiCode();
if (string.IsNullOrEmpty(pCode))
{
resultMsg = "请输入验证码";
}
else if (string.IsNullOrEmpty(sCode))
{
resultMsg = "验证码过期";
}
else if (pCode.ToLower() != sCode.ToLower())
{
resultMsg = "验证码不正确";
}
C# mvc 验证码2的更多相关文章
- MVC 验证码实现( 简易版)
现在网站上越来越多的验证码,使用场景也是越来越多,登陆.注册.上传.下载...等等地方,都有可能大量使用到验证码,那么制作验证码到底有多简单呢?我们一起来看下最简易版的验证码实现过程- 验证码的基本步 ...
- ASP.NET MVC验证码演示(Ver2)
前一版本<ASP.NET MVC验证码演示>http://www.cnblogs.com/insus/p/3622116.html,Insus.NET还是使用了Generic handle ...
- .NET MVC 验证码
.NET MVC 验证码
- MVC验证码的编写
主要是相互学习一下mvc,希望各位大神指导 /// <summary> /// 生成随机数字 /// </summary> /// <returns>随机数字< ...
- ASP.NET MVC验证码演示
我们在网站登录或理一个评论时,可以放置一个验证码(Captcha),可以为系统免去那些恶意刷新等功能. 今次Insus.NET在asp.net mvc应用程序实现与演示验证码的产生以及应用等 . 前天 ...
- ASP.NET mvc 验证码 (转)
ASP.net 验证码(C#) MVC http://blog.163.com/xu_shuhao/blog/static/5257748720101022697309/ 网站添加验证码,主要为防止机 ...
- mvc验证码图片生成
/// <summary> ///生成验证码 /// </summary> public class VerifyCode { /// <summary> /// ...
- asp.net mvc 验证码
效果图 验证码类 namespace QJW.VerifyCode { //用法: //public FileContentResult CreateValidate() //{ // Validat ...
- 简单C#、asp.net mvc验证码的实现
using System;using System.Collections.Generic;using System.Linq;using System.Web;using System.Text;u ...
- C# mvc 验证码3
//// <summary> /// 生成验证码 /// </summary> /// <param name="length">指定验证码的长 ...
随机推荐
- PostgreSQL相关的软件,库,工具和资源集合
PostgreSQL相关的软件,库,工具和资源集合. 备份 wal-e - Simple Continuous Archiving for Postgres to S3, Azure, or Swif ...
- HTML5的渐变色 渐变的两种类型 createLinearGradient 和createRadialGradient
今天又再看了html5的颜色渐变API,发现没有第一次看那么复杂. 不过我对这个颜色渐变存在着一个疑惑就是两种色带之间,那段是属于两种颜色混合的,有点模糊. 比如从红色变成黄色,在红与黄之间的那个地方 ...
- C# winform如何在textbox中判断输入的是字母还是数字?
1.用正规式using System.Text.RegularExpressions; string pattern = @"^\d+(\.\d)?$";if(Text1.Text ...
- [Javascript] Linting JavaScript with ESLint
ESLint is a JavaScript linter (static analysis tool) that offers full support for ES6, JSX, and othe ...
- sort()排序 collections.sort();
1.main方法: public class Test { public static void main(String[] args) { /** * * sort()方法详解 * 1.Collec ...
- spring core源码解读之ASM4用户手册翻译之一asm简介
第一章:ASM介绍 1.1 ASM动机: 程序的分析,生成,转换技术可以应用到许多场景: 1.程序分析,从简单的语法解析到完整的语义分析,可以应用在程序中找到潜在的bug,发现无用的代码,工程代码的逆 ...
- Java基础知识强化之IO流笔记23:计算机是如何识别把两个字节拼接为中文(附加)
1. 计算机是如何识别什么时候该把两个字节转换为一个中文呢? 在计算机中中文的存储分两个字节: • 第一个字节肯定是负数. • 第二个字节常见的是负数,可能有正数.但是没影响. 2. 代码示例: pa ...
- cocos2dx 文件处理
问题1:fopen 在vs下使用fopen进行文件处理,跑通了,但是移植到android源码下时就出现了一大推问题,首先需要理解的是在vs下开发资源是存放在执行文件的相同目录下的,而移植到androi ...
- Windows2012中Python2.7.11+Python3.4.4+Pycharm
下载软件包 Python2.7.11: https://www.python.org/ftp/python/2.7.11/python-2.7.11.amd64.msi Python3.4.4: ...
- PL/SQL Developer 远程连接Oracle数据库
PL/SQL Developer 远程连接Oracle数据库 网上搜了很多方法,这个可行! 1. 配置服务器tnsnames.ora文件,如果本机上没有安装oracle,可以从安装了oracle ...