/// <summary>
/// 生成验证码
/// </summary>
/// <param name="length">指定验证码的长度</param>
/// <returns></returns>
public string CreateValidateCode(int length)
{
int rand;
char code;
string randomcode = String.Empty;
//生成一定长度的验证码
System.Random random = new Random();
for (int i = ; i < length; i++)
{
rand = random.Next();
if (rand % == )
{
code = (char)('A' + (char)(rand % ));
}
else
{
code = (char)('' + (char)(rand % ));
}
randomcode += code.ToString();
}
return randomcode;
} /// <summary>
/// 创建验证码的图片(非MVC模式下使用)
/// </summary>
/// <param name="containsPage">要输出到的page对象</param>
/// <param name="validateCode">验证码</param>
/// <param name="fontSize">字体大小</param>
public void CreateValidateGraphic(HttpContext containsPage, string validateCode, int fontSize = )
{
Bitmap image = new Bitmap((int)Math.Ceiling(validateCode.Length * 21.0), (int)Math.Ceiling(fontSize * 1.8));
Graphics g = Graphics.FromImage(image);
try
{
//生成随机生成器
Random random = new Random();
//清空图片背景色
g.Clear(Color.White);
//画图片的干扰线
for (int 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 Font("Arial", fontSize, (FontStyle.Bold | FontStyle.Italic));
LinearGradientBrush brush = new LinearGradientBrush(new Rectangle(, , image.Width, image.Height),
Color.Blue, Color.DarkRed, 1.2f, true);
g.DrawString(validateCode, font, brush, , );
//画图片的前景干扰点
for (int i = ; i < ; i++)
{
int x = random.Next(image.Width);
int y = random.Next(image.Height);
image.SetPixel(x, y, Color.FromArgb(random.Next()));
}
//画图片的边框线
g.DrawRectangle(new Pen(Color.Silver), , , image.Width - , image.Height - );
//保存图片数据
MemoryStream stream = new MemoryStream();
image.Save(stream, ImageFormat.Jpeg);
//输出图片流
containsPage.Response.Clear();
containsPage.Response.ContentType = "image/jpeg";
containsPage.Response.BinaryWrite(stream.ToArray());
}
finally
{
g.Dispose();
image.Dispose();
}
} /// <summary>
/// 创建验证码的图片(MVC模式下使用)
/// </summary>
/// <param name="validateCode">验证码</param>
/// <param name="fontSize">字体大小</param>
public byte[] CreateValidateGraphic(string validateCode, int fontSize = )
{
Bitmap image = new Bitmap((int)Math.Ceiling(validateCode.Length * 21.0), (int)Math.Ceiling(fontSize * 1.8));
Graphics g = Graphics.FromImage(image);
try
{
//生成随机生成器
Random random = new Random();
//清空图片背景色
g.Clear(Color.White);
//画图片的干扰线
for (int 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 Font("Arial", fontSize, (FontStyle.Bold | FontStyle.Italic));
LinearGradientBrush brush = new LinearGradientBrush(new Rectangle(, , image.Width, image.Height),
Color.Blue, Color.DarkRed, 1.2f, true);
g.DrawString(validateCode, font, brush, , );
//画图片的前景干扰点
for (int i = ; i < ; i++)
{
int x = random.Next(image.Width);
int y = random.Next(image.Height);
image.SetPixel(x, y, Color.FromArgb(random.Next()));
}
//画图片的边框线
g.DrawRectangle(new Pen(Color.Silver), , , image.Width - , image.Height - );
//保存图片数据
MemoryStream stream = new MemoryStream();
image.Save(stream, ImageFormat.Jpeg);
//输出图片流
return stream.ToArray();
}
finally
{
g.Dispose();
image.Dispose();
}
}
         /// <summary>
/// 验证码Key
/// </summary>
public const string ValidateCodeKey = "ValidateCode"; /// <summary>
/// 获取验证码
/// </summary>
/// <returns></returns>
[OutputCache(Duration = )]
public ActionResult VerifyCode()
{
ValidateCode vCode = new ValidateCode();
string code = vCode.CreateValidateCode();
Session[ConstHelper.ValidateCodeKey] = code;
byte[] bytes = vCode.CreateValidateGraphic(code);
return File(bytes, @"image/jpeg");
}

C# 验证码生成(MVC和非MVC两种方式)的更多相关文章

  1. IOS xib生成界面和代码生成界面两种方式混合

    应用程序代理类 WKAppDelegate.m // // WKAppDelegate.m // HelloWorld // // Created by easy5 on 13-9-18. // Co ...

  2. 【吉光片羽】MVC 导出Word的两种方式

    1.直接将Html转成Word.MVC自带FileResult很好用.Html中我们也可以嵌入自己的样式. html: <div id="target"> <st ...

  3. Docker镜像构建的两种方式

    关于Docker里面的几个主要概念 这里用个不太恰当的比方来说明. 大家肯定安装过ghost系统,镜像就像是ghost文件,容器就像是ghost系统.你可以拿别人的ghost文件安装系统(使用镜像运行 ...

  4. 在基于MVC的Web项目中使用Web API和直接连接两种方式混合式接入

    在我之前介绍的混合式开发框架中,其界面是基于Winform的实现方式,后台使用Web API.WCF服务以及直接连接数据库的几种方式混合式接入,在Web项目中我们也可以采用这种方式实现混合式的接入方式 ...

  5. Asp.net MVC在Razor中输出Html的两种方式

    http://qubernet.blog.163.com/blog/static/177947284201485104616368/ Razor中所有的Html都会自动编码,这样就不需要我们手动去编码 ...

  6. ASP.NET 生成二维码(采用ThoughtWorks.QRCode和QrCode.Net两种方式)

    最近做项目遇到生成二维码的问题,发现网上用的最多的是ThoughtWorks.QRCode和QrCode.Net两种方式.访问官网看着例子写了两个Demo,使用过程中发现两个都挺好用的,Thought ...

  7. C# MVC 实现登录的5种方式

    最近悟出来一个道理,在这儿分享给大家:学历代表你的过去,能力代表你的现在,学习代表你的将来. 十年河东十年河西,莫欺少年穷.  学无止境,精益求精    小弟之前做过三月的MVC,后来又一直webFo ...

  8. cmd窗口使用sftp命令非密钥和密钥登录SFTP服务器的两种方式

    cmd窗口使用sftp命令非密钥和密钥登录SFTP服务器的两种方式 一.在Windows环境下搭建SFTP服务器可参见http://www.cnblogs.com/Kevin00/p/6341295. ...

  9. 【Spring】SpringMVC非注解配置的两种方式

    目录结构: contents structure [+] SpringMVC是什么 Spring MVC的设计原理 SpringMVC配置的第一种方式 1,复制Jar包 2,Web.xml文件 3,M ...

  10. 两种方式实现java生成Excel

    Web应用中难免会遇到需要将数据导出并生成excel文件的需求.同样,对于本博客中的总结,也是建立在为了完成这样的一个需求,才开始去了解其实现形式,并且顺利完成需求的开发,先将实现过程总结于此.本博文 ...

随机推荐

  1. 动作Action

    /** * DelayTime延迟 * @param d Duration 延迟时间 */ auto delayTime = DelayTime::create(); sprite->runAc ...

  2. (一)初步了解python

    python的创始人为吉多·范罗苏姆(Guido van Rossum).1989年的圣诞节期间,吉多·范罗苏姆为了在阿姆斯特丹打发时间,决心开发一个新的脚本解释程序,作为ABC语言的一种继承. Py ...

  3. 读取HttpWebResponse流的两种方法及注意的问题

    1.  获取流 HttpWebRequest request= (HttpWebRequest)WebRequest.Create(uri); //构建http request     request ...

  4. Hihocode 1015 KMP算法

    时间限制:1000ms 单点时限:1000ms 内存限制:256MB 描述 小Hi和小Ho是一对好朋友,出生在信息化社会的他们对编程产生了莫大的兴趣,他们约定好互相帮助,在编程的学习道路上一同前进. ...

  5. WordPress网站加速优化,一键免费使用七牛CDN插件

    利用wordpress搭建网站是个人建站的主流方案,我曾分享过wordpress网站加速优化必做的十件事,帮助了不少个人站长.今天介绍帮助wordpress网站提升速度至少10倍的免费CDN加速插件: ...

  6. 前端环境安装(node.js+npm+grunt+bower)

    前端开发环境安装(本教程不带开发工具的安装教程,只是环境安装) 本人机器环境win7 64位. 一.node.js安装 进入官网下载node.js文件,http://www.nodejs.org/ 2 ...

  7. Centos 下安装Zabbix Linux 客户端

    今天在linux上安装了客户端,过程如下: (1)下载zabbix客户端软件 wget www.zabbix.com/downloads/2.0.3/zabbix_agents_2.0.3.linux ...

  8. jQuery基础与常用方法学习笔记

    JQ与JS的关系:可以共存,不可混用.jq源码,源生JS面向对象写的   JQ写法 链式操作 $(‘#div’).html(‘hello’).css().click() 赋值取值合体 .html(‘h ...

  9. GitHub上优秀的ThirdParty

    仅为个人记录. 1.SwiftyJSON GitHub地址:https://github.com/SwiftyJSON/SwiftyJSON 2.CryptoSwift GitHub地址:https: ...

  10. 常用python处理try except异常的三种方式

    如果你在写python程序时遇到异常后想进行如下处理的话,一般用try来处理异常,假设有下面的一段程序: try:     语句1     语句2     .     .     语句N except ...