参考这篇文章: http://www.cnblogs.com/yuangang/p/6000460.html

using System;
using System.IO;
using System.DrawingCore;
using System.DrawingCore.Imaging; namespace MobileWeb.Utility
{
public class Captcha
{ public string CreateValidateNumber(int length)
{
int[] randMembers = new int[length];
int[] validateNums = new int[length];
System.Text.StringBuilder validateNumberStr = new System.Text.StringBuilder();
//生成起始序列值
int seekSeek = unchecked((int)DateTime.Now.Ticks);
Random seekRand = new Random(seekSeek);
int beginSeek = (int)seekRand.Next(, Int32.MaxValue - length * );
int[] seeks = new int[length];
for (int i = ; i < length; i++)
{
beginSeek += ;
seeks[i] = beginSeek;
}
//生成随机数字
for (int i = ; i < length; i++)
{
Random rand = new Random(seeks[i]);
int pownum = * (int)Math.Pow(, length);
randMembers[i] = rand.Next(pownum, Int32.MaxValue);
}
//抽取随机数字
for (int i = ; i < length; i++)
{
string numStr = randMembers[i].ToString();
int numLength = numStr.Length;
Random rand = new Random();
int numPosition = rand.Next(, numLength - );
validateNums[i] = Int32.Parse(numStr.Substring(numPosition, ));
}
//生成验证码
for (int i = ; i < length; i++)
{
validateNumberStr.Append(validateNums[i].ToString());
}
return validateNumberStr.ToString();
} public byte[] CreateValidateGraphic(string validateNum)
{
Bitmap Img = null;
Graphics g = null;
MemoryStream ms = null;
Random random = new Random();
//验证码颜色集合
Color[] c = { Color.Black, Color.Red, Color.DarkBlue, Color.Green, Color.Orange, Color.Brown, Color.DarkCyan, Color.Purple }; //验证码字体集合
string[] fonts = { "Verdana", "Microsoft Sans Serif", "Comic Sans MS", "Arial", "宋体" }; //定义图像的大小,生成图像的实例
Img = new Bitmap((int)validateNum.Length * , ); g = Graphics.FromImage(Img);//从Img对象生成新的Graphics对象 g.Clear(Color.White);//背景设为白色 //在随机位置画背景点
for (int i = ; i < ; i++)
{
int x = random.Next(Img.Width);
int y = random.Next(Img.Height);
g.DrawRectangle(new Pen(Color.LightGray, ), x, y, , );
}
//验证码绘制在g中
for (int i = ; i < validateNum.Length; i++)
{
int cindex = random.Next();//随机颜色索引值
int findex = random.Next();//随机字体索引值
Font f = new Font(fonts[findex], , FontStyle.Bold);//字体
Brush b = new SolidBrush(c[cindex]);//颜色
int ii = ;
if ((i + ) % == )//控制验证码不在同一高度
{
ii = ;
}
g.DrawString(validateNum.Substring(i, ), f, b, + (i * ), ii);//绘制一个验证字符
}
ms = new MemoryStream();//生成内存流对象
Img.Save(ms, ImageFormat.Jpeg);//将此图像以Png图像文件的格式保存到流中 //回收资源
g.Dispose();
Img.Dispose();
return ms.ToArray();
}
} }

调用方法, Controller加一个方法, HTML里 <img id="cc_image" src="/product/ValidateCode" alt="点击切换验证码">

        public ActionResult ValidateCode()
{
Captcha captcha = new Captcha(); var code = captcha.CreateValidateNumber();
HttpContext.Session.SetString("ValidateCode", code);
return File(captcha.CreateValidateGraphic(code), "image/jpeg");
}

