asp.net 驗(yàn)證碼控件
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 /// 驗(yàn)證碼實(shí)例 的摘要說明
12 /// </summary>
13 public class 驗(yàn)證碼實(shí)例 : ihttphandler, irequiressessionstate //在一般處理程序中使用session要實(shí)現(xiàn)該接口,在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)) //生成一個(gè)畫板
21 {
22 random rand = new random();
23 int code = rand.next(1000, 999999); //制定隨機(jī)函數(shù),用于限定字隨機(jī)字符串大。
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)); //畫的圖片相關(guān)參數(shù),(字體,大。,顏色,位置;
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 }