一区二区三区在线-一区二区三区亚洲视频-一区二区三区亚洲-一区二区三区午夜-一区二区三区四区在线视频-一区二区三区四区在线免费观看

服務(wù)器之家:專注于服務(wù)器技術(shù)及軟件下載分享
分類導(dǎo)航

PHP教程|ASP.NET教程|Java教程|ASP教程|編程技術(shù)|正則表達(dá)式|C/C++|IOS|C#|Swift|Android|VB|R語言|JavaScript|易語言|vb.net|

服務(wù)器之家 - 編程語言 - ASP.NET教程 - .NET Core 2.0如何生成圖片驗證碼完整實例

.NET Core 2.0如何生成圖片驗證碼完整實例

2020-05-28 15:05ChaITSimpleLove ASP.NET教程

這篇文章主要給大家介紹了關(guān)于.NET Core 2.0如何生成圖片驗證碼的相關(guān)資料,該功能主要是利用ZKWeb.System.Drawing來實現(xiàn),文中給出了詳細(xì)的示例代碼供大家參考學(xué)習(xí),需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧

前言

圖片驗證碼在我們?nèi)粘i_發(fā)中是必不可少會遇見的一個功能,最近工作中就遇到了這個需求,所以下面將實現(xiàn)的方法分享給大家,話不多說了,來一起看看詳細(xì)的介紹吧。

.NET Core 2.0生成圖片驗證碼

NuGet包引入:ZKWeb.System.Drawing,如下所示:

.NET Core 2.0如何生成圖片驗證碼完整實例

代碼實例如下:VerifyCodeHelper

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
using System;
using System.DrawingCore;
using System.DrawingCore.Drawing2D;
using System.DrawingCore.Imaging;
using System.IO;
 
namespace Common.Helper
{
 public sealed class VerifyCodeHelper
 {
  #region 單例模式
  //創(chuàng)建私有化靜態(tài)obj鎖
  private static readonly object _ObjLock = new object();
  //創(chuàng)建私有靜態(tài)字段,接收類的實例化對象
  private static VerifyCodeHelper _VerifyCodeHelper = null;
  //構(gòu)造函數(shù)私有化
  private VerifyCodeHelper() { }
  //創(chuàng)建單利對象資源并返回
  public static VerifyCodeHelper GetSingleObj()
  {
   if (_VerifyCodeHelper == null)
   {
    lock (_ObjLock)
    {
     if (_VerifyCodeHelper == null)
      _VerifyCodeHelper = new VerifyCodeHelper();
    }
   }
   return _VerifyCodeHelper;
  }
  #endregion
 
  #region 生產(chǎn)驗證碼
  public enum VerifyCodeType { NumberVerifyCode, AbcVerifyCode, MixVerifyCode };
 
  /// <summary>
  /// 1.數(shù)字驗證碼
  /// </summary>
  /// <param name="length"></param>
  /// <returns></returns>
  private string CreateNumberVerifyCode(int length)
  {
   int[] randMembers = new int[length];
   int[] validateNums = new int[length];
   string validateNumberStr = "";
   //生成起始序列值
   int seekSeek = unchecked((int)DateTime.Now.Ticks);
   Random seekRand = new Random(seekSeek);
   int beginSeek = seekRand.Next(0, Int32.MaxValue - length * 10000);
   int[] seeks = new int[length];
   for (int i = 0; i < length; i++)
   {
    beginSeek += 10000;
    seeks[i] = beginSeek;
   }
   //生成隨機數(shù)字
   for (int i = 0; i < length; i++)
   {
    Random rand = new Random(seeks[i]);
    int pownum = 1 * (int)Math.Pow(10, length);
    randMembers[i] = rand.Next(pownum, Int32.MaxValue);
   }
   //抽取隨機數(shù)字
   for (int i = 0; i < length; i++)
   {
    string numStr = randMembers[i].ToString();
    int numLength = numStr.Length;
    Random rand = new Random();
    int numPosition = rand.Next(0, numLength - 1);
    validateNums[i] = Int32.Parse(numStr.Substring(numPosition, 1));
   }
   //生成驗證碼
   for (int i = 0; i < length; i++)
   {
    validateNumberStr += validateNums[i].ToString();
   }
   return validateNumberStr;
  }
 
