网站验证码

 1 // 直接复制进去就可以用了
 2
 3 public  System.Web.UI.WebControls. Image  verifyNumber = new System.Web.UI.WebControls. Image ();
 4    private void Page_Load(object sender, System.EventArgs e)
 5    { 
 6     string tmp  =  RndNum( Convert .ToInt16( 6 ));
 7     Session [ "verify" ] = tmp;
 8     ValidateCode(tmp);
 9    }
10    private void ValidateCode(string VNum) 
11    {
12     Bitmap Img = null ;
13     Graphics g = null ;
14     MemoryStream ms = null
15      int  gheight = VNum.Length * 9 ;
16     Img = new Bitmap(gheight, 18 );
17     g = Graphics.FromImage(Img);
18      // 背景颜色
19     g.Clear(Color.WhiteSmoke);
20      // 文字字体
21     Font f = new Font("Tahoma", 9 );
22      // 文字颜色
23     SolidBrush s = new SolidBrush(Color.Red);
24     g.DrawString(VNum,f,s, 3 , 3 );
25     ms = new MemoryStream();
26     Img. Save (ms,ImageFormat.Jpeg);
27     Response.ClearContent(); 
28     Response.ContentType = " image / Jpeg";
29     Response.BinaryWrite(ms.ToArray());
30     g.Dispose();
31     Img.Dispose();
32     Response. End ();
33    }
34    private string RndNum( int  VcodeNum)
35    {
36     string MaxNum = "";
37     string MinNum = "";
38      for ( int  i = 0 ;i    {
39      MaxNum = MaxNum + " 9 ";
40     }
41     MinNum = MaxNum.Remove( 0 , 1 );
42     Random rd = new Random();   
43     string VNum = Convert .ToString(rd. Next ( Convert .ToInt32(MinNum), Convert .ToInt32(MaxNum)));
44      return  VNum;
45    }
46  
47

你可能感兴趣的:(验证码)