DZ.NET论坛的图片验证码(原)
作了些改动,呵呵,以前他的那个数字是要根据在线用户来计算的,我改成随机的了,现在把源码贴出来
getcode.aspx代码如下
VerifyImage.cs代码如下
getcode.aspx代码如下
1using System;
2using System.Data;
3using System.Configuration;
4using System.Collections;
5using System.Web;
6using System.Web.Security;
7using System.Web.UI;
8using System.Web.UI.WebControls;
9using System.Web.UI.WebControls.WebParts;
10using System.Web.UI.HtmlControls;
11using System.IO;
12using System.Drawing;
13using System.Drawing.Imaging;
14using System.Text;
15public partial class GetCode : System.Web.UI.Page
16{
17 protected void Page_Load(object sender, EventArgs e)
18 {
19 Response.Expires = 0;
20 Response.CacheControl = "no-cache";
21 int number;
22 char code;
23 string checkCode = String.Empty;
24 System.Random random = new Random();
25 for (int i = 0; i < 5; i++)
26 {
27 number = random.Next();
28 code = (char)('0' + (char)(number % 10));
29 checkCode += code.ToString();
30 }
31 Session["getcode"] = checkCode;
32 VerifyImage vi = new VerifyImage(checkCode, 90, 40);
33 Bitmap image = vi.Image;
34 System.Web.HttpContext.Current.Response.ContentType = "image/pjpeg";
35 image.Save(this.Response.OutputStream, ImageFormat.Jpeg);
36 }
37}
38
2using System.Data;
3using System.Configuration;
4using System.Collections;
5using System.Web;
6using System.Web.Security;
7using System.Web.UI;
8using System.Web.UI.WebControls;
9using System.Web.UI.WebControls.WebParts;
10using System.Web.UI.HtmlControls;
11using System.IO;
12using System.Drawing;
13using System.Drawing.Imaging;
14using System.Text;
15public partial class GetCode : System.Web.UI.Page
16{
17 protected void Page_Load(object sender, EventArgs e)
18 {
19 Response.Expires = 0;
20 Response.CacheControl = "no-cache";
21 int number;
22 char code;
23 string checkCode = String.Empty;
24 System.Random random = new Random();
25 for (int i = 0; i < 5; i++)
26 {
27 number = random.Next();
28 code = (char)('0' + (char)(number % 10));
29 checkCode += code.ToString();
30 }
31 Session["getcode"] = checkCode;
32 VerifyImage vi = new VerifyImage(checkCode, 90, 40);
33 Bitmap image = vi.Image;
34 System.Web.HttpContext.Current.Response.ContentType = "image/pjpeg";
35 image.Save(this.Response.OutputStream, ImageFormat.Jpeg);
36 }
37}
38
VerifyImage.cs代码如下
1using System;
2using System.Data;
3using System.Configuration;
4using System.Web;
5using System.Web.Security;
6using System.Web.UI;
7using System.Web.UI.WebControls;
8using System.Web.UI.WebControls.WebParts;
9using System.Web.UI.HtmlControls;
10using System.IO;
11using System.Drawing;
12using System.Drawing.Drawing2D;
13using System.Drawing.Imaging;
14using System.Drawing.Text;
15using System.Security.Cryptography;
16
17/// <summary>
18/// 验证码图片类
19/// </summary>
20public class VerifyImage
21{
22 /// <summary>
23 /// 要显示的文字
24 /// </summary>
25 public string Text
26 {
27 get { return this.text; }
28 }
29 /// <summary>
30 /// 图片
31 /// </summary>
32 public Bitmap Image
33 {
34 get { return this.image; }
35 }
36 /// <summary>
37 /// 宽度
38 /// </summary>
39 public int Width
40 {
41 get { return this.width; }
42 }
43 /// <summary>
44 /// 高度
45 /// </summary>
46 public int Height
47 {
48 get { return this.height; }
49 }
50
51 private string text;
52 private int width;
53 private int height;
54 private Bitmap image;
55
56 private static byte[] randb = new byte[4];
57 private static RNGCryptoServiceProvider rand = new RNGCryptoServiceProvider();
58
59 /// <summary>
60 /// 构造函数
61 /// </summary>
62 /// <param name="code">要显示的验证码</param>
63 /// <param name="width">宽度</param>
64 /// <param name="height">高度</param>
65 public VerifyImage(string code, int width, int height)
66 {
67 this.text = code;
68 this.width = width;
69 this.height = height;
70 this.GenerateImage();
71
72 }
73
74 ~VerifyImage()
75 {
76 Dispose(false);
77 }
78
79 public void Dispose()
80 {
81 GC.SuppressFinalize(this);
82 this.Dispose(true);
83 }
84
85 protected virtual void Dispose(bool disposing)
86 {
87 if (disposing)
88 this.image.Dispose();
89 }
90 private FontFamily[] fonts = {
91 new FontFamily("Times New Roman"),
92 new FontFamily("Georgia"),
93 new FontFamily("Arial"),
94 new FontFamily("Comic Sans MS")
95 };
96
97 /// <summary>
98 /// 获得下一个随机数
99 /// </summary>
100 /// <returns></returns>
101 public static int Next()
102 {
103 rand.GetBytes(randb);
104 int value = BitConverter.ToInt32(randb, 0);
105 if (value < 0) value = -value;
106 return value;
107 }
108
109 /// <summary>
110 /// 获得下一个随机数
111 /// </summary>
112 /// <param name="max">最大值</param>
113 /// <returns></returns>
114 public static int Next(int max)
115 {
116 rand.GetBytes(randb);
117 int value = BitConverter.ToInt32(randb, 0);
118 value = value % (max + 1);
119 if (value < 0) value = -value;
120 return value;
121 }
122
123 /// <summary>
124 /// 获得下一个随机数
125 /// </summary>
126 /// <param name="min">最小值</param>
127 /// <param name="max">最大值</param>
128 /// <returns></returns>
129 public static int Next(int min, int max)
130 {
131 int value = Next(max - min) + min;
132 return value;
133 }
134
135
136 /// <summary>
137 /// 生成验证码图片
138 /// </summary>
139 private void GenerateImage()
140 {
141 Bitmap bitmap = new Bitmap(this.width, this.height, PixelFormat.Format32bppArgb);
142
143 Graphics g = Graphics.FromImage(bitmap);
144 Rectangle rect = new Rectangle(0, 0, this.width, this.height);
145 g.SmoothingMode = SmoothingMode.AntiAlias;
146
147 g.Clear(Color.White);
148
149 int emSize = Next(3) + 15;//(int)((this.width - 20) * 2 / text.Length);
150 FontFamily family = fonts[Next(fonts.Length - 1)];
151 Font font = new Font(family, emSize, FontStyle.Bold);
152
153 SizeF measured = new SizeF(0, 0);
154 SizeF workingSize = new SizeF(this.width, this.height);
155 while (emSize > 2 && (measured = g.MeasureString(text, font)).Width > workingSize.Width || measured.Height > workingSize.Height)
156 {
157 font.Dispose();
158 font = new Font(family, emSize -= 2);
159 }
160
161 SolidBrush drawBrush = new SolidBrush(Color.FromArgb(Next(100), Next(100), Next(100)));
162 for (int x = 0; x < 3; x++)
163 {
164 Pen linePen = new Pen(Color.FromArgb(Next(150), Next(150), Next(150)), 1);
165 g.DrawLine(linePen, new PointF(0.0F + Next(20), 0.0F + Next(this.height)), new PointF(0.0F + Next(this.width), 0.0F + Next(this.height)));
166 }
167
168 for (int x = 0; x < this.text.Length; x++)
169 {
170 drawBrush.Color = Color.FromArgb(Next(150) + 20, Next(150) + 20, Next(150) + 20);
171 PointF drawPoint = new PointF(0.0F + Next(4) + x * 15, 8.0F + Next(4));
172 g.DrawString(this.text[x].ToString(), font, drawBrush, drawPoint);
173 }
174
175 double distort = Next(5, 10) * (Next(10) == 1 ? 1 : -1);
176
177 using (Bitmap copy = (Bitmap)bitmap.Clone())
178 {
179 for (int y = 0; y < height; y++)
180 {
181 for (int x = 0; x < width; x++)
182 {
183 int newX = (int)(x + (distort * Math.Sin(Math.PI * y / 84.0)));
184 int newY = (int)(y + (distort * Math.Cos(Math.PI * x / 54.0)));
185 if (newX < 0 || newX >= width) newX = 0;
186 if (newY < 0 || newY >= height) newY = 0;
187 bitmap.SetPixel(x, y, copy.GetPixel(newX, newY));
188 }
189 }
190 }
191
192
193 //g.DrawRectangle(new Pen(Color.Silver), 0, 0, bitmap.Width - 1, bitmap.Height - 1);
194
195 font.Dispose();
196 drawBrush.Dispose();
197 g.Dispose();
198
199 this.image = bitmap;
200 }
201}
202
2using System.Data;
3using System.Configuration;
4using System.Web;
5using System.Web.Security;
6using System.Web.UI;
7using System.Web.UI.WebControls;
8using System.Web.UI.WebControls.WebParts;
9using System.Web.UI.HtmlControls;
10using System.IO;
11using System.Drawing;
12using System.Drawing.Drawing2D;
13using System.Drawing.Imaging;
14using System.Drawing.Text;
15using System.Security.Cryptography;
16
17/// <summary>
18/// 验证码图片类
19/// </summary>
20public class VerifyImage
21{
22 /// <summary>
23 /// 要显示的文字
24 /// </summary>
25 public string Text
26 {
27 get { return this.text; }
28 }
29 /// <summary>
30 /// 图片
31 /// </summary>
32 public Bitmap Image
33 {
34 get { return this.image; }
35 }
36 /// <summary>
37 /// 宽度
38 /// </summary>
39 public int Width
40 {
41 get { return this.width; }
42 }
43 /// <summary>
44 /// 高度
45 /// </summary>
46 public int Height
47 {
48 get { return this.height; }
49 }
50
51 private string text;
52 private int width;
53 private int height;
54 private Bitmap image;
55
56 private static byte[] randb = new byte[4];
57 private static RNGCryptoServiceProvider rand = new RNGCryptoServiceProvider();
58
59 /// <summary>
60 /// 构造函数
61 /// </summary>
62 /// <param name="code">要显示的验证码</param>
63 /// <param name="width">宽度</param>
64 /// <param name="height">高度</param>
65 public VerifyImage(string code, int width, int height)
66 {
67 this.text = code;
68 this.width = width;
69 this.height = height;
70 this.GenerateImage();
71
72 }
73
74 ~VerifyImage()
75 {
76 Dispose(false);
77 }
78
79 public void Dispose()
80 {
81 GC.SuppressFinalize(this);
82 this.Dispose(true);
83 }
84
85 protected virtual void Dispose(bool disposing)
86 {
87 if (disposing)
88 this.image.Dispose();
89 }
90 private FontFamily[] fonts = {
91 new FontFamily("Times New Roman"),
92 new FontFamily("Georgia"),
93 new FontFamily("Arial"),
94 new FontFamily("Comic Sans MS")
95 };
96
97 /// <summary>
98 /// 获得下一个随机数
99 /// </summary>
100 /// <returns></returns>
101 public static int Next()
102 {
103 rand.GetBytes(randb);
104 int value = BitConverter.ToInt32(randb, 0);
105 if (value < 0) value = -value;
106 return value;
107 }
108
109 /// <summary>
110 /// 获得下一个随机数
111 /// </summary>
112 /// <param name="max">最大值</param>
113 /// <returns></returns>
114 public static int Next(int max)
115 {
116 rand.GetBytes(randb);
117 int value = BitConverter.ToInt32(randb, 0);
118 value = value % (max + 1);
119 if (value < 0) value = -value;
120 return value;
121 }
122
123 /// <summary>
124 /// 获得下一个随机数
125 /// </summary>
126 /// <param name="min">最小值</param>
127 /// <param name="max">最大值</param>
128 /// <returns></returns>
129 public static int Next(int min, int max)
130 {
131 int value = Next(max - min) + min;
132 return value;
133 }
134
135
136 /// <summary>
137 /// 生成验证码图片
138 /// </summary>
139 private void GenerateImage()
140 {
141 Bitmap bitmap = new Bitmap(this.width, this.height, PixelFormat.Format32bppArgb);
142
143 Graphics g = Graphics.FromImage(bitmap);
144 Rectangle rect = new Rectangle(0, 0, this.width, this.height);
145 g.SmoothingMode = SmoothingMode.AntiAlias;
146
147 g.Clear(Color.White);
148
149 int emSize = Next(3) + 15;//(int)((this.width - 20) * 2 / text.Length);
150 FontFamily family = fonts[Next(fonts.Length - 1)];
151 Font font = new Font(family, emSize, FontStyle.Bold);
152
153 SizeF measured = new SizeF(0, 0);
154 SizeF workingSize = new SizeF(this.width, this.height);
155 while (emSize > 2 && (measured = g.MeasureString(text, font)).Width > workingSize.Width || measured.Height > workingSize.Height)
156 {
157 font.Dispose();
158 font = new Font(family, emSize -= 2);
159 }
160
161 SolidBrush drawBrush = new SolidBrush(Color.FromArgb(Next(100), Next(100), Next(100)));
162 for (int x = 0; x < 3; x++)
163 {
164 Pen linePen = new Pen(Color.FromArgb(Next(150), Next(150), Next(150)), 1);
165 g.DrawLine(linePen, new PointF(0.0F + Next(20), 0.0F + Next(this.height)), new PointF(0.0F + Next(this.width), 0.0F + Next(this.height)));
166 }
167
168 for (int x = 0; x < this.text.Length; x++)
169 {
170 drawBrush.Color = Color.FromArgb(Next(150) + 20, Next(150) + 20, Next(150) + 20);
171 PointF drawPoint = new PointF(0.0F + Next(4) + x * 15, 8.0F + Next(4));
172 g.DrawString(this.text[x].ToString(), font, drawBrush, drawPoint);
173 }
174
175 double distort = Next(5, 10) * (Next(10) == 1 ? 1 : -1);
176
177 using (Bitmap copy = (Bitmap)bitmap.Clone())
178 {
179 for (int y = 0; y < height; y++)
180 {
181 for (int x = 0; x < width; x++)
182 {
183 int newX = (int)(x + (distort * Math.Sin(Math.PI * y / 84.0)));
184 int newY = (int)(y + (distort * Math.Cos(Math.PI * x / 54.0)));
185 if (newX < 0 || newX >= width) newX = 0;
186 if (newY < 0 || newY >= height) newY = 0;
187 bitmap.SetPixel(x, y, copy.GetPixel(newX, newY));
188 }
189 }
190 }
191
192
193 //g.DrawRectangle(new Pen(Color.Silver), 0, 0, bitmap.Width - 1, bitmap.Height - 1);
194
195 font.Dispose();
196 drawBrush.Dispose();
197 g.Dispose();
198
199 this.image = bitmap;
200 }
201}
202