The first is to create a new verification code page validatecode.aspx
Defining variables so that they are useful for later modification
private int codelen = 4;//captcha length private int fineness = 85;//picture sharpness private int imgwidth = 48;//picture width private int imgheight = 24;//picture Height private string fontFamily = "Times New Roman";//font name private int fontSize = 14;//font size 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 V Alidatecode = ""; Random random = new random (), or//stochastic object for (int i = 0; i < Codelen; i++)//loop generates each bit value {int n = random. Next (10);//digital Validatecode + = n.tostring (); } session["Vcode"] = validatecode;//Save Verification Code return validatecode;//back verification code} private void Disturbbitmap (Bitmap Bitmap)//image Background { Random random = new random ();//generate for (int i = 0; i < bitmap) by randomly number. Width; i++)//Generate {for (int j = 0; J < bitmap) per pixel by loop nesting. Height; J + +) {if (random. Next (<= this.fineness) bitmap. SetPixel (I,j,color.lightgray); }}} private void Drewvalidatecode (Bitmap bitmap,string validatecode)//Draw Verification code image {Graphics g = graphics.fromimage (Bitmap); //Gets the drawing object font font = new Font (fontfamily,fontsize,fontstyle.bold);//sets the drawing font g.drawstring (Validatecode,font, Brushes.black,posx,posy);//Draw Captcha Image}
The last is to call the
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 Verification code image bitmap. Save (response.outputstream,imageformat.gif);//preserve image, wait for output}
Validatecode.aspx Page Completed
The rest is simple. Create a new page
<asp:image id= "Image1" runat= "Server" height= "21px" width= "61px" imageurl= "~/default2.aspx" imagealign= "Middle"/ >
Post-run effects
When committing, the value in the text box is compared with session["vcode"] = validatecode;//to save the verification code to determine if the input is correct.