以下是一个验证码类源代码,使用方法:建立一个ValidateImg.aspx,后台代码里写:ValidateImage img = new ValidateImage();验证的地方:ValidateImage.Validate(txtCode.Text.Trim())
1 /// <summary>
2 /// 验证码类
3 /// </summary>
4 public class ValidateImage
5 {
6 /// <summary>
7 /// 要显示的文字
8 /// </summary>
9 public string Text
10 {
11 get { return this.text; }
12 }
13 /// <summary>
14 /// 图片
15 /// </summary>
16 public Bitmap Image
17 {
18 get { return this.image; }
19 }
20 /// <summary>
21 /// 宽度
22 /// </summary>
23 public int Width
24 {
25 get { return this.width; }
26 }
27 /// <summary>
28 /// 高度
29 /// </summary>
30 public int Height
31 {
32 get { return this.height; }
33 }
34
35 private string text;
36 private int width;
37 private int height;
38 private Bitmap image;
39
40 private static byte[] randb = new byte[4];
41 private static Random rand = new Random();
42
43 public ValidateImage()
44 {
45 text = rand.Next(1000, 9999).ToString();
46 System.Web.HttpContext.Current.Session["CheckCode"] = text;
47 this.width = (int)Math.Ceiling(text.Length * 16.5);
48 this.height = 30;
49
50 GenerateImage();
51 System.Web.HttpContext.Current.Response.ContentType = "image/pjpeg";
52 Image.Save(System.Web.HttpContext.Current.Response.OutputStream, ImageFormat.Jpeg);
53
54 }
55
56 public static bool Validate(string input)
57 {
58 return System.Web.HttpContext.Current.Session["CheckCode"] != null && System.Web.HttpContext.Current.Session["CheckCode"].ToString().Equals(input);
59 }
60
61 ~ValidateImage()
62 {
63 Dispose(false);
64 }
65
66 public void Dispose()
67 {
68 GC.SuppressFinalize(this);
69 this.Dispose(true);
70 }
71
72 protected virtual void Dispose(bool disposing)
73 {
74 if (disposing)
75 this.image.Dispose();
76 }
77 private FontFamily[] fonts = {
78 new FontFamily("Times New Roman"),
79 new FontFamily("Georgia"),
80 new FontFamily("Arial"),
81 new FontFamily("Comic Sans MS")
82 };
83
84 public int Next(int max)
85 {
86 return rand.Next(max);
87 }
88
89 /// <summary>
90 /// 生成验证码图片
91
92 /// </summary>
93 private void GenerateImage()
94 {
95 Bitmap bitmap = new Bitmap(this.width, this.height, PixelFormat.Format32bppArgb);
96
97 Graphics g = Graphics.FromImage(bitmap);
98 Rectangle rect = new Rectangle(0, 0, this.width, this.height);
99 g.SmoothingMode = SmoothingMode.AntiAlias;
100
101 g.Clear(Color.White);
102
103 //int emSize = Next(3) + 15;//(int)((this.width - 20) * 2 / text.Length);
104 //int emSize = (int)((this.width - 20) * 2 / text.Length);
105
106 int emSize = 12;
107 FontFamily family = fonts[Next(fonts.Length - 1)];
108 Font font = new Font(family, emSize, FontStyle.Bold);
109
110 SizeF measured = new SizeF(0, 0);
111 SizeF workingSize = new SizeF(this.width, this.height);
112 while (emSize > 2 && (measured = g.MeasureString(text, font)).Width > workingSize.Width || measured.Height > workingSize.Height)
113 {
114 font.Dispose();
115 font = new Font(family, emSize -= 2);
116 }
117
118 SolidBrush drawBrush = new SolidBrush(Color.FromArgb(Next(100), Next(80), Next(80)));
119 for (int x = 0; x < 1; x++)
120 {
121 Pen linePen = new Pen(Color.FromArgb(Next(150), Next(100), Next(100)), 1);
122 g.DrawLine(linePen, new PointF(0.0F + Next(2), 0.0F + Next(this.height)), new PointF(0.0F + Next(this.width), 0.0F + Next(this.height - 10)));
123 }
124
125 for (int x = 0; x < this.text.Length; x++)
126 {
127 drawBrush.Color = Color.FromArgb(Next(150) + 20, Next(150) + 20, Next(150) + 20);
128 PointF drawPoint = new PointF(0.0F + Next(5) + x * 15, 2.0F + Next(4));
129 g.DrawString(this.text[x].ToString(), font, drawBrush, drawPoint);
130 }
131
132 double distort = rand.Next(5, 10) * (Next(10) == 1 ? 1 : -1);
133
134 using (Bitmap copy = (Bitmap)bitmap.Clone())
135 {
136 for (int y = 0; y < height; y++)
137 {
138 for (int x = 0; x < width; x++)
139 {
140 int newX = (int)(x + (distort * Math.Sin(Math.PI * y / 84.0)));
141 int newY = (int)(y + (distort * Math.Cos(Math.PI * x / 54.0)));
142 if (newX < 0 || newX >= width) newX = 0;
143 if (newY < 0 || newY >= height) newY = 0;
144 bitmap.SetPixel(x, y, copy.GetPixel(newX, newY));
145 }
146 }
147 }
148
149
150 //g.DrawRectangle(new Pen(Color.Silver), 0, 0, bitmap.Width - 1, bitmap.Height - 1);
151
152 font.Dispose();
153 drawBrush.Dispose();
154 g.Dispose();
155
156 this.image = bitmap;
157 }
158 }
2 /// 验证码类
3 /// </summary>
4 public class ValidateImage
5 {
6 /// <summary>
7 /// 要显示的文字
8 /// </summary>
9 public string Text
10 {
11 get { return this.text; }
12 }
13 /// <summary>
14 /// 图片
15 /// </summary>
16 public Bitmap Image
17 {
18 get { return this.image; }
19 }
20 /// <summary>
21 /// 宽度
22 /// </summary>
23 public int Width
24 {
25 get { return this.width; }
26 }
27 /// <summary>
28 /// 高度
29 /// </summary>
30 public int Height
31 {
32 get { return this.height; }
33 }
34
35 private string text;
36 private int width;
37 private int height;
38 private Bitmap image;
39
40 private static byte[] randb = new byte[4];
41 private static Random rand = new Random();
42
43 public ValidateImage()
44 {
45 text = rand.Next(1000, 9999).ToString();
46 System.Web.HttpContext.Current.Session["CheckCode"] = text;
47 this.width = (int)Math.Ceiling(text.Length * 16.5);
48 this.height = 30;
49
50 GenerateImage();
51 System.Web.HttpContext.Current.Response.ContentType = "image/pjpeg";
52 Image.Save(System.Web.HttpContext.Current.Response.OutputStream, ImageFormat.Jpeg);
53
54 }
55
56 public static bool Validate(string input)
57 {
58 return System.Web.HttpContext.Current.Session["CheckCode"] != null && System.Web.HttpContext.Current.Session["CheckCode"].ToString().Equals(input);
59 }
60
61 ~ValidateImage()
62 {
63 Dispose(false);
64 }
65
66 public void Dispose()
67 {
68 GC.SuppressFinalize(this);
69 this.Dispose(true);
70 }
71
72 protected virtual void Dispose(bool disposing)
73 {
74 if (disposing)
75 this.image.Dispose();
76 }
77 private FontFamily[] fonts = {
78 new FontFamily("Times New Roman"),
79 new FontFamily("Georgia"),
80 new FontFamily("Arial"),
81 new FontFamily("Comic Sans MS")
82 };
83
84 public int Next(int max)
85 {
86 return rand.Next(max);
87 }
88
89 /// <summary>
90 /// 生成验证码图片
91
92 /// </summary>
93 private void GenerateImage()
94 {
95 Bitmap bitmap = new Bitmap(this.width, this.height, PixelFormat.Format32bppArgb);
96
97 Graphics g = Graphics.FromImage(bitmap);
98 Rectangle rect = new Rectangle(0, 0, this.width, this.height);
99 g.SmoothingMode = SmoothingMode.AntiAlias;
100
101 g.Clear(Color.White);
102
103 //int emSize = Next(3) + 15;//(int)((this.width - 20) * 2 / text.Length);
104 //int emSize = (int)((this.width - 20) * 2 / text.Length);
105
106 int emSize = 12;
107 FontFamily family = fonts[Next(fonts.Length - 1)];
108 Font font = new Font(family, emSize, FontStyle.Bold);
109
110 SizeF measured = new SizeF(0, 0);
111 SizeF workingSize = new SizeF(this.width, this.height);
112 while (emSize > 2 && (measured = g.MeasureString(text, font)).Width > workingSize.Width || measured.Height > workingSize.Height)
113 {
114 font.Dispose();
115 font = new Font(family, emSize -= 2);
116 }
117
118 SolidBrush drawBrush = new SolidBrush(Color.FromArgb(Next(100), Next(80), Next(80)));
119 for (int x = 0; x < 1; x++)
120 {
121 Pen linePen = new Pen(Color.FromArgb(Next(150), Next(100), Next(100)), 1);
122 g.DrawLine(linePen, new PointF(0.0F + Next(2), 0.0F + Next(this.height)), new PointF(0.0F + Next(this.width), 0.0F + Next(this.height - 10)));
123 }
124
125 for (int x = 0; x < this.text.Length; x++)
126 {
127 drawBrush.Color = Color.FromArgb(Next(150) + 20, Next(150) + 20, Next(150) + 20);
128 PointF drawPoint = new PointF(0.0F + Next(5) + x * 15, 2.0F + Next(4));
129 g.DrawString(this.text[x].ToString(), font, drawBrush, drawPoint);
130 }
131
132 double distort = rand.Next(5, 10) * (Next(10) == 1 ? 1 : -1);
133
134 using (Bitmap copy = (Bitmap)bitmap.Clone())
135 {
136 for (int y = 0; y < height; y++)
137 {
138 for (int x = 0; x < width; x++)
139 {
140 int newX = (int)(x + (distort * Math.Sin(Math.PI * y / 84.0)));
141 int newY = (int)(y + (distort * Math.Cos(Math.PI * x / 54.0)));
142 if (newX < 0 || newX >= width) newX = 0;
143 if (newY < 0 || newY >= height) newY = 0;
144 bitmap.SetPixel(x, y, copy.GetPixel(newX, newY));
145 }
146 }
147 }
148
149
150 //g.DrawRectangle(new Pen(Color.Silver), 0, 0, bitmap.Width - 1, bitmap.Height - 1);
151
152 font.Dispose();
153 drawBrush.Dispose();
154 g.Dispose();
155
156 this.image = bitmap;
157 }
158 }