直接附上一个例子先,仅供学习参考。。
1 using System;
2 using System.Collections;
3 using System.ComponentModel;
4 using System.Data;
5 using System.Web;
6 using System.Web.SessionState;
7 using System.Web.UI;
8 using System.Web.UI.WebControls;
9 using System.Web.UI.HtmlControls;
10 using System.Drawing;
11 using System.Drawing.Imaging;
12 using System.Drawing.Text;
13 /**///// <summary>
14 /// 页面验证码程序
15 /// 使用:在页面中加入HTML代码 <img src="VerifyCode.aspx">
16 ///</summary>
17 public partial class VerifyCode : System.Web.UI.Page
18 {
19 static string[] FontItems = new string[]
20 {
21 "Arial",
22 "Helvetica",
23 "Geneva",
24 "sans-serif",
25 "Verdana"
26 };
27 static Brush[] BrushItems = new Brush[]
28 {
29 Brushes.OliveDrab,
30 Brushes.ForestGreen,
31 Brushes.DarkCyan,
32 Brushes.LightSlateGray,
33 Brushes.RoyalBlue,
34 Brushes.SlateBlue,
35 Brushes.DarkViolet,
36 Brushes.MediumVioletRed,
37 Brushes.IndianRed,
38 Brushes.Firebrick,
39 Brushes.Chocolate,
40 Brushes.Peru,
41 Brushes.Goldenrod
42 };
43 static string[] BrushName = new string[]
44 {
45 "OliveDrab",
46 "ForestGreen",
47 "DarkCyan",
48 "LightSlateGray",
49 "RoyalBlue",
50 "SlateBlue",
51 "DarkViolet",
52 "MediumVioletRed",
53 "IndianRed",
54 "Firebrick",
55 "Chocolate",
56 "Peru",
57 "Goldenrod"
58 };
59 private static Color BackColor = Color.White;
60 private static Pen BorderColor = Pens.DarkGray;
61 private static int Width = 50;
62 private static int Height = 20;
63 private Random _random;
64 private string _code;
65 private int _brushNameIndex;
66 override protected void OnInit(EventArgs e)
67 {
68 //
69 // CODEGEN: This call is required by the ASP.NET Web Form Designer.
70 //
71 //InitializeComponent();
72 //base.OnInit(e);
73 }
74 /**//**//**////<summary>
75 /// Required method for Designer support - do not modify
76 /// the contents of this method with the code editor.
77 ///</summary>
78 private void InitializeComponent()
79 {
80 //this.Load += new System.EventHandler(this.Page_Load);
81 }
82 /**////<summary>
83 ///
84 ///</summary>
85 ///<param name="sender"></param>
86 ///<param name="e"></param>
87 public void Page_Load(object sender, System.EventArgs e)
88 {
89 if (!IsPostBack)
90 {
91 //
92 // TODO : initialize
93 //
94 this._random = new Random();
95 this._code = GetRandomCode();
96 //
97 // TODO : use Session["code"] save the VerifyCode
98 //
99 Session["code"] = this._code;
100 //
101 // TODO : output Image
102 //
103 this.SetPageNoCache();
104 this.OnPaint();
105 }
106 }
107 /**//**//**////<summary>
108 /// 设置页面不被缓存
109 ///</summary>
110 private void SetPageNoCache()
111 {
112 Response.Buffer = true;
113 Response.ExpiresAbsolute = System.DateTime.Now.AddSeconds(-1);
114 Response.Expires = 0;
115 Response.CacheControl = "no-cache";
116 Response.AppendHeader("Pragma","No-Cache");
117 }
118 /**//**//**////<summary>
119 /// 取得一个 4 位的随机码
120 ///</summary>
121 ///<returns></returns>
122 private string GetRandomCode()
123 {
124 return Guid.NewGuid().ToString().Substring(0, 4);
125 }
126 /**//**//**////<summary>
127 /// 随机取一个字体
128 ///</summary>
129 ///<returns></returns>
130 private Font GetFont()
131 {
132 int fontIndex = _random.Next(0, FontItems.Length);
133 FontStyle fontStyle = GetFontStyle(_random.Next(0, 2));
134 return new Font(FontItems[fontIndex], 12, fontStyle);
135 }
136 /**//**//**////<summary>
137 /// 取一个字体的样式
138 ///</summary>
139 ///<param name="index"></param>
140 ///<returns></returns>
141 private FontStyle GetFontStyle(int index)
142 {
143 switch (index)
144 {
145 case 0:
146 return FontStyle.Bold;
147 case 1:
148 return FontStyle.Italic;
149 default:
150 return FontStyle.Regular;
151 }
152 }
153 /**//**//**////<summary>
154 /// 随机取一个笔刷
155 ///</summary>
156 ///<returns></returns>
157 private Brush GetBrush()
158 {
159 int brushIndex = _random.Next(0, BrushItems.Length);
160 _brushNameIndex = brushIndex;
161 return BrushItems[brushIndex];
162 }
163 /**//**//**////<summary>
164 /// 绘画事件
165 ///</summary>
166 private void OnPaint()
167 {
168 Bitmap objBitmap = null;
169 Graphics g = null;
170 try
171 {
172 objBitmap = new Bitmap(Width, Height);
173 g = Graphics.FromImage(objBitmap);
174 Paint_Background(g);
175 Paint_Text(g);
176 Paint_TextStain(objBitmap);
177 Paint_Border(g);
178 objBitmap.Save(Response.OutputStream, ImageFormat.Gif);
179 Response.ContentType = "image/gif";
180 }
181 catch{}
182 finally
183 {
184 if (null != objBitmap)
185 objBitmap.Dispose();
186 if (null != g)
187 g.Dispose();
188 }
189 }
190 /**//**//**////<summary>
191 /// 绘画背景颜色
192 ///</summary>
193 ///<param name="g"></param>
194 private void Paint_Background(Graphics g)
195 {
196 g.Clear(BackColor);
197 }
198 /**//**//**////<summary>
199 /// 绘画边框
200 ///</summary>
201 ///<param name="g"></param>
202 private void Paint_Border(Graphics g)
203 {
204 g.DrawRectangle(BorderColor, 0, 0, Width - 1, Height - 1);
205 }
206 /**//**//**////<summary>
207 /// 绘画文字
208 ///</summary>
209 ///<param name="g"></param>
210 private void Paint_Text(Graphics g)
211 {
212 g.DrawString(_code, GetFont(), GetBrush(), 3, 1);
213 }
214 /**//**//**////<summary>
215 /// 绘画文字噪音点
216 ///</summary>
217 ///<param name="g"></param>
218 private void Paint_TextStain(Bitmap b)
219 {
220 for (int n=0; n<30; n++)
221 {
222 int x = _random.Next(Width);
223 int y = _random.Next(Height);
224 b.SetPixel(x, y, Color.FromName(BrushName[_brushNameIndex]));
225 }
226 }
227 }
不行啊 出来不了
这里收集了四种asp.net验证码,你可以看看有没有你适用的。。。
http://www.cnblogs.com/baiyuntian/archive/2011/11/10/2244334.html