First, create a webpage checkcode. aspx that generates the verification code:
The checkcode. aspx. CS code is as follows:
Add reference:
Using system. IO;
Using system. drawing;
Using system. Drawing. imaging;
Using system. Drawing. drawing2d;
Public partial class default2: system. Web. UI. Page
{
Private bitmap validateimage;
Private graphics g;
Protected void page_load (Object sender, eventargs E)
{
Response. bufferoutput = true; // note
Response. cache. setexpires (datetime. Now. addmilliseconds (-1); // note
Response. cache. setcacheability (httpcacheability. nocache); // note
Response. appendheader ("Pragma", "No-Cache"); // note
String vnum = makevalidatecode ();
Session ["vnum"] = vnum; // obtain the verification code for later verification
Validatecode (vnum );
}
Public void validatecode (string vnum)
{
Validateimage = new Bitmap (60, 20, pixelformat. format24bpprgb );
G = graphics. fromimage (validateimage );
G. fillrectangle (New lineargradientbrush (new point (0, 0), new point (110, 20), color. fromargb (1, 240,255,255,255), color. fromargb (240,255,255,255), 0, 0,200,200); // rectangular frame
G. drawstring (vnum, new font ("Arial", 11), new solidbrush (color. Red), new pointf (6, 0); // Font/color
G. Save ();
Memorystream MS = new memorystream ();
Validateimage. Save (MS, system. Drawing. imaging. imageformat. GIF );
Response. clearcontent ();
Response. C;
Response. binarywrite (Ms. toarray ());
Response. End ();
}
String makevalidatecode ()
{
Char [] S = new char [] {'0', '1', '2', '3', '4', '5', '6 ', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F', 'G ', 'H', 'I', 'J', 'k', 'l', 'M', 'n', 'O', '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', 'I, s', 't', 'U', 'V ', 'w', 'x', 'y', 'z'}; // enumerated Array
String num = "";
Random r = new random ();
For (INT I = 0; I <5; I ++)
{
Num + = s [R. Next (0, S. Length)]. tostring ();
}
Return num;
}
}
Then, add to the HTML of the page that displays the verification code.
Add the following Javascript script language to the line before <SCRIPT type = "text/JavaScript">
Function reloadcode (){
Document. getelementbyid ("YZM"). src = "checkcode. aspx ";
}
</SCRIPT>