首先看下效果实现(由于gif屏幕录制软件是即时找的,有些失祯)
代码主要就是绘制验证码类的实现
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
|
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Drawing; using System.IO; namespace SecurityCodePic { public class DrawingSecurityCode { /// <summary> /// 生成验证码,并返回 /// </summary> /// <returns></returns> public string GetSecurityCode( int n) { string code = GenerateCheckCode(n); CreateCheckCodeImage(code); return code; } /// <summary> /// 动态生成指定数目的随机数或字母 /// </summary> /// <param name="num">整数</param> /// <returns>返回验证码字符串</returns> private string GenerateCheckCode( int num) { int number; //定义变量 char code; string checkCode = String.Empty; //空字符串,只读 Random random = new Random(); //定义随机变量实例 for ( int i=0; i < num;i++ ) { //利用for循环生成指定数目的随机数或字母 number = random.Next(); //返回一个小于指定的最大值的非负的随机数 next有三个构造函数 if (number % 2 == 0) { //产生一个一位数 code = ( char )( '0' + ( char )(number % 10)); } else { //产生一个大写字母 code = ( char )( 'A' +( char )(number % 26)); } checkCode += code.ToString(); } return checkCode; } /// <summary> /// 根据验证码字符串生成验证码图片 /// </summary> /// <param name="checkCode">验证码字符串</param> private void CreateCheckCodeImage( string checkCode) { if (checkCode == null || checkCode.Trim() == String.Empty) return ; // 引用System.Drawing类库 Bitmap myImage = new Bitmap(80, 30); //生成一个指定大小的位图 Graphics graphics = Graphics.FromImage(myImage); //从一个位图生成一个画布 try { graphics.Clear(Color.White); //清除整个绘画面并以指定的背景色填充,这里是把背景色设为白色 Random random = new Random(); //实例化一个伪随机数生成器 //画图片的前景噪音点,这里有100个 for ( int i = 0; i < 100; i++) { int x = random.Next(myImage.Width); int y = random.Next(myImage.Height); myImage.SetPixel(x, y, Color.FromArgb(random.Next())); //指定坐标为x,y处的像素的颜色 } //画图片的背景噪音线,这里为2条 for ( int i = 0; i < 2; i++) { int x1 = random.Next(myImage.Width); int x2 = random.Next(myImage.Width); int y1 = random.Next(myImage.Height); int y2 = random.Next(myImage.Height); graphics.DrawLine( new Pen(Color.Black), x1, y1, x2, y2); //绘制一条坐标x1,y1到坐标x2,y2的指定颜色的线条,这里的线条为黑色 } Font font = new Font( "Arial" , 15, FontStyle.Bold); //定义特定的文本格式,这里的字体为Arial,大小为15,字体加粗 //根据矩形、起始颜色和结束颜色以及方向角度产生一个LinearGradientBrush实例---线性渐变 System.Drawing.Drawing2D.LinearGradientBrush brush = new System.Drawing.Drawing2D.LinearGradientBrush( new Rectangle(0, 0, myImage.Width, myImage.Height), //在坐标0,0处实例化一个和myImage同样大小的矩形 Color.Blue, Color.Red, 1.2f, true ); //绘制文本字符串 graphics.DrawString(checkCode, font, brush, 2, 2); //绘制有坐标对、宽度和高度指定的矩形---画图片的边框线 graphics.DrawRectangle( new Pen(Color.Silver), 0, 0, myImage.Width - 1, myImage.Height - 1); //创建其支持存储器为内存的流 MemoryStream ms = new MemoryStream(); //将此图像以指定格式保存到指定的流中 myImage.Save(ms, System.Drawing.Imaging.ImageFormat.Gif); //这里是以gif的格式保存到内存中 HttpContext.Current.Response.ClearContent(); //清除缓冲区流中的所有内容输出 HttpContext.Current.Response.ContentType = "image/Gif" ; //获取或设置输出流的HTTP MIME类型 HttpContext.Current.Response.BinaryWrite(ms.ToArray()); //将一个二进制字符串写入HTTP输出流 } finally { //释放占用资源 graphics.Dispose(); myImage.Dispose(); } } } } |
然后使用SecurityCode.ashx文件调用上面类的方法实现
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
|
using System; using System.Collections.Generic; using System.Linq; using System.Web; namespace SecurityCodePic { /// <summary> /// SecurityCode1 的摘要说明 /// </summary> public class SecurityCode1 : IHttpHandler { public void ProcessRequest(HttpContext context) { DrawingSecurityCode sc = new DrawingSecurityCode(); string SecurityCode = sc.GetSecurityCode(6); } public bool IsReusable { get { return false ; } } } } |
最后就是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
|
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="SecurityCode_Test.aspx.cs" Inherits="SecurityCodePic.SecurityCode_Test" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> < html xmlns = "http://www.w3.org/1999/xhtml" > < head runat = "server" > < title >验证码的实现</ title > < style type = "text/css" > #VCodeImg { cursor: pointer;} </ style > </ head > < body > < form id = "form1" runat = "server" > < div > < img id = "VCodeImg" src = "SecurityCode.ashx" alt = "验证码" onclick = "javascript:RefreshCode();" /> </ div > </ form > < script type = "text/javascript" > function RefreshCode() { var random = Math.random(); var img = document.getElementById("VCodeImg"); img.src = "SecurityCode.ashx?" + random; //加上无意义的随机参数,浏览器才会认为是新地址,就会重新读取数据 } </ script > </ body > </ html > |
以上就是本文的全部,对了,还有源码下载分享给大家,欢迎大家下载。
源码分享:ASP.NET验证码实现