SoFunction
Updated on 2025-03-07

C# generates a beautiful verification code complete code class

Without further ado, please see the code:

using System;
using ;
using ;
using ;
namespace 
{
  /// <summary>
  /// Verification code class  /// </summary>
  public class Rand
  {
    #region Generate random numbers    /// <summary>
    /// Generate random numbers    /// </summary>
    /// <param name="length">Generate length</param>    public static string Number(int Length)
    {
      return Number(Length, false);
    }
    /// &lt;summary&gt;
    /// Generate random numbers    /// &lt;/summary&gt;
    /// <param name="Length">Generate length</param>    /// <param name="Sleep">Whether to block the current thread before generation to avoid duplication</param>    public static string Number(int Length, bool Sleep)
    {
      if (Sleep) (3);
      string result = "";
       random = new Random();
      for (int i = 0; i &lt; Length; i++)
      {
        result += (10).ToString();
      }
      return result;
    }
    #endregion
    #region Generate random letters and numbers    /// &lt;summary&gt;
    /// Generate random letters and numbers    /// &lt;/summary&gt;
    /// <param name="IntStr">Generate length</param>    public static string Str(int Length)
    {
      return Str(Length, false);
    }
    /// &lt;summary&gt;
    /// Generate random letters and numbers    /// &lt;/summary&gt;
    /// <param name="Length">Generate length</param>    /// <param name="Sleep">Whether to block the current thread before generation to avoid duplication</param>    public static string Str(int Length, bool Sleep)
    {
      if (Sleep) (3);
      char[] Pattern = new char[] { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z' };
      string result = "";
      int n = ;
       random = new Random(~unchecked((int)));
      for (int i = 0; i &lt; Length; i++)
      {
        int rnd = (0, n);
        result += Pattern[rnd];
      }
      return result;
    }
    #endregion
    #region Generate random numbers of random letters    /// &lt;summary&gt;
    /// Generate random numbers of pure letters    /// &lt;/summary&gt;
    /// <param name="IntStr">Generate length</param>    public static string Str_char(int Length)
    {
      return Str_char(Length, false);
    }
    /// &lt;summary&gt;
    /// Generate random numbers of pure letters    /// &lt;/summary&gt;
    /// <param name="Length">Generate length</param>    /// <param name="Sleep">Whether to block the current thread before generation to avoid duplication</param>    public static string Str_char(int Length, bool Sleep)
    {
      if (Sleep) (3);
      char[] Pattern = new char[] { 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z' };
      string result = "";
      int n = ;
       random = new Random(~unchecked((int)));
      for (int i = 0; i &lt; Length; i++)
      {
        int rnd = (0, n);
        result += Pattern[rnd];
      }
      return result;
    }
    #endregion
  }
  /// &lt;summary&gt;
  /// Verify the picture class  /// &lt;/summary&gt;
  public class YZMHelper
  {
    #region Private Field    private string text;
    private Bitmap image;
    private int letterCount = 4;  //The number of verification code digits    private int letterWidth = 16; //The width range of a single font    private int letterHeight = 20; //The height range of a single font    private static byte[] randb = new byte[4];
    private static RNGCryptoServiceProvider rand = new RNGCryptoServiceProvider();
    private Font[] fonts = 
  {
    new Font(new FontFamily("Times New Roman"),10 +Next(1),),
    new Font(new FontFamily("Georgia"), 10 + Next(1),),
    new Font(new FontFamily("Arial"), 10 + Next(1),),
    new Font(new FontFamily("Comic Sans MS"), 10 + Next(1),)
  };
    #endregion
    #region Public properties    /// &lt;summary&gt;
    /// Verification code    /// &lt;/summary&gt;
    public string Text
    {
      get { return ; }
    }
    /// &lt;summary&gt;
    /// Verification code picture    /// &lt;/summary&gt;
    public Bitmap Image
    {
      get { return ; }
    }
    #endregion
    #region constructor    public YZMHelper()
    {
       = 0;
       = true;
 = (-1);
      ("pragma", "no-cache");
       = "no-cache";
       = (4);
      CreateImage();
    }
    #endregion
    #region Private Method    /// &lt;summary&gt;
    /// Get the next random number    /// &lt;/summary&gt;
    /// <param name="max">maximum value</param>    private static int Next(int max)
    {
      (randb);
      int value = BitConverter.ToInt32(randb, 0);
      value = value % (max + 1);
      if (value &lt; 0) value = -value;
      return value;
    }
    /// &lt;summary&gt;
    /// Get the next random number    /// &lt;/summary&gt;
    /// <param name="min">minimum value</param>    /// <param name="max">maximum value</param>    private static int Next(int min, int max)
    {
      int value = Next(max - min) + min;
      return value;
    }
    #endregion
    #region Public Method    /// &lt;summary&gt;
    /// Draw the verification code    /// &lt;/summary&gt;
    public void CreateImage()
    {
      int int_ImageWidth =  * letterWidth;
      Bitmap image = new Bitmap(int_ImageWidth, letterHeight);
      Graphics g = (image);
      ();
      for (int i = 0; i &lt; 2; i++)
      {
        int x1 = Next( - 1);
        int x2 = Next( - 1);
        int y1 = Next( - 1);
        int y2 = Next( - 1);
        (new Pen(), x1, y1, x2, y2);
      }
      int _x = -12, _y = 0;
      for (int int_index = 0; int_index &lt; ; int_index++)
      {
        _x += Next(12, 16);
        _y = Next(-2, 2);
        string str_char = (int_index, 1);
        str_char = Next(1) == 1 ? str_char.ToLower() : str_char.ToUpper();
        Brush newBrush = new SolidBrush(GetRandomColor());
        Point thePos = new Point(_x, _y);
        (str_char, fonts[Next( - 1)], newBrush, thePos);
      }
      for (int i = 0; i &lt; 10; i++)
      {
        int x = Next( - 1);
        int y = Next( - 1);
        (x, y, (Next(0, 255), Next(0, 255), Next(0, 255)));
      }
      image = TwistImage(image, true, Next(1, 3), Next(4, 6));
      (new Pen(, 1), 0, 0, int_ImageWidth - 1, (letterHeight - 1));
       = image;
    }
    /// &lt;summary&gt;
    /// font random colors    /// &lt;/summary&gt;
    public Color GetRandomColor()
    {
      Random RandomNum_First = new Random((int));
      (RandomNum_First.Next(50));
      Random RandomNum_Sencond = new Random((int));
      int int_Red = RandomNum_First.Next(180);
      int int_Green = RandomNum_Sencond.Next(180);
      int int_Blue = (int_Red + int_Green &gt; 300) ? 0 : 400 - int_Red - int_Green;
      int_Blue = (int_Blue &gt; 255) ? 255 : int_Blue;
      return (int_Red, int_Green, int_Blue);
    }
    /// &lt;summary&gt;
    /// Sine curve Wave twisted picture    /// &lt;/summary&gt;
    /// <param name="srcBmp">Picture path</param>    /// <param name="bXDir">Select as True if distorted</param>    /// <param name="nMultValue">The amplitude multiple of the waveform, the greater the degree of distortion, generally 3</param>    /// <param name="dPhase">The starting phase of the waveform, value interval [0-2*PI)</param>    public  TwistImage(Bitmap srcBmp, bool bXDir, double dMultValue, double dPhase)
    {
      double PI = 6.283185307179586476925286766559;
      Bitmap destBmp = new Bitmap(, );
      Graphics graph = (destBmp);
      (new SolidBrush(), 0, 0, , );
      ();
      double dBaseAxisLen = bXDir ? (double) : (double);
      for (int i = 0; i &lt; ; i++)
      {
        for (int j = 0; j &lt; ; j++)
        {
          double dx = 0;
          dx = bXDir ? (PI * (double)j) / dBaseAxisLen : (PI * (double)i) / dBaseAxisLen;
          dx += dPhase;
          double dy = (dx);
          int nOldX = 0, nOldY = 0;
          nOldX = bXDir ? i + (int)(dy * dMultValue) : i;
          nOldY = bXDir ? j : j + (int)(dy * dMultValue);
          Color color = (i, j);
          if (nOldX &gt;= 0 &amp;&amp; nOldX &lt; 
           &amp;&amp; nOldY &gt;= 0 &amp;&amp; nOldY &lt; )
          {
            (nOldX, nOldY, color);
          }
        }
      }
      ();
      return destBmp;
    }
    #endregion
  }
}

The above is all the content of this article. I hope that the content of this article will help you study or work. I also hope to support me more!