I just got started with the blog, and I will first share some posts with you.
-
- Using system;
-
- Using system. collections;
-
- Using system. componentmodel;
-
- Using system. Data;
-
- Using system. drawing;
-
- Using system. Web;
-
- Using system. Web. sessionstate;
-
- Using system. Web. UI;
-
- Using system. Web. UI. webcontrols;
-
- Using system. Web. UI. htmlcontrols;
-
- Namespace webapplication1. Verification Code
-
- {
-
- /// <Summary>
-
- /// Perfect Random verification code 0.10
-
- /// Verion: 0.10
- /// Description: The verification code is randomly generated and rotated to a certain angle. The font color is different.
-
- /// </Summary>
-
- Public class validatecode: system. Web. UI. Page
-
- {
-
- Private void page_load (Object sender, system. eventargs E)
-
- {
-
- String randomcode = This. createrandomcode (4 );
-
- Session ["validatecode"] = randomcode;
-
- // Viewstate ["validatecode"] = randomcode;
-
- This. createimage (randomcode );
-
- }
-
- /// <Summary>
-
- /// Generate random code
- /// </Summary>
-
- /// <Param name = "length"> Number of random codes </param>
-
- /// <Returns> </returns>
-
- Private string createrandomcode (INT length)
-
- {
-
- Int rand;
-
- Char code;
-
- String randomcode = string. empty;
-
- // Generate a verification code of a certain length
-
- System. Random random = new random ();
-
- For (INT I = 0; I <length; I ++)
-
- {
-
- Rand = random. Next ();
-
- If (RAND % 3 = 0)
-
- {
- Code = (char) ('A' + (char) (RAND % 26 ));
-
- }
-
- Else
-
- {
-
- Code = (char) ('0' + (char) (RAND % 10 ));
-
- }
-
- Randomcode + = code. tostring ();
-
- }
-
- Return randomcode;
-
- }
-
- /// <Summary>
-
- /// Create a random Image
-
- /// </Summary>
-
- /// <Param name = "randomcode"> Random Code </param>
-
- Private void createimage (string randomcode)
-
- {
- Int randangle = 45; // random Rotation Angle
-
- Int mapwidth = (INT) (randomcode. length * 23 );
-
- Bitmap map = new Bitmap (mapwidth, 28); // creates an image background.
-
- Graphics graph = graphics. fromimage (MAP );
-
- Graph. Clear (color. aliceblue); // clear the screen and fill in the background.
-
- Graph. drawrectangle (new pen (color. Black, 0), 0, 0, map. Width-1, map. Height-1); // draw a border
-
- // Graph. smoothingmode = system. Drawing. drawing2d. smoothingmode. antialias; // Mode
-
- Random Rand = new random ();
-
- // Generate background noise
-
- Pen blackpen = new pen (color. lightgray, 0 );
-
- For (INT I = 0; I <50; I ++)
- {
-
- Int x = Rand. Next (0, map. width );
-
- Int y = Rand. Next (0, map. Height );
-
- Graph. drawrectangle (blackpen, X, Y, 1, 1 );
-
- }
-
- // Rotate the verification code to prevent Machine recognition
-
- Char [] chars = randomcode. tochararray (); // split the string into a single character array
-
- // Text distance
-
- Stringformat format = new stringformat (stringformatflags. noclip );
-
- Format. Alignment = stringalignment. Center;
-
- Format. linealignment = stringalignment. Center;
-
- // Define the color
- Color [] C = {color. Black, color. Red, color. darkblue, color. Green, color. Orange, color. Brown, color. darkcyan, color. Purple };
-
- // Define the font
-
- String [] font = {"verdana", "Microsoft sans serif", "Comic Sans MS", "Arial", ""};
-
- For (INT I = 0; I <chars. length; I ++)
-
- {
-
- Int cIndex = Rand. Next (7 );
-
- Int findex = Rand. Next (5 );
-
- Font F = new system. Drawing. Font (font [findex], 13, system. Drawing. fontstyle. Bold); // font style (parameter 2 indicates the font size)
-
- Brush B = new system. Drawing. solidbrush (C [cIndex]);
- Point dot = new point (16, 16 );
-
- // Graph. drawstring (Dot. X. tostring (), fontstyle, new solidbrush (color. Black), 10,150); // test the display spacing of X coordinates.
-
- Float angle = Rand. Next (-randangle, randangle); // degrees of Rotation
-
- Graph. translatetransform (Dot. X, Dot. Y); // move the cursor to the specified position
-
- Graph. rotatetransform (angle );
-
- Graph. drawstring (chars [I]. tostring (), F, B, 1, 1, format );
-
- // Graph. drawstring (chars [I]. tostring (), fontstyle, new solidbrush (color. Blue), 1, 1, format );
-
- Graph. rotatetransform (-angle); // go back
-
- Graph. translatetransform (2,-dot. Y); // move the cursor to the specified position
-
- }
- // Graph. drawstring (randomcode, fontstyle, new solidbrush (color. Blue), 2, 2); // standard random code
-
- // Generate an image
-
- System. Io. memorystream MS = new system. Io. memorystream ();
-
- Map. Save (MS, system. Drawing. imaging. imageformat. GIF );
-
- Response. clearcontent ();
-
- Response. contenttype = "image/GIF ";
-
- Response. binarywrite (Ms. toarray ());
-
- Graph. Dispose ();
-
- Map. Dispose ();
-
- }
-
- # Generated by region web Form DesignerCode
-
- Override protected void oninit (eventargs E)
-
- {
-
- //
- // Codegen: This call is required by the ASP. NET web form designer.
-
- //
-
- Initializecomponent ();
-
- Base. oninit (E );
-
- }
-
- /// <Summary>
-
- /// The designer supports the required methods-do not use the code editor to modify
-
- /// Content of this method.
-
- /// </Summary>
-
- Private void initializecomponent ()
-
- {
-
- This. Load + = new system. eventhandler (this. page_load );
-
- }
-
- # Endregion
-
- }
-
- }