asp.net 验证码控件
1 using system;
2 using system.collections.generic;
3 using system.linq;
4 using system.web;
5 using system.drawing;
6 using system.drawing.imaging;
7 using system.web.sessionstate;
8 namespace webapp
9 {
10 /// <summary>
11 /// 验证码实例 的摘要说明
12 /// </summary>
13 public class 验证码实例 : ihttphandler, irequiressessionstate //在一般处理程序中使用session要实现该接口,在system.web.sessionstate中;
14 {
15 public void processrequest(httpcontext context)
16 {
17 context.response.contenttype = "image/jpeg"; //返回jpg类型;
18 using (bitmap bitmap = new bitmap(140, 80)) //像素大小;
19 {
20 using (graphics g = graphics.fromimage(bitmap)) //生成一个画板
21 {
22 random rand = new random();
23 int code = rand.next(1000, 999999); //制定随机函数,用于限定字随机字符串大小;
24 string strcode = code.tostring();
25 httpcontext.current.session["code"] = strcode; //在一般处理程序中使用session接口;
26 g.clear(color.yellowgreen); //指定背景颜色;
27 g.drawstring(strcode, new font("微输雅黑", 20), brushes.white, new pointf(15, 25)); //画的图片相关参数,(字体,大小),颜色,位置;
28 bitmap.save(context.response.outputstream, imageformat.jpeg); //输出到流中并保存为jpg格式;
29 }
30 }
31 }
32 public bool isreusable
33 {
34 get
35 {
36 return false;
37 }
38 }
39 }
40 }