One: code Generation Implementation codes
protected void Page_Load (object sender, EventArgs E)
{
String Validatecode = Createvalidatecode ();//generate Verification Code
Bitmap Bitmap = new Bitmap (imgwidth, imgheight);//generate Bitmap image
Disturbbitmap (bitmap); Image background
Drewvalidatecode (bitmap, Validatecode);//draw captcha image
Bitmap. Save (response.outputstream, imageformat.gif);//preserve image, wait for output
}
private int codelen = 4;//captcha length
private int fineness = 85;//picture sharpness
& nbsp; private int imgwidth = 48;//picture width
private int imgheight = 24;//picture height
&nbs P private string fontFamily = "times New Roman";//font name
private int fontSize = 14;//font size
&nb sp; //private int fontstyle = 0;//font style
private int posX = 0;//draw starting coordinates x
private int posY = 0;//draw coordinates y
private String Createvalidatecode ()//generate verification Code
{
string validatecode = "";
Random random = new random ();//random Number Object
for (int i = 0; i < codelen; i++)//loop generates Per-digit value
{
&nb sp; int n = random. Next (10);//digital
validatecode + = n.tostring ();
}
session["vcode"] = Validatecode;//save verification Code This session is called at the Foreground.
return validatecode;//returns verification code
}
private void Disturbbitmap (Bitmap Bitmap)//image background
{
random random = new random ();//generate
for ( int i = 0; I < bitmap. Width; I++)//generate
{
from pixel by bit by looping through nesting for (int j = 0; J < bitmap. Height; J + +)
{
if (random. Next (<= This.fineness)
bitmap. SetPixel (i, j, color.lightgray);
}
}
}
private void Drewvalidatecode (Bitmap Bitmap, string Validatecode)//draw authenticode image
{
Graphics g = Graphics.fromimage (bitmap);//get the drawing object
Font font = new Font (fontFamily, fontSize, Fontstyle.bold);//set drawing font
g.DrawString (validatecode, font, brushes.black, posX, posY);//draw captcha image
}
Second: Verify the correctness of the verification code when landing
If (TextBox1.Text.ToUpper (). Equals (session["vcode"]+ "")) "session[" vcode "] is the Session value saved when generating the captcha"
Asp. The generation of login verification code and verification of input verification code in net