  /// <summary>
  /// 2.字母驗證碼
  /// </summary>
  /// <param name="length">字符長度</param>
  /// <returns>驗證碼字符</returns>
  private string CreateAbcVerifyCode(int length)
  {
   char[] verification = new char[length];
   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'
   };
   Random random = new Random();
   for (int i = 0; i < length; i++)
   {
    verification[i] = dictionary[random.Next(dictionary.Length - 1)];
   }
   return new string(verification);
  }
 
  /// <summary>
  /// 3.混合驗證碼
  /// </summary>
  /// <param name="length">字符長度</param>
  /// <returns>驗證碼字符</returns>
  private string CreateMixVerifyCode(int length)
  {
   char[] verification = new char[length];
   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',
    '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
    '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'
   };
   Random random = new Random();
   for (int i = 0; i < length; i++)
   {
    verification[i] = dictionary[random.Next(dictionary.Length - 1)];
   }
   return new string(verification);
  }
 
  /// <summary>
  /// 產(chǎn)生驗證碼(隨機產(chǎn)生4-6位)
  /// </summary>
  /// <param name="type">驗證碼類型:數(shù)字,字符,符合</param>
  /// <returns></returns>
  public string CreateVerifyCode(VerifyCodeType type)
  {
   string verifyCode = string.Empty;
   Random random = new Random();
   int length = random.Next(4, 6);
   switch (type)
   {
    case VerifyCodeType.NumberVerifyCode:
     verifyCode = GetSingleObj().CreateNumberVerifyCode(length);
     break;
    case VerifyCodeType.AbcVerifyCode:
     verifyCode = GetSingleObj().CreateAbcVerifyCode(length);
     break;
    case VerifyCodeType.MixVerifyCode:
     verifyCode = GetSingleObj().CreateMixVerifyCode(length);
     break;
   }
   return verifyCode;
  }
  #endregion
 
  #region 驗證碼圖片
  /// <summary>
  /// 驗證碼圖片 => Bitmap
  /// </summary>
  /// <param name="verifyCode">驗證碼</param>
  /// <param name="width">寬</param>
  /// <param name="height">高</param>
  /// <returns>Bitmap</returns>
  public Bitmap CreateBitmapByImgVerifyCode(string verifyCode, int width, int height)
  {
   Font font = new Font("Arial", 14, (FontStyle.Bold | FontStyle.Italic));
   Brush brush;
   Bitmap bitmap = new Bitmap(width, height);
   Graphics g = Graphics.FromImage(bitmap);
   SizeF totalSizeF = g.MeasureString(verifyCode, font);
   SizeF curCharSizeF;
   PointF startPointF = new PointF(0, (height - totalSizeF.Height) / 2);
   Random random = new Random(); //隨機數(shù)產(chǎn)生器
   g.Clear(Color.White); //清空圖片背景色
   for (int i = 0; i < verifyCode.Length; i++)
   {
    brush = new LinearGradientBrush(new Point(0, 0), new Point(1, 1), Color.FromArgb(random.Next(255), random.Next(255), random.Next(255)), Color.FromArgb(random.Next(255), random.Next(255), random.Next(255)));
    g.DrawString(verifyCode[i].ToString(), font, brush, startPointF);
    curCharSizeF = g.MeasureString(verifyCode[i].ToString(), font);
    startPointF.X += curCharSizeF.Width;
   }
 
   //畫圖片的干擾線
   for (int i = 0; i < 10; i++)
   {
    int x1 = random.Next(bitmap.Width);
    int x2 = random.Next(bitmap.Width);
    int y1 = random.Next(bitmap.Height);
    int y2 = random.Next(bitmap.Height);
    g.DrawLine(new Pen(Color.Silver), x1, y1, x2, y2);
   }
 
   //畫圖片的前景干擾點
   for (int i = 0; i < 100; i++)
   {
    int x = random.Next(bitmap.Width);
    int y = random.Next(bitmap.Height);
    bitmap.SetPixel(x, y, Color.FromArgb(random.Next()));
   }
 
   g.DrawRectangle(new Pen(Color.Silver), 0, 0, bitmap.Width - 1, bitmap.Height - 1); //畫圖片的邊框線
   g.Dispose();
   return bitmap;
  }
 
  /// <summary>
  /// 驗證碼圖片 => byte[]
  /// </summary>
  /// <param name="verifyCode">驗證碼</param>
  /// <param name="width">寬</param>
  /// <param name="height">高</param>
  /// <returns>byte[]</returns>
  public byte[] CreateByteByImgVerifyCode(string verifyCode, int width, int height)
  {
   Font font = new Font("Arial", 14, (FontStyle.Bold | FontStyle.Italic));
   Brush brush;
   Bitmap bitmap = new Bitmap(width, height);
   Graphics g = Graphics.FromImage(bitmap);
   SizeF totalSizeF = g.MeasureString(verifyCode, font);
   SizeF curCharSizeF;
   PointF startPointF = new PointF(0, (height - totalSizeF.Height) / 2);
   Random random = new Random(); //隨機數(shù)產(chǎn)生器
   g.Clear(Color.White); //清空圖片背景色
   for (int i = 0; i < verifyCode.Length; i++)
   {
    brush = new LinearGradientBrush(new Point(0, 0), new Point(1, 1), Color.FromArgb(random.Next(255), random.Next(255), random.Next(255)), Color.FromArgb(random.Next(255), random.Next(255), random.Next(255)));
    g.DrawString(verifyCode[i].ToString(), font, brush, startPointF);
    curCharSizeF = g.MeasureString(verifyCode[i].ToString(), font);
    startPointF.X += curCharSizeF.Width;
   }
 
   //畫圖片的干擾線
   for (int i = 0; i < 10; i++)
   {
    int x1 = random.Next(bitmap.Width);
    int x2 = random.Next(bitmap.Width);
    int y1 = random.Next(bitmap.Height);
    int y2 = random.Next(bitmap.Height);
    g.DrawLine(new Pen(Color.Silver), x1, y1, x2, y2);
   }
 
   //畫圖片的前景干擾點
   for (int i = 0; i < 100; i++)
   {
    int x = random.Next(bitmap.Width);
    int y = random.Next(bitmap.Height);
    bitmap.SetPixel(x, y, Color.FromArgb(random.Next()));
   }
 
   g.DrawRectangle(new Pen(Color.Silver), 0, 0, bitmap.Width - 1, bitmap.Height - 1); //畫圖片的邊框線
   g.Dispose();
 
   //保存圖片數(shù)據(jù)
   MemoryStream stream = new MemoryStream();
   bitmap.Save(stream, ImageFormat.Jpeg);
   //輸出圖片流
   return stream.ToArray();
 
  }
  #endregion
 }
}

