ASP.NET验证码

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
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Drawing;
using System.Text;
 
/// <summary>
/// VryImgGen 的摘要说明
/// </summary>
public class VryImgGen
{
    public static string ChineseChars = String.Empty;
 
    /// <summary>
    /// 英文与数字串
    /// </summary>
    protected static readonly string EnglishOrNumChars = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
 
    public VryImgGen()
    {
        rnd = new Random(unchecked((int)DateTime.Now.Ticks));
    }
 
    /// <summary>
    /// 全局随机数生成器
    /// </summary>
    private Random rnd;
 
    int length = 5;
    /// <summary>
    /// 验证码长度(默认6个验证码的长度)
    /// </summary>
    public int Length
    {
        get { return length; }
        set { length = value; }
    }
 
    int fontSize = 18;
    /// <summary>
    /// 验证码字体大小(为了显示扭曲效果,默认30像素,可以自行修改)
    /// </summary>
    public int FontSize
    {
        get { return fontSize; }
        set { fontSize = value; }
    }
 
    int padding = 4;
    /// <summary>
    /// 边框补(默认4像素)
    /// </summary>
    public int Padding
    {
        get { return padding; }
        set { padding = value; }
    }
 
    bool chaos = true;
    /// <summary>
    /// 是否输出燥点(默认输出)
    /// </summary>
    public bool Chaos
    {
        get { return chaos; }
        set { chaos = value; }
    }
 
    Color chaosColor = Color.LightGray;
    /// <summary>
    /// 输出燥点的颜色(默认灰色)
    /// </summary>
    public Color ChaosColor
    {
        get { return chaosColor; }
        set { chaosColor = value; }
    }
 
    Color backgroundColor = Color.White;
    /// <summary>
    /// 自定义背景色(默认白色)
    /// </summary>
    public Color BackgroundColor
    {
        get { return backgroundColor; }
        set { backgroundColor = value; }
    }
 
    Color[] colors = { Color.Black, Color.Red, Color.DarkBlue, Color.Green, Color.Orange, Color.Brown, Color.DarkCyan, Color.Purple };
    /// <summary>
    /// 自定义随机颜色数组
    /// </summary>
    public Color[] Colors
    {
        get { return colors; }
        set { colors = value; }
    }
 
    string[] fonts = { "Arial", "Georgia" };
    /// <summary>
    /// 自定义字体数组
    /// </summary>
    public string[] Fonts
    {
        get { return fonts; }
        set { fonts = value; }
    }
 
    #region 产生波形滤镜效果
 
    private const double PI = 3.1415926535897932384626433832795;
    private const double PI2 = 6.283185307179586476925286766559;
 
    /// <summary>
    /// 正弦曲线Wave扭曲图片(Edit By 51aspx.com)
    /// </summary>
    /// <param name="srcBmp">图片路径</param>
    /// <param name="bXDir">如果扭曲则选择为True</param>
    /// <param name="nMultValue">波形的幅度倍数,越大扭曲的程度越高,一般为3</param>
    /// <param name="dPhase">波形的起始相位,取值区间[0-2*PI)</param>
    /// <returns></returns>
    public System.Drawing.Bitmap TwistImage(Bitmap srcBmp, bool bXDir, double dMultValue, double dPhase)
    {
        System.Drawing.Bitmap destBmp = new Bitmap(srcBmp.Width, srcBmp.Height);
 
        // 将位图背景填充为白色
        System.Drawing.Graphics graph = System.Drawing.Graphics.FromImage(destBmp);
        graph.FillRectangle(new SolidBrush(System.Drawing.Color.White), 0, 0, destBmp.Width, destBmp.Height);
        graph.Dispose();
 
        double dBaseAxisLen = bXDir ? (double)destBmp.Height : (double)destBmp.Width;
 
        for (int i = 0; i < destBmp.Width; i++)
        {
            for (int j = 0; j < destBmp.Height; j++)
            {
                double dx = 0;
                dx = bXDir ? (PI2 * (double)j) / dBaseAxisLen : (PI2 * (double)i) / dBaseAxisLen;
                dx += dPhase;
                double dy = Math.Sin(dx);
 
                // 取得当前点的颜色
                int nOldX = 0, nOldY = 0;
                nOldX = bXDir ? i + (int)(dy * dMultValue) : i;
                nOldY = bXDir ? j : j + (int)(dy * dMultValue);
 
                System.Drawing.Color color = srcBmp.GetPixel(i, j);
                if (nOldX >= 0 && nOldX < destBmp.Width
                 && nOldY >= 0 && nOldY < destBmp.Height)
                {
                    destBmp.SetPixel(nOldX, nOldY, color);
                }
            }
        }
 
        return destBmp;
    }
 
 
 
