/// <summary> /// 獲取圖形驗證碼 /// </summary> /// <param name="postVal"></param> /// <returns></returns> [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") //} //); ////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////// }
/// <summary> /// 隨機碼和圖片流生成 /// </summary> public class ValidateCode { /// <summary> /// 產生圖像驗證碼 /// </summary> /// <param name="Code">驗證碼。</param> /// <param name="CodeLength">驗證碼元數。</param> /// <param name="Width"></param> /// <param name="Height"></param> /// <param name="FontSize"></param> /// <returns></returns> 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(); } } /// <summary> /// 內存流的方式返回 隨機碼和圖片流生成 /// </summary> /// <param name="ImgCode"></param> /// <param name="CodeLength"></param> /// <param name="Width"></param> /// <param name="Height"></param> /// <param name="FontSize"></param> /// <returns></returns> 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(); } } }