新建控制器:VerifyCodeController,

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
using System.DrawingCore.Imaging;
using System.IO;
using Common.Helper;
using Microsoft.AspNetCore.Mvc;
 
namespace WebApplicationApi.Controllers
{
 public class VerifyCodeController : Controller
 {
  public IActionResult Index()
  {
   return View();
  }
 
  /// <summary>
  /// 數(shù)字驗證碼
  /// </summary>
  /// <returns></returns>
  public FileContentResult NumberVerifyCode()
  {
   string code = VerifyCodeHelper.GetSingleObj().CreateVerifyCode(VerifyCodeHelper.VerifyCodeType.NumberVerifyCode);
   byte[] codeImage = VerifyCodeHelper.GetSingleObj().CreateByteByImgVerifyCode(code, 100, 40);
   return File(codeImage, @"image/jpeg");
  }
 
  /// <summary>
  /// 字母驗證碼
  /// </summary>
  /// <returns></returns>
  public FileContentResult AbcVerifyCode()
  {
   string code = VerifyCodeHelper.GetSingleObj().CreateVerifyCode(VerifyCodeHelper.VerifyCodeType.AbcVerifyCode);
   var bitmap = VerifyCodeHelper.GetSingleObj().CreateBitmapByImgVerifyCode(code, 100, 40);
   MemoryStream stream = new MemoryStream();
   bitmap.Save(stream, ImageFormat.Png);
   return File(stream.ToArray(), "image/png");
  }
 
