When we design the user login module, we often use verification codes, which can effectively prevent malicious cracking by hacker software. Now I will disclose the source code of my commonly used verification codes.
How to use:
1. Add a class to the Web project, such as "CreateImage.cs", and then copy the source code I published into it;
2. Create a new Web form, such as "Image.aspx", and add the code "CreateImage.DrawImage ();" to Page_Load. Of course, don't forget to add a reference to the class! !
3. Add the following javascript code to the appropriate position on the page (where you want to put the verification code) and it’s OK.
The source code is as follows:
///
///Verification code module///
public class CreateImage
{
public static void DrawImage()
{
CreateImage img=new CreateImage();
HttpContext.Current.Session["CheckCode"]=img.RndNum(4);
img.CreateImages(HttpContext.Current.Session["CheckCode"].ToString());
}
///
/// Generate verification image ///
/// Verify characters private void CreateImages(string checkCode)
{
int iwidth = (int)(checkCode.Length * 13);
System.Drawing.Bitmap image = new System.Drawing.Bitmap(iwidth, 23);
Graphics g = Graphics.FromImage(image);
g.Clear(Color.White);
//Define color Color[] c = {Color.Black,Color.Red,Color.DarkBlue,Color.Green,Color.Orange,Color.Brown,Color.DarkCyan,Color.Purple};
//Define font string[] font = {"Verdana","Microsoft Sans Serif","Comic Sans MS","Arial","宋体"};
Random rand = new Random();
//Randomly output noise points for(int i=0;i<50;i++)
{
int x = rand.Next(image.Width);
int y = rand.Next(image.Height);
g.DrawRectangle(new Pen(Color.LightGray, 0),x,y,1,1);
}
//Output verification code characters in different fonts and colors for(int i=0;i
{
int cindex = rand.Next(7);
int findex = rand.Next(5);
Font f = new System.Drawing.Font(font[findex], 10, System.Drawing.FontStyle.Bold);
Brush b = new System.Drawing.SolidBrush(c[cindex]);
int ii=4;
if((i+1)%2==0)
{
ii=2;
}
g.DrawString(checkCode.Substring(i,1), f, b, 3+(i*12), ii);
}
//Draw a border g.DrawRectangle(new Pen(Color.Black,0),0,0,image.Width-1,image.Height-1);
//Output to the browser System.IO.MemoryStream ms = new System.IO.MemoryStream();
image.Save(ms,System.Drawing.Imaging.ImageFormat.Jpeg);
HttpContext.Current.Response.ClearContent();
//Response.ClearContent();
HttpContext.Current.Response.ContentType = "image/Jpeg";
HttpContext.Current.Response.BinaryWrite(ms.ToArray());
g.Dispose();
image.Dispose();
}
///
/// Generate random letters ///
/// Number of generated letters /// string
private string RndNum(int VcodeNum)
{
string Vchar = "0,1,2,3,4,5,6,7,8,9";
string[] VcArray = Vchar.Split(',') ;
string VNum = "" ; // Since the string is very short, StringBuilder is not needed int temp = -1 ; // Record the last random value and try to avoid producing several identical random numbers // Use a simple algorithm to ensure Generate different random numbers Random rand =new Random();
for ( int i = 1 ; i < VcodeNum+1 ; i++ )
{
if (temp != -1)
{
rand =new Random(i*temp*unchecked((int)DateTime.Now.Ticks));
}
int t = rand.Next(VcArray.Length );
if (temp != -1 && temp == t)
{
return RndNum(VcodeNum);
}
temp = t;
VNum += VcArray[t];
}
return VNum;
}