博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
mvc4验证码&输出图像的处理方式
阅读量:6923 次
发布时间:2019-06-27

本文共 2113 字,大约阅读时间需要 7 分钟。

///         /// 绘制验证码        ///         /// 
public ActionResult VerificationCode() { int _verificationLength = 6; int _width = 100, _height = 20; 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", 14, 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) / 2, (_height - _verificationTextSize.Height) / 2); _image.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg); return File(ms.ToArray(), "image/jpeg"); }
///         /// 获取验证码【字符串】        ///         /// 验证码长度【必须大于0】        /// 
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', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9' }; for (int i = 0; i < Length; i++) { _verification[i] = _dictionary[_random.Next(_dictionary.Length - 1)]; } return new string(_verification); }

 

转载于:https://www.cnblogs.com/mlfg/p/5231720.html

你可能感兴趣的文章
Android之service探究
查看>>
ASP.NET下Word文档的在线编辑、保存和全文关键字搜索的完整示例
查看>>
SYBASE存储过程详解
查看>>
集体智慧算法
查看>>
压缩原理及无线的一点思考
查看>>
ifcfg-p1p1
查看>>
Linux磁盘和文件系统
查看>>
LINUX iptables防火墙规则的匹配条件
查看>>
linux解决磁盘indoe满了问题
查看>>
mysql索引的类型和优缺点
查看>>
CentOS-6.5 安装教程-配置篇-网卡配置
查看>>
Java取整数
查看>>
redis的存储的5种数据类型
查看>>
情感+事业,强者必学的定律
查看>>
Discovery CentOS6.4 issue
查看>>
元芳,这个Bug你怎么看
查看>>
日志管理(二)
查看>>
Vert.x 用JWT验证Web请求 译<十>
查看>>
明星软件工程师的10种特质
查看>>
redis哈希(hash)
查看>>