Asp.net| Graphics | Verification Code | page
Project needs, to the homepage login interface to add a graphics verification code, fashion bar, online a search, especially many, find a few, are not very satisfied. The main problem is that most of the code generated image width is not unique, the page layout is not easy to control, followed by a single color, some are too abstract, not careful to see easily mistaken. For a specific customer, I only need "picture" long and wide fixed, color and diverse digital graphics verification code, for reference to the existing code on the Internet, their own fencing completed, the following is the effect of the map:
The principle is not complicated, that is, the Web page as a canvas, using a variety of brushes, in a specific area drawing out the number, and then in a specific format (in this case, PNG format) to send back to the client, in IE as "picture", the string used for validation is stored in session.
The main code is as follows:
Generate a random number string
public string getrandomnumberstring (int int_numberlength)
{
String str_number = String. Empty;
Random therandomnumber = new Random ();
for (int int_index = 0; Int_index < int_numberlength; int_index++)
Str_number + = Therandomnumber.next (10). ToString ();
return str_number;
}
Generate Random Colors
Public Color Getrandomcolor ()
{
Random Randomnum_first = new Random ((int) DateTime.Now.Ticks);
There's nothing to say about random numbers in C #
System.Threading.Thread.Sleep (Randomnum_first.next (50));
Random Randomnum_sencond = new Random ((int) DateTime.Now.Ticks);
In order to be displayed on a white background, try to produce dark
int int_red = Randomnum_first.next (256);
int int_green = Randomnum_sencond.next (256);
int int_blue = (int_red + int_green > 400)? 0:400-int_red-int_green;
Int_blue = (Int_blue > 255)? 255:int_blue;
Return Color.FromArgb (int_red, Int_green, Int_blue);
}
Generate a final image from a validation string
public void CreateImage (string str_validatecode)
{
int int_imagewidth = Str_ Validatecode.length * 13;
Random newrandom = new Random ();
// 20px
Bitmap thebitmap = new Bitmap (int_imagewidth);
Graphics thegraphics = Graphics.fromimage (Thebitmap);
// white background
thegraphics.clear (color.white);
// Gray Border
thegraphics.drawrectangle (New Pen (Color.lightgray, 1), 0, 0, Int_imagewidth-1, 19);
// 10pt font
font thefont = new Font ("Arial") );
for (int int_index = 0; Int_index < str_validatecode.length; int_index++)
{
String Str_char = Str_validatecode.substring (Int_index, 1);
Brush Newbrush = new SolidBrush (Getrandomcolor ());
Point thepos = new Point (Int_index * + 1 + newrandom.next (3), 1 + newrandom.next (3));
Thegraphics.drawstring (Str_char, Thefont, Newbrush, Thepos);
}
Send the generated picture back to the client
MemoryStream ms = new MemoryStream ();
Thebitmap.save (MS, Imageformat.png);
Response.clearcontent (); Need to output image information to modify HTTP headers
Response.ContentType = "Image/png";
Response.BinaryWrite (Ms. ToArray ());
Thegraphics.dispose ();
Thebitmap.dispose ();
Response.End ();
}
Finally, call the above code in Page_Load
private void Page_Load (object sender, System.EventArgs e)
{
if (! IsPostBack)
{
4-digit Verification Code
String str_validatecode = getrandomnumberstring (4);
Session for validation
session["Validatecode"] = Str_validatecode;
CreateImage (Str_validatecode);
}
}
When used, add an image to the page and change the picture path to validatecode.aspx relative path
Fill in the following code where validation is required:
if (TextBox1.Text = = session["Validatecode"). ToString ())
{
TextBox1.Text = "Right!";
}
Else
TextBox1.Text = "wrong!";
OK, basically finish, sum up:
Advantages:
1. Simple and clear, suitable for simple application
2. Interface friendly, picture long wide format fixed
Disadvantages:
1. If more than one page needs this captcha, it will cause session to be overridden by other pages, you can consider specifying a specific session value for the parity value
2. Only numbers are supported for the time being, but changing the code in Getrandomnumberstring () can implement the random string of the specified word Fu
3. The Verification Code changes after page refresh
Welcome to the Bo Friends commentary