SoFunction
Updated on 2025-03-08

A complete example of a more beautiful verification code implemented by C#

This article describes a relatively beautiful verification code implemented by C#. Share it for your reference, as follows:

using System;
using ;
using ;
using ;
using ;
using ;
using ;
using ;
public partial class ValidateCode : 
{
 private int letterWidth = 16;//The width range of a single font private int letterHeight = 22;//The height range of a single font private int letterCount = 4;//The number of verification code digits private char[] chars = "0123456789".ToCharArray();
 private string[] fonts = { "Arial", "Georgia" };
 /// <summary>
 /// Generate waveform filter effect /// </summary>
 private const double PI = 3.1415926535897932384626433832795;
 private const double PI2 = 6.283185307179586476925286766559;
 protected void Page_Load(object sender, EventArgs e)
 {
  //Prevent web pages from backing--Cache is prohibited   = 0;
   = true;
   = (-1);
  ("pragma", "no-cache");
   = "no-cache";
  string str_ValidateCode = GetRandomNumberString(letterCount);
  HttpCookie objCookie = new HttpCookie("ValidateCode");
   = str_ValidateCode;
   = "/";
   = (1200);
  (objCookie);
  CreateImage(str_ValidateCode);
 }
 public void CreateImage(string checkCode)
 {
  int int_ImageWidth =  * letterWidth;
  Random newRandom = new Random();
  Bitmap image = new Bitmap(int_ImageWidth, letterHeight);
  Graphics g = (image);
  //Generate random generator  Random random = new Random();
  // White background  ();
  //Draw the background noise line of the picture  for (int i = 0; i < 10; i++)
  {
   int x1 = ();
   int x2 = ();
   int y1 = ();
   int y2 = ();
   (new Pen(), x1, y1, x2, y2);
  }
  //Foreground noise points for drawing pictures  for (int i = 0; i < 10; i++)
  {
   int x = ();
   int y = ();
   (x, y, (()));
  }
  //Verification code characters for random fonts and colors  int findex;
  for (int int_index = 0; int_index < ; int_index++)
  {
   findex = ( - 1);
   string str_char = (int_index, 1);
   Brush newBrush = new SolidBrush(GetRandomColor());
   Point thePos = new Point(int_index * letterWidth + 1 + (3), 1 + (3));//5+1+a+s+p+x
   (str_char, new Font(fonts[findex], 12, ), newBrush, thePos);
  }
  //Gray border  (new Pen(, 1), 0, 0, int_ImageWidth - 1, (letterHeight - 1));
  //Picture twist  //image = TwistImage(image, true, 3, 4);
  //Send the generated image back to the client  MemoryStream ms = new MemoryStream();
  (ms, );
  (); //Image information needs to be output to modify the HTTP header   = "image/Png";
  (());
  ();
  ();
 }
 /// <summary>
 /// Sine curve Wave twisted picture /// </summary>
 /// <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> /// &lt;returns&gt;&lt;/returns&gt;
 public  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;
 }
 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(210);
  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);// 5+1+a+s+p+x
 }
 // Generate random numeric strings public string GetRandomNumberString(int int_NumberLength)
 {
  Random random = new Random();
  string validateCode = ;
  for (int i = 0; i &lt; int_NumberLength; i++)
   validateCode += chars[(0, )].ToString();
  return validateCode;
 }
}

Copy the codeThe code is as follows:

<img alt="Can't see clearly, change to another one" title="Can't see clearly, change to another one" src="" style="cursor:pointer" onclick="=+'?r='+()" />

For more information about C# related content, please check out the topic of this site:Summary of XML file operation skills in C#》、《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.