Asp.Net Core 生成图形验证码

前几天有朋友问我怎么生成图片验证码,话不多说直接上代码。

支持.NET CORE开源。助力.NET Core社区发展。

 1 using System;
 2 using System.IO;
 3 using System.DrawingCore.Imaging;
 4 using System.DrawingCore;
 5 using CZFW.Framework.Model;
 6 
 7 namespace CZFW.Core.Security
 8 {
 9     /// <summary>
10     /// 图形验证码
11     /// </summary>
12     public class VerifyCode
13     {
14         public byte[] GetVerifyCode()
15         {
16             const int codeW = 80;
17             const int codeH = 30;
18             const int fontSize = 16;
19             string chkCode = string.Empty;
20             //颜色列表,用于验证码、噪线、噪点 
21             Color[] color = { Color.Black,Color.Red, Color.Blue, Color.Green, Color.Orange, Color.Brown, Color.Brown, Color.DarkBlue };
22             //字体列表,用于验证码 
23             string[] font = { "Times New Roman" };
24             //验证码的字符集,去掉了一些容易混淆的字符 
25             char[] character = { '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'd', 'e', 'f', 'h', 'k', 'm', 'n', 'r', 'x', 'y', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'J', 'K', 'L', 'M', 'N', 'P', 'R', 'S', 'T', 'W', 'X', 'Y' };
26             Random rnd = new Random();
27             //生成验证码字符串 
28             for (int i = 0; i < 4; i++)
29             {
30                 chkCode += character[rnd.Next(character.Length)];
31             }
32             //写入Session、验证码加密
33             WebHelper.WriteSession("czfw_session_verifycode", DesEncrypt.Encrypt(chkCode.ToLower(), "MD5"));
34             //创建画布
35             Bitmap bmp = new Bitmap(codeW, codeH);
36             Graphics g = Graphics.FromImage(bmp);
37             g.Clear(Color.White);
38             //画噪线 
39             for (int i = 0; i < 3; i++)
40             {
41                 int x1 = rnd.Next(codeW);
42                 int y1 = rnd.Next(codeH);
43                 int x2 = rnd.Next(codeW);
44                 int y2 = rnd.Next(codeH);
45                 Color clr = color[rnd.Next(color.Length)];
46                 g.DrawLine(new Pen(clr), x1, y1, x2, y2);
47             }
48             //画验证码字符串 
49             for (int i = 0; i < chkCode.Length; i++)
50             {
51                 string fnt = font[rnd.Next(font.Length)];
52                 Font ft = new Font(fnt, fontSize);
53                 Color clr = color[rnd.Next(color.Length)];
54                 g.DrawString(chkCode[i].ToString(), ft, new SolidBrush(clr), (float)i * 18, 0);
55             }
56             //将验证码图片写入内存流,并将其以 "image/Png" 格式输出 
57             MemoryStream ms = new MemoryStream();
58             try
59             {
60                 bmp.Save(ms, ImageFormat.Png);
61                 return ms.ToArray();
62             }
63             catch (Exception)
64             {
65                 return null;
66             }
67             finally
68             {
69                 g.Dispose();
70                 bmp.Dispose();
71             }
72         }
73     }
74 }

上面的是生成图片下面是在控制器中使用

  public ActionResult GetAuthCode()
        {
            return File(new VerifyCode().GetVerifyCode(), @"image/Gif");
        }

用File返回到web页面上就是一个验证码图片了。