ASP. NET text Verification Code
Although I think the image verification code is useless and annoying (the verification code is frequently entered on every mobile website), people have to get one.
Generate the image verification code page createimg. aspx, and verify the page default. aspx.
The createimg. ASPX page uses the following functions:
String getrandomvalidate (INT Len) returns a string with a random length of Len.
Drawline (Graphics GFC, bitmap IMG) draws the bottom line in the picture
Drawpoint)
Getimagevalidate (string strvalue) uses the string returned by the getrandomvalidate function to generate an image
ImplementationCodeAs follows:
Using system. drawing;
Using system. IO;
Public partial class createimg: system. Web. UI. Page
{
Random ran = new random ();
Protected void page_load (Object sender, eventargs E)
{
String STR = getrandomvalidate (4 );
Session ["check"] = STR; // This step is used to write the verification code to the session for verification. It cannot be the default value or use the cookie.
Getimagevalidate (STR );
}
// Obtain a random string with its own length defined
Private string getrandomvalidate (INT Len)
{
Int num;
Int TEM;
String rtustr = "";
For (INT I = 0; I <Len; I ++)
{
Num = ran. Next ();
/*
* You can select the verification code that generates a combination of characters and numbers.
*/
TEM = num % 10 + '0'; // generate a number
// TEM = num % 26 + 'a'; // generate characters
Rtustr + = convert. tochar (TEM). tostring ();
}
Return rtustr;
}
// Generate an image
Private void getimagevalidate (string strvalue)
{
// String STR = "oo00"; // the first two are letters O, and the last two are digits 0
Int width = convert. toint32 (strvalue. length * 12); // calculate the image width.
Bitmap IMG = new Bitmap (width, 23 );
Graphics GFC = graphics. fromimage (IMG); // generates a graphics object for drawing.
GFC. Clear (color. White );
Drawline (GFC, IMG );
// Write the verification code. You need to define the font.
Font font = new font ("Arial", 12, fontstyle. Bold );
System. Drawing. drawing2d. lineargradientbrush brush =
New system. Drawing. drawing2d. lineargradientbrush (New rectangle (0, 0, IMG. Width, IMG. Height), color. darkorchid, color. Blue, 1.5f, true );
GFC. drawstring (strvalue, Font, brush, 3, 2 );
Drawpoint (IMG );
GFC. drawrectangle (new pen (color. darkblue), 0, 0, IMG. Width-1, IMG. Height-1 );
// Add the image to the page
Memorystream MS = new memorystream ();
IMG. Save (MS, system. Drawing. imaging. imageformat. GIF );
// Change the HTTP Header
Response. clearcontent ();
Response. contenttype = "image/GIF ";
Response. binarywrite (Ms. toarray ());
// Dispose
GFC. Dispose ();
IMG. Dispose ();
Response. End ();
}
Private void drawline (Graphics GFC, bitmap IMG)
{
// Select to draw 10 lines. You can also add or remove lines. You only need to randomly add random points.
For (INT I = 0; I <10; I ++)
{
Int X1 = ran. Next (IMG. width );
Int Y1 = ran. Next (IMG. Height );
Int X2 = ran. Next (IMG. width );
Int y2 = ran. Next (IMG. Height );
GFC. drawline (new pen (color. Silver), X1, Y1, X2, Y2); // note that the paint brush must be light colored; otherwise, the Verification Code cannot be clearly viewed.
}
}
Private void drawpoint (Bitmap IMG)
{
/*
// Select to draw 100 points, which can be changed according to the actual situation
For (INT I = 0; I <100; I ++)
{
Int x = ran. Next (IMG. width );
Int y = ran. Next (IMG. Height );
IMG. setpixel (X, Y, color. fromargb (ran. Next (); // random noise color
}
*/
Int Col = ran. Next (); // the color of a grocery store is the same in one image.
For (INT I = 0; I <100; I ++)
{
Int x = ran. Next (IMG. width );
Int y = ran. Next (IMG. Height );
IMG. setpixel (X, Y, color. fromargb (COL ));
}
}
}
How to use:
To create a page, you must have at least one text box, image control, and one button control. You must specify the imageurl of the image control as createimg. aspx, as shown below:
<Asp: Image id = "image1" runat = "server"Imageurl = "~ /Createimg. aspx"/>
The test is very simple, just like using the session at ordinary times, ELE. Me. Do you still remember a session ["check"] On the createimg page? This page has already registered a session, so you only need to simply compare the following in the default page.
Protected void page_load (Object sender, eventargs E)
{
If (session ["check"] = NULL)
Message. Text = "sorry, the image is wrong! ";
}
Protected void checkbutton_click (Object sender, eventargs E)
{
If (check. Text. tostring () = session ["check"]. tostring ())
Message. Text = "yes, you pass it ";
Else
Message. Text = "I'm sorry! Try again ...";
}
Shows the test result:
Logistics Distribution Network http://wlphw.com/QQ online: 471226865