把旧系统迁移到.Net Core 2.0 日记 (13) --图形验证码的更多相关文章

  1. 把旧系统迁移到.Net Core 2.0 日记 (15) --Session 改用Redis

    安装Microsoft.Extensions.Caching.Redis.Core NuGet中搜索Microsoft.Extensions.Caching.Redis.Core并安装,此NuGet包 ...

  2. 把旧系统迁移到.Net Core 2.0 日记(1) - Startup.cs 解析

    因为自己到开发电脑转到Mac Air,之前的Webform/MVC应用在Mac 跑不起来,而且.Net Core 2.0 已经比较稳定了. 1. 为什么会有跨平台的.Net Core  近年来,我们已 ...

  3. 把旧系统迁移到.Net Core 2.0 日记 (12) --发布遇到的问题

    1. 开发时是在Mac+MySql, 尝试发布时是在SQL2005+Win 2008 (第一版) 在Startup.cs里,数据库连接要改,分页时netcore默认是用offset关键字分页, 如果用 ...

  4. 把旧系统迁移到.Net Core 2.0 日记 (17) --多租户和SoftDelete

    在EF Core 2.0版本中出现了全局过滤新特性即HasQueryFilter,它出现的意义在哪里?能够解决什么问题呢? 通过HasQueryFilter方法来创建过滤器能够允许我们对访问特定数据库 ...

  5. 把旧系统迁移到.Net Core 2.0 日记(10) -- EF core 和之前版本多对多映射区别

    EF Core 现在不支持多对多映射,只能做2个一对多映射. 比如Product和Category 我现在定义Product和Category是多对多关系. 那么实体定义如下: public clas ...

  6. 把旧系统迁移到.Net Core 2.0 日记 (18) --JWT 认证(Json Web Token)

    我们最常用的认证系统是Cookie认证,通常用一般需要人工登录的系统,用户访问授权范围的url时,会自动Redirect到Account/Login,登录后把认证结果存在cookie里. 系统只要找到 ...

  7. 把旧系统迁移到.Net Core 2.0 日记(8) - EASYUI datagrid+ Dapper+ 导出Excel

    迁移也没太大变化,有一个, 之前的Request.QueryString 是返回NameValueCollection, 现在则是返回整个字符串. 你要改成Request.Query[“key”] 直 ...

  8. 把旧系统迁移到.Net Core 2.0 日记(5) Razor/HtmlHelper/资源文件

    net core 的layout.cshtml文件有变化, 区分开发环境和非开发环境. 开发环境用的是非压缩的js和css, 正式环境用压缩的js和css <environment includ ...

  9. 把旧系统迁移到.Net Core 2.0 日记(4) - 使用EF+Mysql

    因为Mac 不能装SqlServer, 所以把数据库迁移到MySql,然后EntityFramework要改成Pomelo.EntityFrameworkCore.MySql 数据库迁移时,nvarc ...

随机推荐

  1. 单源最短路——Dijkstra模板

    算法思想: 类似最小生成树的贪心算法,从起点 v0 每次新拓展一个距离最小的点,再以这个点为中间点,更新起点到其他点的距离. 算法实现: 需要定义两个一维数组:①vis[ i ] 表示是否从源点到顶点 ...

  2. MYSQL常用函数(聚合函数(常用于GROUP BY从句的SELECT查询中))

    AVG(col)返回指定列的平均值 COUNT(col)返回指定列中非NULL值的个数 MIN(col)返回指定列的最小值 MAX(col)返回指定列的最大值 SUM(col)返回指定列的所有值之和 ...

  3. Access大数据高效分页语句

    Access大数据高效分页语句 oracle的分页查询可以利用rowid伪列. db2的分页查询可以利用row_number() over()聚合函数. mysql有limit. access仿佛先天 ...

  4. 日常英语---四、vis.js是什么

    日常英语---四.vis.js是什么 一.总结 一句话总结:A dynamic, browser based visualization library. 动态基于浏览器的可视库 http://vis ...

  5. Very Good Article on How Git Commands Work

    http://stackoverflow.com/questions/30038999/differences-between-commit-commit-and-push-commit-and-sy ...

  6. ubuntu 下Visual Studio Code 安装

    Build in Visual Studio Code Install VSCode The easiest way to install for Debian/Ubuntu based distri ...

  7. tchart2

  8. C#方式操作Cookie

    1.设置cookie public static void SetCookie(string TokenValue) { HttpCookie tokencookie = new HttpCookie ...

  9. android -------- android studio 中设置创建类时的说明信息(包含 作者 ,创建时间,注释说明等)

    今天简单来说一下android studio开发工具中的 一个小设置功能: 在开发过程中我们习惯给新建的类添加一些注释信息,创建日期.时间和作者等. 设置信息 File—>Settings—&g ...

  10. SVN入门使用

    1.安装客户端:TortoiseSVN-1.9.7.27907-x64-svn-1.9.7     2.安装服务器:Setup-Subversion-1.8.5.msi 下载地址:http://sou ...