SoFunction
Updated on 2025-03-07

(C#) Code that generates random verification codes


using System; 
using ; 
using ; 
using ; 
using ; 
using ; 
using ; 
using ; 
using ; 
using ; 
using ; 
/**//// <summary> 
///  
///** (C#) Generate verification code **
///  
///  File:  
///  
///   Author: Zxjay Piaoyao)
///  
///  E-Mail: tda7264@ 
///  
///  Date: 07-04-10 
///  
/// </summary> 
public partial class GenerateCheckCode :  
...{ 
    protected void Page_Load(object sender, EventArgs e) 
    ...{ 
        string chkCode = ; 
//Color list, used for verification code, noise lines, noise
        Color[] color =...{ , , , , , , ,  }; 
//Font list, used for verification code
        string[] font =...{ "Times New Roman", "MS Mincho", "Book Antiqua", "Gungsuh", "PMingLiU", "Impact" }; 
//The character set of verification code has removed some easily confusing characters.
        char[] character =...{ '2', '3', '4', '5', '6', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'J', 'K', 'L', 'M', 'N', 'P', 'R', 'S', 'T', 'W', 'X', 'Y' }; 
        Random rnd = new Random(); 
// Generate verification code string
        for (int i = 0; i < 4; i++) 
        ...{ 
            chkCode += character[()]; 
        } 
        Bitmap bmp = new Bitmap(100, 40); 
        Graphics g = (bmp); 
        (); 
//Draw noise lines
        for (int i = 0; i < 10; i++) 
        ...{ 
            int x1 = (100); 
            int y1 = (40); 
            int x2 = (100); 
            int y2 = (40); 
            Color clr = color[()]; 
            (new Pen(clr), x1, y1, x2, y2); 
        } 
//Draw the verification code string
        for (int i = 0; i < ; i++) 
        ...{ 
            string fnt = font[()]; 
            Font ft = new Font(fnt, 18); 
            Color clr = color[()]; 
            (chkCode[i].ToString(), ft, new SolidBrush(clr), (float)i * 20 + 8, (float)8); 
        } 
//Draw noise
        for (int i = 0; i < 100; i++) 
        ...{ 
            int x = (); 
            int y = (); 
            Color clr = color[()]; 
            (x, y, clr); 
        } 
//Clear the output cache of this page and set the page without cache.
         = true; 
         = (0); 
         = 0; 
         = "no-cache"; 
        ("Pragma", "No-Cache"); 
//Write the verification code image to the memory stream and output it in "image/Png" format
        MemoryStream ms = new MemoryStream(); 
        try 
        ...{ 
            (ms, ); 
            (); 
             = "image/Png"; 
            (()); 
        } 
        finally 
        ...{ 
//Explanatory release of resources
            (); 
            (); 
        } 
    }