  /// <summary>
  /// 混合驗證碼
  /// </summary>
  /// <returns></returns>
  public FileContentResult MixVerifyCode()
  {
   string code = VerifyCodeHelper.GetSingleObj().CreateVerifyCode(VerifyCodeHelper.VerifyCodeType.MixVerifyCode);
   var bitmap = VerifyCodeHelper.GetSingleObj().CreateBitmapByImgVerifyCode(code, 100, 40);
   MemoryStream stream = new MemoryStream();
   bitmap.Save(stream, ImageFormat.Gif);
   return File(stream.ToArray(), "image/gif");
  }
 
 }
}

添加頁面index.cshtml,如下代碼:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
@{
 Layout = null;
}
<!DOCTYPE html>
<html>
<head>
 <meta name="viewport" content="width=device-width" />
 <title>Index</title>
</head>
<body>
 <figure>
  <h3>圖片驗證碼</h3>
  <fieldset>
   <legend>數(shù)字驗證碼</legend>
   <img id="nubImg" title="數(shù)字驗證碼" src="/VerifyCode/NumberVerifyCode?random=" alt="vcode" onclick="refresh()" style="cursor:pointer;" />
  </fieldset>
  <fieldset>
   <legend>數(shù)字驗證碼</legend>
   <img id="abcImg" title="字母驗證碼" src="/VerifyCode/AbcVerifyCode" alt="vcode" onclick="this.src=this.src+'?'" style="cursor:pointer;" />
  </fieldset>
  <fieldset>
   <legend>混合驗證碼</legend>
   <img id="mixImg" title="數(shù)字字母混合驗證碼" src="/VerifyCode/MixVerifyCode" alt="vcode" onclick="this.src=this.src+'?'" style="cursor:pointer;" />
  </fieldset>
 </figure>
 
 <script type="text/javascript">
  function refresh() {
   var id = document.getElementById("nubImg");
   var str = "/VerifyCode/NumberVerifyCode?random=" + Math.random();
   id.setAttribute("src", str);
  }
 </script>
 
</body>
</html>

生成驗證碼如下所示:

.NET Core 2.0如何生成圖片驗證碼完整實例

總結(jié)

以上就是這篇文章的全部內(nèi)容了,希望本文的內(nèi)容對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,如果有疑問大家可以留言交流,謝謝大家對服務(wù)器之家的支持。

原文鏈接:https://blog.csdn.net/chaitsimplelove/article/details/80531791

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 青青草国产免费久久久91 | 娇喘嗯嗯 轻点啊视频福利 九九九九在线精品免费视频 | 校园全肉高h湿一女多男 | 91麻豆精品激情在线观看最新 | 草逼吧 | 亚洲aⅴ天堂 | 白丝校花被扒开双腿喷水小说 | 男人女人日皮视频 | 丰腴尤物贵妇浪荡小说 | 国产亚洲欧美成人久久片 | 校花的第一次好紧好爽 | 精选国产AV精选一区二区三区 | 日本视频在线播放 | 国精视频一区二区视频 | 女子监狱第二季未删减在线看 | 无人区免费一二三四乱码 | 91国内精品久久久久影院优播 | 91国语精品自产拍在线观看一 | 亚洲精品视频导航 | 亚洲啊v | 夫妻性生活在线 | 国产一区二区在线免费观看 | 亚洲国产视频网站 | www.毛片在线观看 | 国产精品久久久久久 | 亚洲欧洲网站 | 毛片一级毛片 | 亚洲欧美韩国日产综合在线 | 欧美va免费大片 | 四虎影视在线永久免费观看 | 欧美日韩亚洲高清不卡一区二区三区 | 亚洲爱视频 | 色综合伊人色综合网站中国 | 免费人成黄页在线观看69 | 国产成人亚洲精品91专区高清 | 欧美贵妇videos办公室360 | 饭冈加奈子黑人解禁在线播放 | 日本美女视频韩国视频网站免费 | 1024在线视频精品免费 | 牛牛色婷婷在线视频播放 | 亚洲乱码一二三四区国产 |