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頁面上就是一個驗證碼圖片了。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM