C# 随机验证码

本文详细解析了一段用于生成ReCaptcha图像验证码的C#代码,包括随机文本生成、图像绘制、线条干扰等关键步骤,展示了如何利用System.Drawing命名空间创建复杂且安全的图像验证码。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

using System;
using System.Drawing;
using System.Drawing.Imaging;
using System.Text;

namespace SystemScan
{
  public class Recaptcha
  {
    private Random rand;

    public Bitmap CreateImage(out string code)
    {
      try
      {
        code = this.GetRandomText();
        Bitmap bitmap = new Bitmap(200, 50, PixelFormat.Format32bppArgb);
        Graphics g = Graphics.FromImage((Image) bitmap);
        Pen pen = new Pen(Color.Yellow);
        Rectangle rect = new Rectangle(0, 0, 200, 50);
        SolidBrush solidBrush1 = new SolidBrush(Color.Maroon);
        SolidBrush solidBrush2 = new SolidBrush(Color.White);
        int num = 0;
        g.DrawRectangle(pen, rect);
        g.FillRectangle((Brush) solidBrush1, rect);
        for (int index = 0; index < code.Length; ++index)
        {
          g.DrawString(code[index].ToString(), new Font("verdana", (float) (8 + new Random().Next(14, 18))), (Brush) solidBrush2, new PointF((float) (8 + num), 10f));
          num += 20;
        }
        this.DrawRandomLines(g);
        return bitmap;
      }
      catch (Exception ex)
      {
        code = string.Empty;
        return (Bitmap) null;
      }
    }

    private void DrawRandomLines(Graphics g)
    {
      SolidBrush solidBrush = new SolidBrush(Color.Yellow);
      for (int index = 0; index < 20; ++index)
        g.DrawLines(new Pen((Brush) solidBrush, 2f), this.GetRandomPoints());
    }

    private Point[] GetRandomPoints()
    {
      this.rand = new Random();
      Point[] pointArray = new Point[2];
      int index1 = 0;
      Point point1 = new Point(this.rand.Next(1, 150), this.rand.Next(1, 150));
      pointArray[index1] = point1;
      int index2 = 1;
      Point point2 = new Point(this.rand.Next(1, 100), this.rand.Next(1, 100));
      pointArray[index2] = point2;
      return pointArray;
    }

    private string GetRandomText()
    {
      string str1 = string.Empty;
      StringBuilder stringBuilder = new StringBuilder();
      if (string.IsNullOrEmpty(str1))
      {
        string str2 = "abcdefghijklmnopqrstuvwxyz1234567890ABCDEFGHIJKLMNOPQRSTUVWXYZ";
        Random random = new Random();
        for (int index = 0; index <= 5; ++index)
          stringBuilder.Append(str2[random.Next(str2.Length)]);
        str1 = stringBuilder.ToString();
      }
      return str1;
    }
  }
}
this.picCaptcha.Image = (Image) new Recaptcha().CreateImage(out this.strCaptcha);

if (this.strCaptcha != this.txtCaptcha.Text)
      {
        int num = (int) MessageBox.Show("Text entered did not match with text in image");
        this.picCaptcha.Image = (Image) new Recaptcha().CreateImage(out this.strCaptcha);
        this.txtCaptcha.Text = "";
        this.txtCaptcha.Focus();
      }

  

转载于:https://www.cnblogs.com/allenfly/p/11463364.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值