北京北大青鳥校區學術老師指導:ASP.NET驗證碼

      此技術文章由北京北大青鳥校區學術部丁老師提供:
      1.創建“一般處理程序”(北京北大青鳥校區
      using System;
      using System.IO;
      using System.Drawing;
      using System.Drawing.Imaging;
      using System.Text;
      using System.Collections;
      using System.Web;
      using System.Web.UI;
      using System.Web.UI.WebControls;
      using System.Web.UI.WebControls.WebParts;
      using System.Web.UI.HtmlControls;
      namespace WebApplication1
      {
      ///
      /// ChkCode 的摘要說明
      ///
      public class ChkCode : IHttpHandler
      {
      //驗證碼的字符集
      private readonly char[] character = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ".ToCharArray();
      public void ProcessRequest(HttpContext context)
      {
      string chkCode = string.Empty;
      //顏色列表,用于驗證碼、噪線、噪點
      Color[] color = { Color.Black, Color.Red, Color.Blue, Color.Green, Color.Orange, Color.Brown, Color.Brown, Color.DarkBlue };
      //字體列表,用于驗證碼
      string[] font = { "Times New Roman", "MS Mincho", "Book Antiqua", "Gungsuh", "PMingLiU", "Impact" };
      Random rnd = new Random();
      //生成驗證碼字符串
      for (int i = 0; i < 4; i++)
      {
      chkCode += character[rnd.Next(character.Length)];
      }
      //將驗證碼寫入Session
      context.Session["ChkCode"] = chkCode;
      Bitmap bmp = new Bitmap(100, 40);
      Graphics g = Graphics.FromImage(bmp);
      g.Clear(Color.White);
      //畫噪線
      for (int i = 0; i < 10; i++)
      {
      int x1 = rnd.Next(100);
      int y1 = rnd.Next(40);
      int x2 = rnd.Next(100);
      int y2 = rnd.Next(40);
      Color clr = color[rnd.Next(color.Length)];
      g.DrawLine(new Pen(clr), x1, y1, x2, y2);
      }
      //畫驗證碼字符串 (北京北大青鳥校區
      for (int i = 0; i < chkCode.Length; i++)
      {
      string fnt = font[rnd.Next(font.Length)];
      Font ft = new Font(fnt, 18);
      Color clr = color[rnd.Next(color.Length)];
      g.DrawString(chkCode[i].ToString(), ft, new SolidBrush(clr), (float)i * 20 + 8, (float)8);
      }
      //畫噪點
      for (int i = 0; i < 100; i++)
      {
      int x = rnd.Next(bmp.Width);
      int y = rnd.Next(bmp.Height);
      Color clr = color[rnd.Next(color.Length)];
      bmp.SetPixel(x, y, clr);
      }
      //清除該頁輸出緩存,設置該頁無緩存
      context.Response.Buffer = true;
      context.Response.ExpiresAbsolute = System.DateTime.Now.AddMilliseconds(0);
      context.Response.Expires = 0;
      context.Response.CacheControl = "no-cache";
      context.Response.AppendHeader("Pragma", "No-Cache");
      //將驗證碼圖片寫入內存流,并將其以 "image/Png" 格式輸出
      MemoryStream ms = new MemoryStream();
      try
      {
      bmp.Save(ms, ImageFormat.Png);
      context.Response.ClearContent();
      context.Response.ContentType = "image/Png";
      context.Response.BinaryWrite(ms.ToArray());
      }
      finally
      {
      //顯式釋放資源
      bmp.Dispose();
      g.Dispose();
      }
      }
      public bool IsReusable
      {
      get
      {
      return false;
      }
      }
      }
      }(北京北大青鳥校區

      2.創建測試頁


      (北京北大青鳥校區
      北大青鳥網上報名
      北大青鳥招生簡章
      主站蜘蛛池模板: 激情无码亚洲一区二区三区| 国产精品一区二区久久| 国产免费一区二区三区| 国产精品亚洲不卡一区二区三区| 无码国产精品一区二区免费式芒果| 男人的天堂精品国产一区| 国精产品一区一区三区有限在线| 3d动漫精品啪啪一区二区免费| 福利一区国产原创多挂探花| 国产精品无码一区二区在线观 | 久久91精品国产一区二区| 亚洲国产综合精品一区在线播放| 中文字幕人妻第一区| 国产电影一区二区| 日韩精品无码免费一区二区三区 | 中文字幕无码不卡一区二区三区| 国产无线乱码一区二三区 | 亚洲天堂一区二区三区四区| 亚洲色欲一区二区三区在线观看| 男人的天堂亚洲一区二区三区| 国产无吗一区二区三区在线欢| 韩国精品福利一区二区三区| 国产福利酱国产一区二区| 色欲AV蜜桃一区二区三| 一区二区无码免费视频网站| 波多野结衣久久一区二区| 在线精品一区二区三区| 日本福利一区二区| 美女免费视频一区二区三区| 精品视频在线观看一区二区| 视频在线一区二区三区| 精品日韩在线视频一区二区三区| 视频一区在线播放| 亚洲一区二区三区自拍公司| 久久中文字幕一区二区| 亚洲第一区二区快射影院| 怡红院AV一区二区三区| 一区二区三区高清在线| 一本大道东京热无码一区| 乱精品一区字幕二区| 海角国精产品一区一区三区糖心 |