ASP.NET産生登錄驗證碼

1.新增CheckCode.aspx

  1.     private Bitmap validateimage;
  2.     private Graphics g;
  3.     protected void Page_Load(object sender, EventArgs e)
  4.     {
  5.         Response.BufferOutput = true;    //特别注意
  6.         Response.Cache.SetExpires(DateTime.Now.AddMilliseconds(-1));//特别注意
  7.         Response.Cache.SetCacheability(HttpCacheability.NoCache);//特别注意
  8.         Response.AppendHeader("Pragma""No-Cache"); //特别注意
  9.         string VNum = MakeValidateCode();
  10.         Session["VNum"] = VNum;//取得验证码,以便后来验证
  11.         ValidateCode(VNum);
  12.     }
  13.     public void ValidateCode(string VNum)
  14.     {
  15.         validateimage = new Bitmap(60, 20, PixelFormat.Format24bppRgb);
  16.         g = Graphics.FromImage(validateimage);
  17.         g.FillRectangle(new LinearGradientBrush(new Point(0, 0), new Point(110, 20), Color.FromArgb(240, 255, 255, 255), Color.FromArgb(240, 255, 255, 255)), 0, 0, 200, 200); //矩形框
  18.         g.DrawString(VNum, new Font("arial", 11), new SolidBrush(Color.Red), new PointF(6, 0));//字体/颜色
  19.         g.Save();
  20.         MemoryStream ms = new MemoryStream();
  21.         validateimage.Save(ms, System.Drawing.Imaging.ImageFormat.Gif);
  22.         Response.ClearContent();
  23.         //Response.C;
  24.         Response.BinaryWrite(ms.ToArray());
  25.         Response.End();
  26.     }
  27.     string MakeValidateCode()
  28.     {
  29.         char[] s = new char[] { '0''1''2''3''4''5''6''7''8''9''a''b''c''d''e''f''g''h''i''j''k''l''m''n''o''p''q''r''s''t''u''v''w''x''y''z''A''B''C''D''E''F''G''H''I''J''K''L''M''N''P''Q''I''S''T''U''V''W''X''Y''Z' };//枚举数组
  30.         string num = "";
  31.         Random r = new Random();
  32.         for (int i = 0; i < 5; i++)
  33.         {
  34.             num += s[r.Next(0, s.Length)].ToString();
  35.         }
  36.         return num;
  37.     }

2.調用

<img src="CheckCode.aspx" id="CheckCode" alt="点击刷新" onclick="reloadcode()" />

  1. function reloadcode()
  2. document.getElementById("CheckCode").src ="CheckCode.aspx";
阅读更多

没有更多推荐了,返回首页