博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
获取图形验证码
阅读量:6763 次
发布时间:2019-06-26

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

///         /// 获取图形验证码        ///         ///         /// 
[HttpPost] public IActionResult ValidateImgCode([FromBody]JObject postVal) { int width = 100; int height = 40; int fontsize = 20; string ImgCode = string.Empty; //第一种用内存流的返回值 // System.IO.MemoryStream ms = ValidateCode.Create_Validate_Graphic(out ImgCode, 4, width, height, fontsize); return File(ms.ToArray(), @"image/jpeg"); // //下面是用字节返回 // //byte[] bytes = ValidateCode.CreateValidateGraphic(out ImgCode, 4, width, height, fontsize); // return File(bytes, @"image/jpeg"); //return AsResult.Success(new //{ // CodeImg = ImgCode, // CodeByte= File(bytes, @"image/jpeg") //} //); // // }
///     /// 随机码和图片流生成    ///     public class ValidateCode    {                ///                 /// 产生图像验证码                ///                 /// 验证码。                /// 验证码元数。                ///                 ///                 ///                 /// 
public static byte[] CreateValidateGraphic(out String ImgCode, int CodeLength, int Width, int Height, int FontSize) { String sCode = String.Empty; //顏色列表,用于验证码噪点 噪线 Color[] oColors ={ Color.Black, Color.Red, Color.Blue, Color.Green, Color.Orange, Color.Brown, Color.Brown, 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; Point oPoint1 = default(Point); Point oPoint2 = default(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); //从Img对象生成新的Graphics对象 oGraphics = Graphics.FromImage(oBmp); //背景设为白色 oGraphics.Clear(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); } ImgCode = sCode;                //保存图片数据                MemoryStream stream = new MemoryStream(); oBmp.Save(stream, ImageFormat.Jpeg);                //输出图片流                return stream.ToArray(); } finally { oGraphics.Dispose(); oBmp.Dispose(); } } /// /// 内存流的方式返回 随机码和图片流生成 /// /// /// /// /// /// ///
public static MemoryStream Create_Validate_Graphic(out String ImgCode, int CodeLength, int Width, int Height, int FontSize) { String sCode = String.Empty; //顏色列表,用于验证码噪点 噪线 Color[] oColors ={ Color.Black, Color.Red, Color.Blue, Color.Green, Color.Orange, Color.Brown, Color.Brown, 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; Point oPoint1 = default(Point); Point oPoint2 = default(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); //从Img对象生成新的Graphics对象 oGraphics = Graphics.FromImage(oBmp); //背景设为白色 oGraphics.Clear(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); } ImgCode = sCode;                //保存图片数据 MemoryStream stream = null; stream = new MemoryStream(); oBmp.Save(stream, ImageFormat.Jpeg);                //输出图片流                return stream; } finally { oGraphics.Dispose(); oBmp.Dispose(); } } }

 

转载地址:http://rwbeo.baihongyu.com/

你可能感兴趣的文章
BaseAnimation是基于开源的APP,致力于收集各种动画效果(最新版本1.3) (转)
查看>>
Java
查看>>
HTTP Response Spliting 防范策略研究
查看>>
Libgdx window add alpha action change the background actor alpha
查看>>
(转)过滤器原理
查看>>
JavaScript 触发click事件 兼容FireFox,IE 和 Chrome
查看>>
【英语称谓】软件行业外企称谓简称
查看>>
实现打印级别且带图片的Excel 方案
查看>>
自动化CI构建工具
查看>>
批处理中的变量
查看>>
grub2手动引导ubuntu
查看>>
ORACLE SEQUENCE 介绍
查看>>
发个招聘信息
查看>>
JSEclipse—Eclipse上的JavaScript开发工具
查看>>
【NOIP模拟题】小象涂色(概率+期望+递推)
查看>>
Tomcat全攻略
查看>>
[CB2]start up
查看>>
转:高层游戏引擎——基于OGRE所实现的高层游戏引擎框架
查看>>
CodeCounter代码统计
查看>>
.Net垃圾收集机制—了解算法与代龄
查看>>