Small and convenient MVC backend verification code for your reference.
Call:
Public ActionResult Vcode () // Verification Code
{
String code = ValidateCode. CreateRandomCode (4 );
ValidateCode. CreateImage (code );
Session ["vcode"] = code. ToLower (); // Save the session for verification
System. IO. MemoryStream MS = new System. IO. MemoryStream ();
Return File (ms. GetBuffer (), "image/JPEG ");
}
// The following is an internal topic class.
Using System;
Using System. Collections. Generic;
Using System. Drawing;
Using System. Drawing. Drawing2D;
Using System. Linq;
Using System. Web;
Namespace xxxxxx. Models // enter the namespace by yourself
{
Public class ValidateCode
{
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); // if it is A multiple of 3, divide it by 26 and convert the remainder (less than 26) into uppercase letters.
}
Else if (rand % 7 = 0)
{
Code = (char) ('A' + (char) (rand % 26 ));
}
Else
{
Code = (char) ('0' + (char) (rand % 10 ));
}
Randomcode + = code. ToString ();
}
Return randomcode;
}
Public static void CreateImage (string randomcode)
{
Int randAngle = 30; // random rotation angle range
Int mapwidth = (int) (randomcode. Length * 23); // The width of the background. This value is 23 times the Length of the character required by your input (4*23 = 92 width)
Using (Bitmap map = new Bitmap (mapwidth, 28) // create a canvas with a width of (4*28) and a height of 28, starting from (0, 0) in the upper right corner;
{
Random rand = new Random (); // Random number instance
Using (Bitmap temp = new Bitmap (map. Width, map. Height) // temporary canvas
{
Using (Graphics tempGra = Graphics. FromImage (temp ))
{
Char [] chars = randomcode. ToCharArray (); // split the string into a single character array
StringFormat format = new StringFormat (StringFormatFlags. NoClip); // define the text format
Format. Alignment = StringAlignment. Center; // text distance
Format. LineAlignment = StringAlignment. Center;
Color [] c = {Color. black, Color. red, Color. darkBlue, Color. green, Color. orange, Color. brown, Color. darkCyan, Color. purple}; // define the color
String [] fontList = {"Verdana", "Microsoft Sans Serif", "Comic Sans MS", "Arial", ""}; // define the font
For (int I = 0; I <chars. Length; I ++) // draw text one by one
{
Int cindex = rand. Next (7); // random color number
Int findex = rand. Next (5); // random font number
Font font = new System. Drawing. Font (fontList [findex], 13, System. Drawing. FontStyle. Bold); // Font style (parameter 2 indicates the font size)
Brush brush = new System. Drawing. SolidBrush (c [cindex]); // text color 1
// Brush brushPen = new LinearGradientBrush (new Rectangle (0, 0, temp. width, temp. height), Color. fromArgb (rand. next (0,256), 0, 0), Color. fromArgb (0, 0, rand. next (0,256), rand. next (90); // text color 2 gradient
Point dot = new Point (16, 14); // defines a Point
// 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
TempGra. TranslateTransform (dot. X, dot. Y); // change the origin of the coordinate system (all operations are performed at this origin (16, 16 ))
TempGra. RotateTransform (angle); // rotate the canvas
// Matrix m = new Matrix (); // create a transformation
// M. RotateAt (angle, new PointF (16, 16), MatrixOrder. Append); // rotate
// M. Shear (rand. Next (-10, 10) * 0.03f, 0); // twist
// TempGra. Transform = m; // canvas application Transformation
TempGra. DrawString (chars [I]. ToString (), font, brush, 1, 1, format); // start to draw letters or text
TempGra. RotateTransform (-angle); // go back
TempGra. TranslateTransform (4,-dot. Y); // you can change the origin of a coordinate system.
}
}
Using (Graphics graph = Graphics. FromImage (map) // create the target canvas;
{
Graph. Clear (Color. AliceBlue); // Clear the screen and fill in the background.
Rectangle rect = new Rectangle (0, 0, map. Width, map. Height); // draw the gradient background
Brush brushBack = new LinearGradientBrush (rect, Color. fromArgb (rand. next (150,256), 255,255), Color. fromArgb (255, rand. next (150,256), 255), rand. next (90); // gradient background color
Graph. FillRectangle (brushBack, rect); // fill the background in the canvas.
Graph. drawRectangle (new Pen (Color. lightPink, 0), 0, 0, map. width-1, map. height-1); // draw a border, which is first drawn from the upper right corner (0, 0) of the screen by default.
// Graph. SmoothingMode = System. Drawing. Drawing2D. SmoothingMode. AntiAlias; // image anti-aliasing Mode
Pen blackPen = new Pen (Color. LightGray, 0); // stroke Color
For (int I = 0; I <50; I ++) // 50 noise points
{
Int x = rand. Next (1, map. Width-3 );
Int y = rand. Next (1, map. Height-3 );
Graph. DrawRectangle (blackPen, x, y, 1, 1); // generates noise.
}
Graph. DrawImage (temp, new Point (0, 0); // draw a picture after drawing
Pen pen = new Pen (Color. Gray, 1); // interfere with line strokes
For (int I = 0; I <2; I ++) // draw two interference lines
{
Point p1 = new Point (0, rand. Next (map. Height ));
Point p2 = new Point (rand. Next (map. Width), rand. Next (map. Height ));
Point p3 = new Point (rand. Next (map. Width), rand. Next (map. Height ));
Point p4 = new Point (map. Width, rand. Next (map. Height ));
Point [] p = {p1, p2, p3, p4 };
Graph. DrawBeziers (pen, p );
}
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 ());
Temp. Dispose (); // release
Graph. Dispose ();
Map. Dispose ();
}
}
}
}
}
}