随机生成字母数字验证码,可定义验证码的长度 (转)

 
java 代码
 
  1. using System;  
  2. using System.Data;  
  3. using System.Configuration;  
  4. using System.Collections;  
  5. using System.Web;  
  6. using System.Web.Security;  
  7. using System.Web.UI;  
  8. using System.Web.UI.WebControls;  
  9. using System.Web.UI.WebControls.WebParts;  
  10. using System.Web.UI.HtmlControls;  
  11. using System.Drawing;  
  12. using System.Drawing.Imaging;  
  13.   
  14. public partial class Login : System.Web.UI.Page  
  15. ...{  
  16.     protected void Page_Load(object sender, EventArgs e)  
  17.     ...{  
  18.         if (!IsPostBack)  
  19.         ...{  
  20.         this.CreateCheckCodeImage(GenerateCheckCode());  
  21.         }  
  22.    }  
  23.   private string GenerateCheckCode()  
  24.     ...{  
  25.         int number;  
  26.         char code;  
  27.         string checkCode = String.Empty;  
  28.   
  29.         System.Random random = new Random();  
  30.   
  31.         for (int i = 0; i < 15; i++)  
  32.         ...{  
  33.             number = random.Next();  
  34.             if (number % 2 == 0)  
  35.                 code = (char)('0' + (char)(number % 10));  
  36.             else  
  37.                 code = (char)('A' + (char)(number % 26));  
  38.   
  39.             checkCode += code.ToString();  
  40.         }  
  41.         Response.Cookies.Add(new HttpCookie("CheckCode", checkCode));  
  42.         return checkCode;  
  43.     }  
  44.   
  45.     private void CreateCheckCodeImage(string checkCode)  
  46.     ...{  
  47.         if (checkCode == null || checkCode.Trim() == String.Empty)  
  48.             return;  
  49.   
  50.         System.Drawing.Bitmap image = new System.Drawing.Bitmap((int)Math.Ceiling((checkCode.Length * 12.5)), 22);  
  51.         Graphics g = Graphics.FromImage(image);  
  52.   
  53.         try  
  54.         ...{  
  55.             //生成随机生成器  
  56.             Random random = new Random();  
  57.   
  58.             //清空图片背景色  
  59.             g.Clear(Color.White);  
  60.   
  61.             //画图片的背景噪音线  
  62.             for (int i = 0; i < 35; i++)  
  63.             ...{  
  64.                 int x1 = random.Next(image.Width);  
  65.                 int x2 = random.Next(image.Width);  
  66.                 int y1 = random.Next(image.Height);  
  67.                 int y2 = random.Next(image.Height);  
  68.   
  69.                 g.DrawLine(new Pen(Color.Silver), x1, y1, x2, y2);  
  70.             }  
  71.   
  72.             Font font = new System.Drawing.Font("Arial"12, (System.Drawing.FontStyle.Bold | System.Drawing.FontStyle.Italic));  
  73.             System.Drawing.Drawing2D.LinearGradientBrush brush = new System.Drawing.Drawing2D.LinearGradientBrush(new Rectangle(00, image.Width, image.Height), Color.Blue, Color.DarkRed, 1.2f, true);  
  74.             g.DrawString(checkCode, font, brush, 22);  
  75.   
  76.             //画图片的前景噪音点  
  77.             for (int i = 0; i < 180; i++)  
  78.             ...{  
  79.                 int x = random.Next(image.Width);  
  80.                 int y = random.Next(image.Height);  
  81.   
  82.                 image.SetPixel(x, y, Color.FromArgb(random.Next()));  
  83.             }  
  84.   
  85.             //画图片的边框线  
  86.             g.DrawRectangle(new Pen(Color.Silver), 00, image.Width - 1, image.Height - 1);  
  87.            image.Save(Server.MapPath("imgs") + "\VaImag.gif", ImageFormat.Gif);  
  88.         }  
  89.         finally  
  90.         ...{  
  91.             g.Dispose();  
  92.             image.Dispose();  
  93.         }  
  94.     }  
  95. }  
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值