SoFunction
Updated on 2025-03-07

How to generate verification code pictures in WinForm

This article describes the method of WinForm to generate verification code pictures. Share it for your reference, as follows:

1. Create ValidCode class:

public class ValidCode
{
  #region Private Fields
  private const double PI = 3.1415926535897932384626433832795;
  private const double PI2 = 6.283185307179586476925286766559;
  //private readonly int _wordsLen = 4;
  private int _len;
  private CodeType _codetype;
  private readonly Single _jianju = (float)18.0;
  private readonly Single _height = (float)24.0;
  private string _checkCode;
  #endregion
  #region Public Property
  public string CheckCode
  {
   get
   {
    return _checkCode;
   }
  }
  #endregion
  #region Constructors
  /// <summary>
  /// public constructors
  /// </summary>
  /// <param name="len"> Verification code length </param>  /// <param name="ctype"> Verification code type: letters, numbers, letters + numbers </param>  public ValidCode(int len, CodeType ctype)
  {
   this._len = len;
   this._codetype = ctype;
  }
  #endregion
  #region Public Field
  public enum CodeType { Words, Numbers, Characters, Alphas }
  #endregion
  #region Private Methods
  private string GenerateNumbers()
  {
   string strOut = "";
    random = new Random();
   for (int i = 0; i &lt; _len; i++)
   {
    string num = ((10000) % 10);
    strOut += num;
   }
   return ();
  }
  private string GenerateCharacters()
  {
   string strOut = "";
    random = new Random();
   for (int i = 0; i &lt; _len; i++)
   {
    string num = ((char)(65 + (10000) % 26));
    strOut += num;
   }
   return ();
  }
  //
  private string GenerateAlphas()
  {
   string strOut = "";
   string num = "";
    random = new Random();
   for (int i = 0; i &lt; _len; i++)
   {
    if ((500) % 2 == 0)
    {
     num = ((10000) % 10);
    }
    else
    {
     num = ((char)(65 + (10000) % 26));
    }
    strOut += num;
   }
   return ();
  }
  private  TwistImage(Bitmap srcBmp, bool bXDir, double dMultValue, double dPhase)
  {
    destBmp = new Bitmap(, );
   // Fill the bitmap background as white    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 ? (PI2 * (double)j) / dBaseAxisLen : (PI2 * (double)i) / dBaseAxisLen;
     dx += dPhase;
     double dy = (dx);
     // Get the color of the current point     int nOldX = 0, nOldY = 0;
     nOldX = bXDir ? i + (int)(dy * dMultValue) : i;
     nOldY = bXDir ? j : j + (int)(dy * dMultValue);
      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
  #region Public Methods
  public Stream CreateCheckCodeImage()
  {
   string checkCode;
   switch (_codetype)
   {
    case :
     checkCode = GenerateAlphas();
     break;
    case :
     checkCode = GenerateNumbers();
     break;
    case :
     checkCode = GenerateCharacters();
     break;
    default:
     checkCode = GenerateAlphas();
     break;
   }
   this._checkCode = checkCode;
   MemoryStream ms = null;
   //
   if (checkCode == null || () == )
    return null;
   Bitmap image = new ((int)(( * _jianju)), (int)_height);
   Graphics g = (image);
   try
   {
    Random random = new Random();
    ();
    // Background noise line for drawing pictures    for (int i = 0; i &lt; 18; i++)
    {
     int x1 = ();
     int x2 = ();
     int y1 = ();
     int y2 = ();
     (new Pen((()), 1), x1, y1, x2, y2);
    }
    Font font = new ("Times New Roman", 14, );
    LinearGradientBrush brush = new LinearGradientBrush(new Rectangle(0, 0, , ), , , 1.2f, true);
    if (_codetype != )
    {
     for (int i = 0; i &lt; ; i++)
     {
      ((i, 1), font, brush, 2 + i * _jianju, 1);
     }
    }
    else
    {
     (checkCode, font, brush, 2, 2);
    }
    // Foreground noise points for drawing pictures    for (int i = 0; i &lt; 150; i++)
    {
     int x = ();
     int y = ();
     (x, y, (()));
    }
    // Waveform filter effect for drawing pictures    if (_codetype != )
    {
     image = TwistImage(image, true, 3, 1);
    }
    // Draw the border lines of the picture    (new Pen(), 0, 0,  - 1,  - 1);
    ms = new ();
    (ms, );
   }
   finally
   {
    ();
    ();
   }
   return ms;
  }
  #endregion
}

2. Generate verification code picture code:

//Parameter 1: Generate a verification code picture of several characters Parameter 2: The form of verification code (there are mixed numbers, letters, and letters)ValidCode validCode = new ValidCode(5, );
this. = (());

For more information about C# related content, please check out the topic of this site:Tutorial on the usage of common C# controls》、《Summary of WinForm control usage》、《C# data structure and algorithm tutorial》、《Introduction to C# object-oriented programming tutorial"and"Summary of thread usage techniques for C# programming

I hope this article will be helpful to everyone's C# programming.