SoFunction
Updated on 2025-04-06

C# generates random verification code code sharing


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);
        }

        /// <summary>
/// Generate random numbers
        /// </summary>
/// <param name="Length">Generate length</param>
/// <param name="Sleep">Do you want 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 < Length; i++)
            {
                result += (10).ToString();
            }
            return result;
        }
        #endregion

#region Generate random letters and numbers
        /// <summary>
/// Generate random letters and numbers
        /// </summary>
/// <param name="IntStr">Generate Length</param>
        public static string Str(int Length)
        {
            return Str(Length, false);
        }

        /// <summary>
/// Generate random letters and numbers
        /// </summary>
/// <param name="Length">Generate length</param>
/// <param name="Sleep">Do you want 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 < Length; i++)
            {
                int rnd = (0, n);
                result += Pattern[rnd];
            }
            return result;
        }
        #endregion

#region Generate random numbers of random letters
        /// <summary>
/// Generate random numbers of random letters
        /// </summary>
/// <param name="IntStr">Generate Length</param>
        public static string Str_char(int Length)
        {
            return Str_char(Length, false);
        }

        /// <summary>
/// Generate random numbers of random letters
        /// </summary>
/// <param name="Length">Generate length</param>
/// <param name="Sleep">Do you want 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 < Length; i++)
            {
                int rnd = (0, n);
                result += Pattern[rnd];
            }
            return result;
        }
        #endregion
    }

    /// <summary>
/// Verify the picture class
    /// </summary>
    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
        /// <summary>
/// Verification code
        /// </summary>
        public string Text
        {
            get { return ; }
        }

        /// <summary>
/// Verification code picture
        /// </summary>
        public Bitmap Image
        {
            get { return ; }
        }
        #endregion

#region constructor
        public YZMHelper()
        {
            = 0;
            = true;
            = (-1);
            ("pragma", "no-cache");
            = "no-cache";
            = (4);
            CreateImage();
        }
        #endregion

#region Private Method
        /// <summary>
/// Get the next random number
        /// </summary>
/// <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 < 0) value = -value;
            return value;
        }

        /// <summary>
/// Get the next random number
        /// </summary>
/// <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
        /// <summary>
/// Draw the verification code
        /// </summary>
        public void CreateImage()
        {
            int int_ImageWidth = * letterWidth;
            Bitmap image = new Bitmap(int_ImageWidth, letterHeight);
            Graphics g = (image);
            ();
            for (int i = 0; i < 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 < ; 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 < 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;
        }

        /// <summary>
/// font random colors
        /// </summary>
        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 > 300) ? 0 : 400 - int_Red - int_Green;
            int_Blue = (int_Blue > 255) ? 255 : int_Blue;
            return (int_Red, int_Green, int_Blue);
        }

        /// <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>
        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 < ; i++)
            {
                for (int j = 0; j < ; 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 >= 0 && nOldX <
                     && nOldY >= 0 && nOldY < )
                    {
                        (nOldX, nOldY, color);
                    }
                }
            }
            ();
            return destBmp;
        }
        #endregion
    }
}