1. Create a chkcode. aspx and chkcode. aspx. CS Code as follows:
- Using system;
- Using system. drawing;
- Using system. Drawing. imaging;
- Using system. IO;
- Public partial class chkcode: system. Web. UI. Page
- {
- Protected void page_load (Object sender, eventargs E)
- {
- Showimg ();
- }
- Private void showimg ()
- {
- Random ran = new random ();
- Int intrandom = ran. Next (1001,999 9 );
- // Write a random number (Verification string) to the session
- Session. removeall ();
- Session ["chkcode"] = intrandom;
- // Font name
- String strfontname = "Arial ";
- // Font size
- Int intfontsize = 9;
- // Image Width
- Int intwidth = 35;
- // Image Length
- Int intheight = 18;
- // Background color
- Color bgcolor = colortranslator. fromhtml ("#" + request. querystring ["colorb"]);
- // Foreground color
- Color forecolor = colortranslator. fromhtml ("#" + request. querystring ["colgrading"]);
- // Generate a font
- Font forfont = new font (strfontname, intfontsize, fontstyle. Bold );
- // Generate an image
- Bitmap newbitmap = new Bitmap (intwidth, intheight, pixelformat. format32bppargb );
- Graphics G = graphics. fromimage (newbitmap );
- // Define the same size of a square box as the word slice
- Rectangle newrect = new rectangle (0, 0, intwidth, intheight );
- // Apply the background color
- G. fillrectangle (New solidbrush (bgcolor), newrect );
- // Write
- G. drawstring (intrandom. tostring (), forfont, new solidbrush (forecolor), 2, 2 );
- Memorystream mstream = new memorystream ();
- // Save to memorystream
- Newbitmap. Save (mstream, imageformat. GIF );
- G. Dispose ();
- Newbitmap. Dispose ();
- // Send
- Response. clearcontent ();
- Response. contenttype = "image/GIF ";
- Response. binarywrite (mstream. toarray ());
- Response. End ();
- }
- }
2. Call Method
- <Asp: Image id = "image1" runat = "server" imageurl = "chkcode. aspx? Colgrading = ff0000 & colorb = efb3ff "/>