/// <summary>
/// 绘制验证码
/// </summary>
/// <returns></returns>
public ActionResult VerificationCode()
{ int _verificationLength = ;
int _width = , _height = ;
SizeF _verificationTextSize;
Bitmap _bitmap = new Bitmap(Server.MapPath("~/Skins/Common/Texture.jpg"), true);
MemoryStream ms = new MemoryStream();
TextureBrush _brush = new TextureBrush(_bitmap);
//获取验证码
string _verificationText = CMS.Common.Text.VerificationText(_verificationLength);
//存储验证码
Session["VerificationCode"] = _verificationText.ToUpper();
Font _font = new Font("Arial", , FontStyle.Bold);
Bitmap _image = new Bitmap(_width, _height);
Graphics _g = Graphics.FromImage(_image);
//清空背景色
_g.Clear(Color.White);
//绘制验证码
_verificationTextSize = _g.MeasureString(_verificationText, _font);
_g.DrawString(_verificationText, _font, _brush, (_width - _verificationTextSize.Width) / , (_height - _verificationTextSize.Height) / ); _image.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg); return File(ms.ToArray(), "image/jpeg"); }
 /// <summary>
/// 获取验证码【字符串】
/// </summary>
/// <param name="Length">验证码长度【必须大于0】</param>
/// <returns></returns>
public static string VerificationText(int Length)
{
char[] _verification = new Char[Length];
Random _random = new Random();
char[] _dictionary = { 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', '', '', '', '', '', '', '', '', '', '' };
for (int i = ; i < Length; i++)
{
_verification[i] = _dictionary[_random.Next(_dictionary.Length - )];
}
return new string(_verification);
}

最新文章

  1. gulp插件autoprefixer
  2. 论文笔记Outline
  3. Jquery 复习练习(02)Javascript 与jquery 互转 onclick 与click区别
  4. 一步步构建自己的AngularJS(2)——scope之$watch及$digest
  5. Class文件结构
  6. centos的软件安装方法rpm和yum
  7. JQuery EasyUI弹出对话框解决Asp.net服务器控件无法执行后台代码的方法(转)
  8. How to installation V145 Renault CAN Clip diagnostic software
  9. eclipse jetty启动内存溢出
  10. Nginx/Apache图片缩略图技术
  11. protractor protractor.conf.js [launcher] Process exited with error code 1 undefined:1190
  12. 【翻译自mos文章】当指定asm disk 为FRA时,11.2.0.3的dbua hang住
  13. 数组名取地址所算数运算应注意的&amp;quot;trap&amp;quot;
  14. 十四、oracle 数据库管理--管理表空间和数据文件
  15. 微信JS初始化--微信JS系列文章(一)
  16. Java虚拟机之Java内存区域
  17. 初识webgl--我的webgl学习第一课(基于threeJs)
  18. RabbitMQ的介绍及使用进阶(Docker+.Net Core)
  19. hearbeat of RAC
  20. 2018.08.28 洛谷P3345 [ZJOI2015]幻想乡战略游戏(点分树)

热门文章

  1. 「小程序JAVA实战」小程序的组件(23)
  2. promtheus 配置文件
  3. C# XML文件读取
  4. Mysql EXPLAIN列的解释
  5. bootstrap下,对数组循环处理的方法
  6. IDEA快捷键【收藏】
  7. Spket,eclipse下安装Spket插件,格式化js
  8. 文件后缀与mime类型对应表
  9. etherboot无盘启动
  10. PythonScripter2.7报错ascii codec can&#39;t encode characters in position 0-1:ordinal not in range(128)