Asp.net (C #) to generate a verification code click to refresh

Source: Internet
Author: User
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>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.