SoFunction
Updated on 2025-03-10

Small examples of implementing verification codes and refreshing verification codes


/// <summary>
/// Generate verification code picture and save the session name VerificationCode
    /// </summary>
    public static void CreateVerificationCode()
    {
        int number;
        string checkCode = ;

//Random number seeds
        Random randoms = new Random();

for (int i = 0; i < 4; i++) //The length of the check code is 4
        {
//Random integers
            number = ();

//The characters are randomly generated from 0-9, A-Z, and the corresponding ASCII codes are
            //48-57,65-90
            number = number % 36;
            if (number < 10)
            {
                number += 48;
            }
            else
            {
                number += 55;
            }
            checkCode += ((char)number).ToString();
        }

//Save the verification code in the session
        ["VerificationCode"] = checkCode;

//If the verification code is empty, return directly
        if (checkCode == null || () == )
        {
            return;
        }
//Determine the length of the output picture based on the length of the verification code
        image = new (55, 20);//(int)(( * 15))
//Create Graphics object
        Graphics g = (image);
        try
        {
//Generate random number seeds
            Random random = new Random();
//Clear the background color of the picture
            ();
//10 background noise lines for drawing pictures
            //---------------------------------------------------
            for (int i = 0; i < 10; i++)
            {
//The starting point coordinate of the noise line (x1,y1), the end coordinate (x2,y2)
                int x1 = ();
                int x2 = ();
                int y1 = ();
                int y2 = ();

//Draw the noise line with silver
                (new Pen(), x1, y1, x2, y2);
            }
            //---------------------------------------------------
            //Brush b = ;
            //(b, 0, 0, , );
//-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
//The font of the verification code in the output picture: Arial No. 12, rough italic
            Font font = new Font("Arial", 12, ( | ));

//Linear gradient painting brush
            LinearGradientBrush brush = new LinearGradientBrush(new Rectangle(0, 0, , ), , , 1.2f, true);
            (checkCode, font, brush, 2, 2);

//Foreground noise points for drawing pictures 50
            for (int i = 0; i < 50; i++)
            {
                int x = ();
                int y = ();
                (x, y, (()));
            }

//Draw the border line of the picture
            (new Pen(), 0, 0, - 1, - 1);

//Create memory streams to output pictures
            using (MemoryStream ms = new MemoryStream())
            {
//The image format is specified as png
                (ms, );
//Clear all output in the buffer stream
                ();
//The HTTP MIME type of the output stream is set to "image/Png"
                = "image/Jpeg";
//Binary stream of output image
                (());
            }
        }
        finally
        {
//Release Bitmap object and Graphics object
            ();
            ();
        }
    }