    #endregion
 
    /// <summary>
    /// 生成校验码图片
    /// </summary>
    /// <param name="code">验证码</param>
    /// <returns></returns>
    public Bitmap CreateImage(string code)
    {
        int fSize = FontSize;
        int fWidth = fSize + Padding;
 
        int imageWidth = (int)(code.Length * fWidth) + 4 + Padding * 2;
        int imageHeight = fSize<pre class="brush: csharp; auto-links: true; collapse: false; first-line: 1; gutter: html-script: light: ruler: smart-tabs: tab-size: 4; toolbar:">using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Drawing;
using System.Text;
 
/// <summary>
/// VryImgGen 的摘要说明
/// </summary>
public class VryImgGen
{
    public static string ChineseChars = String.Empty;
 
    /// <summary>
    /// 英文与数字串
    /// </summary>
    protected static readonly string EnglishOrNumChars = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
 
    public VryImgGen()
    {
        rnd = new Random(unchecked((int)DateTime.Now.Ticks));
    }
 
    /// <summary>
    /// 全局随机数生成器
    /// </summary>
    private Random rnd;
 
    int length = 5;
    /// <summary>
    /// 验证码长度(默认6个验证码的长度)
    /// </summary>
    public int Length
    {
        get { return length; }
        set { length = value; }
    }
 
    int fontSize = 18;
    /// <summary>
    /// 验证码字体大小(为了显示扭曲效果,默认30像素,可以自行修改)
    /// </summary>
    public int FontSize
    {
        get { return fontSize; }
        set { fontSize = value; }
    }
 
    int padding = 4;
    /// <summary>
    /// 边框补(默认4像素)
    /// </summary>
    public int Padding
    {
        get { return padding; }
        set { padding = value; }
    }
 
    bool chaos = true;
    /// <summary>
    /// 是否输出燥点(默认输出)
    /// </summary>
    public bool Chaos
    {
        get { return chaos; }
        set { chaos = value; }
    }
 
    Color chaosColor = Color.LightGray;
    /// <summary>
    /// 输出燥点的颜色(默认灰色)
    /// </summary>
    public Color ChaosColor
    {
        get { return chaosColor; }
        set { chaosColor = value; }
    }
 
    Color backgroundColor = Color.White;
    /// <summary>
    /// 自定义背景色(默认白色)
    /// </summary>
    public Color BackgroundColor
    {
        get { return backgroundColor; }
        set { backgroundColor = value; }
    }
 
    Color[] colors = { Color.Black, Color.Red, Color.DarkBlue, Color.Green, Color.Orange, Color.Brown, Color.DarkCyan, Color.Purple };
    /// <summary>
    /// 自定义随机颜色数组
    /// </summary>
    public Color[] Colors
    {
        get { return colors; }
        set { colors = value; }
    }
 
    string[] fonts = { "Arial", "Georgia" };
    /// <summary>
    /// 自定义字体数组
    /// </summary>
    public string[] Fonts
    {
        get { return fonts; }
        set { fonts = value; }
    }
 
    #region 产生波形滤镜效果
 
    private const double PI = 3.1415926535897932384626433832795;
    private const double PI2 = 6.283185307179586476925286766559;
 
    /// <summary>
    /// 正弦曲线Wave扭曲图片(Edit By 51aspx.com)
    /// </summary>
    /// <param name="srcBmp">图片路径</param>
    /// <param name="bXDir">如果扭曲则选择为True</param>
    /// <param name="nMultValue">波形的幅度倍数,越大扭曲的程度越高,一般为3</param>
    /// <param name="dPhase">波形的起始相位,取值区间[0-2*PI)</param>
    /// <returns></returns>
    public System.Drawing.Bitmap TwistImage(Bitmap srcBmp, bool bXDir, double dMultValue, double dPhase)
    {
        System.Drawing.Bitmap destBmp = new Bitmap(srcBmp.Width, srcBmp.Height);
 
        // 将位图背景填充为白色
        System.Drawing.Graphics graph = System.Drawing.Graphics.FromImage(destBmp);
        graph.FillRectangle(new SolidBrush(System.Drawing.Color.White), 0, 0, destBmp.Width, destBmp.Height);
        graph.Dispose();
 
        double dBaseAxisLen = bXDir ? (double)destBmp.Height : (double)destBmp.Width;
 
        for (int i = 0; i < destBmp.Width; i++)
        {
            for (int j = 0; j < destBmp.Height; j++)
            {
                double dx = 0;
                dx = bXDir ? (PI2 * (double)j) / dBaseAxisLen : (PI2 * (double)i) / dBaseAxisLen;
                dx += dPhase;
                double dy = Math.Sin(dx);
 
                // 取得当前点的颜色
                int nOldX = 0, nOldY = 0;
                nOldX = bXDir ? i + (int)(dy * dMultValue) : i;
                nOldY = bXDir ? j : j + (int)(dy * dMultValue);
 
                System.Drawing.Color color = srcBmp.GetPixel(i, j);
                if (nOldX >= 0 && nOldX < destBmp.Width
                 && nOldY >= 0 && nOldY < destBmp.Height)
                {
                    destBmp.SetPixel(nOldX, nOldY, color);
                }
            }
        }
 
        return destBmp;
    }
 
 
 
    #endregion
 
    /// <summary>
    /// 生成校验码图片
    /// </summary>
    /// <param name="code">验证码</param>
    /// <returns></returns>
    public Bitmap CreateImage(string code)
    {
        int fSize = FontSize;
        int fWidth = fSize + Padding;
 
        int imageWidth = (int)(code.Length * fWidth) + 4 + Padding * 2;
        int imageHeight = fSize * 2 + Padding * 2;
 
        System.Drawing.Bitmap image = new System.Drawing.Bitmap(imageWidth, imageHeight);
 
        Graphics g = Graphics.FromImage(image);
 
        g.Clear(BackgroundColor);
 
        //给背景添加随机生成的燥点
        if (this.Chaos)
        {
 
            Pen pen = new Pen(ChaosColor, 0);
            int c = Length * 10;
 
            for (int i = 0; i < c; i++)
            {
                int x = rnd.Next(image.Width);
                int y = rnd.Next(image.Height);
 
                g.DrawRectangle(pen, x, y, 1, 1);
            }
        }
 
        int left = 0, top = 0, top1 = 1, top2 = 1;
 
        int n1 = (imageHeight - FontSize - Padding * 2);
        int n2 = n1 / 4;
        top1 = n2;
        top2 = n2 * 2;
 
        Font f;
        Brush b;
 
        int cindex, findex;
 
        //随机字体和颜色的验证码字符
        for (int i = 0; i < code.Length; i++)
        {
            cindex = rnd.Next(Colors.Length - 1);
            findex = rnd.Next(Fonts.Length - 1);
 
            f = new System.Drawing.Font(Fonts[findex], fSize, System.Drawing.FontStyle.Bold);
            b = new System.Drawing.SolidBrush(Colors[cindex]);
 
            if (i % 2 == 1)
            {
                top = top2;
            }
            else
            {
                top = top1;
            }
 
            left = i * fWidth;
 
            g.DrawString(code.Substring(i, 1), f, b, left, top);
        }
 
        //画一个边框 边框颜色为Color.Gainsboro
        g.DrawRectangle(new Pen(Color.Gainsboro, 0), 0, 0, image.Width - 1, image.Height - 1);
        g.Dispose();
 
        //产生波形(Add By 51aspx.com)
        image = TwistImage(image, false, 0, 4);
 
        return image;
    }
 
    /// <summary>
    /// 生成随机字符码
    /// </summary>
    /// <param name="codeLen">字符串长度</param>
    /// <param name="zhCharsCount">中文字符数</param>
    /// <returns></returns>
    public string CreateVerifyCode(int codeLen, int zhCharsCount)
    {
        char[] chs = new char[codeLen];
 
        int index;
        for (int i = 0; i < zhCharsCount; i++)
        {
            index = rnd.Next(0, codeLen);
            if (chs[index] == '\0')
                chs[index] = CreateZhChar();
            else
                --i;
        }
        for (int i = 0; i < codeLen; i++)
        {
            if (chs[i] == '\0')
                chs[i] = CreateEnOrNumChar();
        }
 
        return new string(chs, 0, chs.Length);
    }
 
    /// <summary>
    /// 生成默认长度5的随机字符码
    /// </summary>
    /// <returns></returns>
    public string CreateVerifyCode()
    {
        return CreateVerifyCode(Length, 0);
    }
 
    /// <summary>
    /// 生成英文或数字字符
    /// </summary>
    /// <returns></returns>
    protected char CreateEnOrNumChar()
    {
        return EnglishOrNumChars[rnd.Next(0, EnglishOrNumChars.Length)];
    }
 
    /// <summary>
    /// 生成汉字字符
    /// </summary>
    /// <returns></returns>
    protected char CreateZhChar()
    {
        //若提供了汉字集,查询汉字集选取汉字
        if (ChineseChars.Length > 0)
        {
            return ChineseChars[rnd.Next(0, ChineseChars.Length)];
        }
        //若没有提供汉字集,则根据《GB2312简体中文编码表》编码规则构造汉字
        else
        {
            byte[] bytes = new byte[2];
 
            //第一个字节值在0xb0, 0xf7之间
            bytes[0] = (byte)rnd.Next(0xb0, 0xf8);
            //第二个字节值在0xa1, 0xfe之间
            bytes[1] = (byte)rnd.Next(0xa1, 0xff);
 
            //根据汉字编码的字节数组解码出中文汉字
            string str1 = Encoding.GetEncoding("gb2312").GetString(bytes);
 
            return str1[0];
        }
    }
 
}
</pre>
* 2 + Padding * 2;
        System.Drawing.Bitmap image = new System.Drawing.Bitmap(imageWidth, imageHeight);
        Graphics g = Graphics.FromImage(image);
        g.Clear(BackgroundColor);
        //给背景添加随机生成的燥点
        if (this.Chaos)
        {
            Pen pen = new Pen(ChaosColor, 0);
            int c = Length * 10;
            for (int i = 0; i < c; i++)
            {
                int x = rnd.Next(image.Width);
                int y = rnd.Next(image.Height);
                g.DrawRectangle(pen, x, y, 1, 1);
            }
        }
        int left = 0, top = 0, top1 = 1, top2 = 1;
        int n1 = (imageHeight - FontSize - Padding * 2);
        int n2 = n1 / 4;
        top1 = n2;
        top2 = n2 * 2;
        Font f;
        Brush b;
        int cindex, findex;
        //随机字体和颜色的验证码字符
        for (int i = 0; i < code.Length; i++)
        {
            cindex = rnd.Next(Colors.Length - 1);
            findex = rnd.Next(Fonts.Length - 1);
            f = new System.Drawing.Font(Fonts[findex], fSize, System.Drawing.FontStyle.Bold);
            b = new System.Drawing.SolidBrush(Colors[cindex]);
            if (i % 2 == 1)
            {
                top = top2;
            }
            else
            {
                top = top1;
            }
            left = i * fWidth;
            g.DrawString(code.Substring(i, 1), f, b, left, top);
        }
        //画一个边框 边框颜色为Color.Gainsboro
        g.DrawRectangle(new Pen(Color.Gainsboro, 0), 0, 0, image.Width - 1, image.Height - 1);
        g.Dispose();
        //产生波形(Add By 51aspx.com)
        image = TwistImage(image, false, 0, 4);
        return image;
    }
    /// <summary>
    /// 生成随机字符码
    /// </summary>
    /// <param name="codeLen">字符串长度</param>
    /// <param name="zhCharsCount">中文字符数</param>
    /// <returns></returns>
    public string CreateVerifyCode(int codeLen, int zhCharsCount)
    {
        char[] chs = new char[codeLen];
        int index;
        for (int i = 0; i < zhCharsCount; i++)
        {
            index = rnd.Next(0, codeLen);
            if (chs[index] == '\0')
                chs[index] = CreateZhChar();
            else
                --i;
        }
        for (int i = 0; i < codeLen; i++)
        {
            if (chs[i] == '\0')
                chs[i] = CreateEnOrNumChar();
        }
        return new string(chs, 0, chs.Length);
    }
    /// <summary>
    /// 生成默认长度5的随机字符码
    /// </summary>
    /// <returns></returns>
    public string CreateVerifyCode()
    {
        return CreateVerifyCode(Length, 0);
    }
    /// <summary>
    /// 生成英文或数字字符
    /// </summary>
    /// <returns></returns>
    protected char CreateEnOrNumChar()
    {
        return EnglishOrNumChars[rnd.Next(0, EnglishOrNumChars.Length)];
    }
    /// <summary>
    /// 生成汉字字符
    /// </summary>
    /// <returns></returns>
    protected char CreateZhChar()
    {
        //若提供了汉字集,查询汉字集选取汉字
        if (ChineseChars.Length > 0)
        {
            return ChineseChars[rnd.Next(0, ChineseChars.Length)];
        }
        //若没有提供汉字集,则根据《GB2312简体中文编码表》编码规则构造汉字
        else
        {
            byte[] bytes = new byte[2];
            //第一个字节值在0xb0, 0xf7之间
            bytes[0] = (byte)rnd.Next(0xb0, 0xf8);
            //第二个字节值在0xa1, 0xfe之间
            bytes[1] = (byte)rnd.Next(0xa1, 0xff);
            //根据汉字编码的字节数组解码出中文汉字
            string str1 = Encoding.GetEncoding("gb2312").GetString(bytes);
            return str1[0];
        }
    }
}
Technorati 标签: ASP.NET验证码
posted @   Bruce-He  阅读(328)  评论(0编辑  收藏  举报
编辑推荐:
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
阅读排行:
· DeepSeek 开源周回顾「GitHub 热点速览」
· 记一次.NET内存居高不下排查解决与启示
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· .NET10 - 预览版1新功能体验(一)
成都共享办公室
点击右上角即可分享
微信分享提示