Then in the project, in a new verifycode.aspx, the following is the code for ASPX:
Using System;
Using System.Collections.Generic;
Using System.Linq;
Using System.Web;
Using System.Web.UI;
Using System.Web.UI.WebControls;
Using System.Drawing;
namespace Form {public partial class VerifyCode:System.Web.UI.Page {public static string HZ;
<summary> the maximum length of the///verification code///</summary> public int MaxLength {get {return 10;} The minimum length of the///<summary>///Verification Code///</summary> public int MinLength {get {1;
} protected void Page_Load (object sender, EventArgs e) {string[] str = createvalidatenumber (4); String strcode = String.
Empty; for (int i = 0; i < str. Length;
i++) {Strcode + = Str[i];
} createcheckcodeimage (str);
HZ = Strcode;
Response.Write (HZ);
The verification code is stored in the session session["Validatecode"] = HZ; ///<summary>///generate Authenticode///</summary>///<param name= "Length" ≫ Specify the length of the authentication code </param>///<returns> Authenticode </returns> public string[] Createvalidatenumber (int length) {String Vchar = "1,2,3,4,5,6,7,8,9,a,b,c,d,e,f,g,h,i,j,k,l,m,n,p" + ", Q,r,s,t,u,v,w,x,y,z,a,b,c,d,e,f,g,
H,i,j,k,l,m,n,p,q "+", r,s,t,u,v,w,x,y,z ";
string[] Vcarray = Vchar.split (new char[] {', '});//split into array string[] num = new String[length];
int temp = -1;//records the last random number, avoiding the production of a few of the same random numbers Random rand = new Random ();
A simple algorithm is used to ensure that the random number is generated for a different for (int i = 1; i < length + 1; i++) {if (temp!=-1) {
Rand = new Random (i * temp * unchecked ((int) DateTime.Now.Ticks)); int t = rand.
Next (vcarray.length-1);
if (temp!=-1 && temp = = t) {return createvalidatenumber (length);
temp = t;
NUM[I-1] = vcarray[t]; Num.
SetValue (Vcarray[t]);
Vnum + = vcarray[t];
return num; } private void Createcheckcodeimage (string[] checkcode) {if (Checkcode = = NULL | | checkcode.length <=
0) return;
System.Drawing.Bitmap image = new System.Drawing.Bitmap ((int) math.ceiling (checkcode.length * 32.5)), 60;
System.Drawing.Graphics g = graphics.fromimage (image);
try {//Generate a random generator Random Random = new Random ();
Clear the picture background color g.clear (color.white); Define color color[] c = {color.black, color.red, Color.darkblue, Color.green, Color.orange, Color.brown, Color.darkcy
AN, color.purple}; The background noise line for the picture is for (int i = 0; i < i++) {int cindex = random.
Next (7); int findex = random.
Next (5); int x1 = Random. Next (image.
Width); int x2 = random. Next (image.
Width); int y1 = random. Next (image.
Height); int y2 = random. Next (image.
Height);
G.drawline (New Pen (C[cindex)), x1, y1, x2, y2);
}Define font string[] f = {"Verdana", "Microsoft Sans Serif", "Comic Sans MS", "Arial", "Song Body"}; for (int k = 0; k <= checkcode.length-1 k++) {int cindex = random.
Next (7); int findex = random.
Next (5);
Font drawfont = new Font (F[findex], num, (System.Drawing.FontStyle.Bold));
SolidBrush Drawbrush = new SolidBrush (C[cindex]);
float x = 5.0F;
Float y = 0.0F;
float width = 42.0F;
float height = 48.0F; int SJX = random.
Next (10); int sjy = random. Next (image.
Height-(int) height);
RectangleF drawrect = new RectangleF (x + SJX + (k), Y + sjy, width, height);
StringFormat Drawformat = new StringFormat ();
Drawformat.alignment = Stringalignment.center;
g.DrawString (Checkcode[k], Drawfont, Drawbrush, DrawRect, Drawformat); ///Picture foreground noise point for (int i = 0; i < i++) {int x = random. Next (iMage.
Width); int y = random. Next (image.
Height); Image. SetPixel (x, Y, Color.FromArgb) (random.
Next ())); int cindex1 = random.
Next (7); Draw a picture of the border line G.drawrectangle (new Pen (C[CINDEX1)), 0, 0, image. Width-1, image.
HEIGHT-1);
System.IO.MemoryStream ms = new System.IO.MemoryStream (); Image.
Save (MS, SYSTEM.DRAWING.IMAGING.IMAGEFORMAT.GIF);
Response.clearcontent ();
Response.ContentType = "Image/gif"; Response.BinaryWrite (Ms.
ToArray ());
finally {g.dispose (); Image.
Dispose ();
}
}
}
}
So! You can generate the verification code, and then you write the verification version of the logic of the judgement can be written!
The above is the entire content of this article, I hope to help you learn, but also hope that we support the cloud habitat community.