ASP.net 验证码
|
源代码网整理以下 public class ValidateCode : System.Web.UI.Page 源代码网整理以下 { 源代码网整理以下 private void Page_Load(object sender, System.EventArgs e) 源代码网整理以下 草地chin ai tp ower6yv972b 源代码网整理以下 #region 源代码网整理以下 override protected void OnInit(EventArgs e) 源代码网整理以下 { 源代码网整理以下 // 软件开发网 www.mscto.com 源代码网整理以下 // CODEGEN: 该调用是 asp.NET web 窗体设计器所必需的。 源代码网整理以下 // 源代码网整理以下 InitializeComponent(); 源代码网整理以下 base.OnInit(e); 源代码网整理以下 } 源代码网整理以下 /// 源代码网整理以下 /// 设计器支持所需的方法 - 不要使用代码编辑器修改 源代码网整理以下 /// 此方法的内容。 源代码网整理以下 /// 软件开发网 www.mscto.com 源代码网整理以下 private void InitializeComponent() 源代码网整理以下 { 源代码网整理以下 this.Load += new System.EventHandler(this.Page_Load); 源代码网整理以下 } 源代码网整理以下 #endregion草地chin ai tp ower6yv972b 源代码网整理以下 private string GenerateCheckCode() 源代码网整理以下 { 源代码网整理以下 int number; 源代码网整理以下 char code; 源代码网整理以下 string checkCode = String.Empty;草地chin ai tp ower6yv972b 源代码网整理以下 System.Random random = new Random();草地chin ai tp ower6yv972b 源代码网整理以下 for(int i=0; i<5; i++) 源代码网整理以下 { 源代码网整理以下 number = random.Next();草地chin ai tp ower6yv972b 源代码网整理以下 if(number % 2 == 0) 源代码网整理以下 code = (char)("0" + (char)(number % 10)); 源代码网整理以下 else 源代码网整理以下 code = (char)("A" + (char)(number % 26));草地chin ai tp ower6yv972b 源代码网整理以下 checkCode += code.ToString(); 源代码网整理以下 }草地chin ai tp ower6yv972b 源代码网整理以下 Response.Cookies.Add(new HttpCookie("CheckCode", checkCode));草地chin ai tp ower6yv972b 软件开发网 www.mscto.com 源代码网整理以下 return checkCode; 源代码网整理以下 }草地chin ai tp ower6yv972b 源代码网整理以下 private void CreateCheckCodeImage(string checkCode) 源代码网整理以下 { 源代码网整理以下 if(checkCode == null || checkCode.Trim() == String.Empty) 软件开发网 www.mscto.com 源代码网整理以下 return;草地chin ai tp ower6yv972b 源代码网整理以下 System.Drawing.Bitmap image = new System.Drawing.Bitmap((int)Math.Ceiling((checkCode.Length * 12.5)), 22); 源代码网整理以下 Graphics g = Graphics.FromImage(image);草地chin ai tp ower6yv972b 源代码网整理以下 try 软件开发网 www.mscto.com 源代码网整理以下 { 源代码网整理以下 //生成随机生成器 源代码网整理以下 Random random = new Random();草地chin ai tp ower6yv972b 源代码网整理以下 //清空图片背景色 源代码网整理以下 g.Clear(Color.White);草地chin ai tp ower6yv972b 软件开发网 www.mscto.com 源代码网整理以下 //画图片的背景噪音线 源代码网整理以下 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);草地chin ai tp ower6yv972b 源代码网整理以下 g.DrawLine(new Pen(Color.Silver), x1, y1, x2, y2); 软件开发网 www.mscto.com 源代码网整理以下 }草地chin ai tp ower6yv972b 源代码网整理以下 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);草地chin ai tp ower6yv972b 源代码网整理以下 //画图片的前景噪音点 源代码网整理以下 for(int i=0; i<100; i++) 源代码网整理以下 { 源代码网整理以下 int x = random.Next(image.Width); 源代码网整理以下 int y = random.Next(image.Height);草地chin ai tp ower6yv972b 软件开发网 www.mscto.com 源代码网整理以下 image.SetPixel(x, y, Color.FromArgb(random.Next())); 源代码网整理以下 }草地chin ai tp ower6yv972b 源代码网整理以下 //画图片的边框线 源代码网整理以下 g.DrawRectangle(new Pen(Color.Silver), 0, 0, image.Width - 1, image.Height - 1);草地chin ai tp ower6yv972b 源代码网整理以下 System.IO.MemoryStream ms = new System.IO.MemoryStream(); 源代码网整理以下 image.Save(ms, System.Drawing.Imaging.ImageFormat.Gif); 源代码网整理以下 Response.ClearContent(); 源代码网整理以下 Response.ContentType = "image/Gif"; 软件开发网 www.mscto.com 源代码网整理以下 Response.BinaryWrite(ms.ToArray()); 源代码网整理以下 } 源代码网整理以下 finally 源代码网整理以下 } 源代码网整理以下 } 源代码网推荐 源代码网供稿. |
