标签:
1 using System; 2 using System.Web; 3 using System.Drawing; 4 using System.Web.SessionState;//IReqiresSessionState的命名空间 5 //点击图片换验证码用imagebutton,不能用时在网页上加?id= 6 public cladd show:IHttpHandler,IRequiresSessionstate 7 { 8 context.Response.ContentType="image/jpeg";//要输出的类型 9 Bitmap img =new Bitmap(50,20);//造空白图 10 Graphics gr=Graphics.FromImage(img);//在那个图上绘制 11 Font font =new Font("宋体",12,FontStyle.Bold);//设置字体 12 SolidBrush brush =new SolidBrush(Color.White);//设置刷子 13 gr.FillREctangle=(brush,0,0,50,20);//刷子绘制的形状 14 brush.Color=Color.Red;//颜色 15 16 17 string s ="1234567890zxcvbnmasdfghjklqwertyuiopZXCVBNMASDFGHJKLQWERTYUIOP"; 18 string str=""; 19 Radom rand =new Random();//初始化随机数 20 string[]arr=new string[4]; 21 for (int i=0;i<4;i++)? 22 { 23 arr[i] =rand.Next(62);//生成一个随机数的起始位置 24 int cont=0; 25 if(i>0) 26 { 27 for(int j=0;j<i;j++) 28 { 29 if(arr[i]==arr[j]) 30 { 31 count++; 32 } 33 } 34 } 35 if(count>0) 36 { 37 i--; 38 continue; 39 } 40 str +=s.Substring(arr[i],1).ToString(); 41 } 42 context.Session["yanzheng"]=str; 43 gr.DrawString(str,font,brush,0,0);//绘制完了图片 44 45 //将图片保存,通过response响应流保存 46 img.Save (context.Response.OutputStream,System.Drawing.Imaging.ImageFormat.Jpeg); 47 public bool IsReusable 48 { 49 get 50 { 51 return false; 52 } 53 } 54 } 55 //Default.aspx.cs 56 //Botton内写 57 if(TextBox3.Text==Session["yanzheng"].ToString()) 58 { 59 Response.Redirest("要跳转的页面") 60 }
标签:
原文地址:http://www.cnblogs.com/zxm1002/p/4968456.html