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: Randomly generate set verification code, and randomly rotate 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 certain length of verification code
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 code picture
</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);//Create picture background
Graphics graph = graphics.fromimage (map);
Graph. Clear (Color.aliceblue);//erase screen, fill 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 ();
Background noise generation Www.jb51.net
Pen Blackpen = new Pen (color.lightgray, 0);
for (int i = 0; i < i++)
{
int x = rand. Next (0, map. Width);
int y = rand. Next (0, map. Height);
Graph. DrawRectangle (Blackpen, x, Y, 1, 1);
}
Verification code rotation to prevent machine recognition
char[] chars = Randomcode. ToCharArray ();//split string into single character array
In text spacing
StringFormat format = new StringFormat (stringformatflags.noclip);
Format. Alignment = Stringalignment.center;
Format. LineAlignment = Stringalignment.center;
Define Color
Color[] C = {color.black, color.red, Color.darkblue, Color.green, Color.orange, Color.brown, Color.darkcyan, COLOR.PURPL e};
Defining fonts
string[] Font = {"Verdana", "Microsoft Sans Serif", "Comic Sans MS", "Arial", "Song Body"};
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], System.Drawing.FontStyle.Bold);//font style (parameter 2 is 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 x-coordinate display spacing
float angle = rand. Next (-randangle, randangle);/the degree 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)/Turn 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 pictures
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 ();
}
}
}