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:
// 生成随机数字字符串
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);
For random numbers in C #, there's nothing to say about
System.Threading.Thread.Sleep (randomnum_first.next);
Random Randomnum_sencond = new Random ((int) DateTime.Now.Ticks);
//To create a darker color
int int_red = Randomnum_first.next (256) for display on a white background;
int int_green = Randomnum_sencond.next (256);
int int_blue = (int_red + int_green >)? 0:400-int_red-int_green;
Int_blue = (Int_blue > 255)? 255:int_blue;
Return Color.FromArgb (int_red, Int_green, Int_blue);
} generate final image based on validation string
public void CreateImage (string str_validatecode)
{
int int_imagewidth = str_validatecode.length * 13;< br> Random newrandom = new Random ();
//Tugau 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);
10pt font
Font thefont = new Font ("Arial");
for (int int_index = 0; Int_index < str_validatecode.length; int_index++)
{
String str_char = Str_valid Atecode.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 ();//Require output image information to modify HTTP headers
Response.ContentType = "Image/png";
Response.BinaryWrite (Ms. ToArray ());
Thegraphics.dispose ();
Thebitmap.dispose ();
Response.End ();
}