成人午夜激情影院,小视频免费在线观看,国产精品夜夜嗨,欧美日韩精品一区二区在线播放

ASP.NET 驗證碼(C#)

2010-08-28 10:50:35來源:西部e網作者:

  public class ValidateCode : System.Web.UI.Page
   {
   private void Page_Load(object sender, System.EventArgs e)
   {
   this.CreateCheckCodeImage(GenerateCheckCode());
   }
  
   #region web 窗體設計器生成的代碼
   override protected void OnInit(EventArgs e)
   {
   //
   // CODEGEN: 該調用是 asp.NET web 窗體設計器所必需的。
   //
   InitializeComponent();
   base.OnInit(e);
   }
  
   /// <summary>
   /// 設計器支持所需的方法 - 不要使用代碼編輯器修改
   /// 此方法的內容。
   /// </summary>
   private void InitializeComponent()
   {
   this.Load += new System.EventHandler(this.Page_Load);
   }
   #endregion
  
   private string GenerateCheckCode()
   {
   int number;
   char code;
   string checkCode = String.Empty;
  
   System.Random random = new Random();
  
   for(int i=0; i<5; i++)
   {
   number = random.Next();
  
   if(number % 2 == 0)
   code = (char)('0' + (char)(number % 10));
   else
   code = (char)('A' + (char)(number % 26));
  
   checkCode += code.ToString();
   }
  
   Response.Cookies.Add(new HttpCookie("CheckCode", checkCode));
  
   return checkCode;
   }
  
   private void CreateCheckCodeImage(string checkCode)
   {
   if(checkCode == null || checkCode.Trim() == String.Empty)
   return;
  
   System.Drawing.Bitmap image = new System.Drawing.Bitmap((int)Math.Ceiling((checkCode.Length * 12.5)), 22);
   Graphics g = Graphics.FromImage(image);
  
   try
   {
   //生成隨機生成器
   Random random = new Random();
  
   //清空圖片背景色
   g.Clear(Color.White);
  
   //畫圖片的背景噪音線
   for(int i=0; i<25; i++)
   {
   int x1 = random.Next(image.Width);
   int x2 = random.Next(image.Width);
   int y1 = random.Next(image.Height);
   int y2 = random.Next(image.Height);
  
   g.DrawLine(new Pen(Color.Silver), x1, y1, x2, y2);
   }
  
   Font font = new System.Drawing.Font("Arial", 12, (System.Drawing.FontStyle.Bold | System.Drawing.FontStyle.Italic));
   System.Drawing.Drawing2D.LinearGradientBrush brush = new System.Drawing.Drawing2D.LinearGradientBrush(new Rectangle(0, 0, image.Width, image.Height), Color.Blue, Color.DarkRed, 1.2f, true);
   g.DrawString(checkCode, font, brush, 2, 2);
  
   //畫圖片的前景噪音點
   for(int i=0; i<100; i++)
   {
   int x = random.Next(image.Width);
   int y = random.Next(image.Height);
  
   image.SetPixel(x, y, Color.FromArgb(random.Next()));
   }
  
   //畫圖片的邊框線
   g.DrawRectangle(new Pen(Color.Silver), 0, 0, image.Width - 1, image.Height - 1);
  
   System.IO.MemoryStream ms = new System.IO.MemoryStream();
   image.Save(ms, System.Drawing.Imaging.ImageFormat.Gif);
   Response.ClearContent();
   Response.ContentType = "image/Gif";
   Response.BinaryWrite(ms.ToArray());
   }
   finally
   {
   g.Dispose();
   image.Dispose();
   }
   }
   }

關鍵詞:ASP.NET
主站蜘蛛池模板: 乌拉特后旗| 安岳县| 峨山| 衡山县| 清远市| 阿图什市| 渝中区| 阿拉善右旗| 张家港市| 揭阳市| 永胜县| 大厂| 原平市| 军事| 郎溪县| 金坛市| 北安市| 肥东县| 宝清县| 平度市| 开封县| 禹城市| 天气| 科尔| 湖北省| 大同县| 呈贡县| 瑞安市| 商都县| 武平县| 淳化县| 克东县| 当涂县| 兴国县| 衡东县| 萍乡市| 东山县| 南安市| 鹿邑县| 淮阳县| 建平县|