Front-end
Copy codeThe Code is as follows:
Height = "25"/>
<Span
Style = "cursor: pointer; text-decoration: underline"> change one </span>
Controller
Copy codeThe Code is as follows:
Using System;
Using System. Collections. Generic;
Using System. Linq;
Using System. Web;
Using System. Web. Mvc;
Using Utility;
Using Jellal **;
Namespace sjlwebsite. Controllers
{
Public class CommonController: Controller
{
# Region Verification Code
[OutputCache (Duration = 0)]
Public ActionResult VCode ()
{
String code = ValidateCode. CreateRandomCode (4 );
Session ["vcode"] = code;
ValidateCode. CreateImage (code );
Return View ();
}
Public string GetCode ()
{
Return Session ["vcode"]. ToStr ();
}
# Endregion
}
}
Verification Code
Copy codeThe Code is as follows:
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 Utility
{
/// <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
{
/// <Summary>
/// Generate random code
/// </Summary>
/// <Param name = "length"> Number of random codes: www.52mvc.com </param>
/// <Returns> </returns>
Public static 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>
Public static 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 www.jb51.net based on 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. ToString (), f, B, 1, 1, format );
// Graph. DrawString (chars. 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 );
HttpContext. Current. Response. ClearContent ();
HttpContext. Current. Response. ContentType = "image/gif ";
HttpContext. Current. Response. BinaryWrite (ms. ToArray ());
Graph. Dispose ();
Map. Dispose ();
}
}
}