您现在的位置是:首页 >

验证码中的等于号怎么输入 C#中生成验证码的类

火烧 2022-03-13 17:54:36 1059
C#中生成验证码的类   在很多网站中都会用到验证码的功能来防止非法注册用户 下面我就提供一个生成验证码的类   代码如下 u i g Sy tem u i g Sy tem Data u i g S

C#中生成验证码的类  

  在很多网站中都会用到验证码的功能来防止非法注册用户 下面我就提供一个生成验证码的类

  代码如下 using System using System Data using System Configuration using System Collections using System Web using System Web Security using System Web UI using System Web UI WebControls using System Web UI WebControls WebParts using System Web UI HtmlControls using System Drawing

  public partial class checkcode System Web UI Page { protected void Page_Load(object sender EventArgs e)

  { CreateCheckCodeImage(GenerateCheckCode()) }

  private string GenerateCheckCode()

  { int number char code string checkCode = String Empty System Random random = new Random()

  for (int i = i < i++)

  { number = random Next()

  if (number % == )

  code = (char)( + (char)(number % )) else code = (char)( A + (char)(number % ))

  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 * )) ) Graphics g = Graphics FromImage(image) try { //生成随机生成器Random random = new Random() //清空图片背景色g Clear(Color White) //画图片的背景噪音线for (int i = i < i++)

  { int x = random Next(image Width) int x = random Next(image Width) int y = random Next(image Height) int y = random Next(image Height) g DrawLine(new Pen(Color Black) x y x y ) } Font font = new System Drawing Font( Arial (System Drawing FontStyle Bold | System Drawing FontStyle Italic)) System Drawing Drawing D LinearGradientBrush brush = new System Drawing Drawing D LinearGradientBrush(new Rectangle( image Width image Height) Color Blue Color DarkRed f true) g DrawString(checkCode font brush ) //画图片的前景噪音点for (int i = i < 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) image Width image Height ) 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() }

验证码中的等于号怎么输入 C#中生成验证码的类
lishixinzhi/Article/program/net/201311/13063  
永远跟党走
  • 如果你觉得本站很棒,可以通过扫码支付打赏哦!

    • 微信收款码
    • 支付宝收款码