The graphic verification code is generated (numbers and English uppercase and lowercase) and submitted for verification. The verification code is case-sensitive.
When I first came into contact, I felt very fun. Then I studied it myself and made a demo. Then I sorted it out. I can use it next time. It is case-sensitive and I can click it to switch.
On the code ....
Page code:
This is the method for changing JQuery clicks on the page.
$ ("# Img"). click (function (){
// This. src = "/Login/GetCheckCode? Time = "+ (new Date (). getTime (); // This is also possible
// $ ("# Img"). attr ("src", "/Login/GetCheckCode? "+ Math. random ());
$ ("# Img"). attr ("src", "/Login/GetCheckCode? Time = "+ (new Date (). getTime ());
Note:Src is followed by a time parameter to re-load the verification code. Otherwise, the Verification Code cannot be loaded.
Explanation on the Internet: the server has compiled the verification code generation method. Because the client browser caches resources with the same URL, you can use a random number/or time to re-request:
});
Background code:
Public ActionResult GetCheckCode ()
{
String code = GenerateCheckCodes (4 );
Session ["ValidateCode"] = code;
Byte [] bytes = CreateCheckCodeImage (code );
Return File (bytes, @ "image/jpeg ");
}
Private string GenerateCheckCodes (int iCount)
{
Char [] oCharacter = {'0', '1', '2', '3', '4', '5', '6', '7 ', '8', '9 ',
'A', 'B', 'C', 'D', 'E', 'E', 'F', 'G', 'h', 'I', 'J ', 'k', 'l', 'M', 'n', 'O', 'P', 'Q', 'R', 's','t ', 'U', 'V', 'w', 'x', 'y', 'Z'
// 'A', 'B', 'C', 'D', 'E', 'E', 'F', 'G', 'h', 'I ', 'J', 'k', 'l', 'M', 'n', 'O', 'P', 'Q', 'R','s ', 'T', 'U', 'V', 'w', 'x', 'y', 'Z'
};
// Int number;
String checkCode = String. Empty;
Int iSeed = DateTime. Now. Millisecond;
System. Random random = new Random (iSeed );
For (int I = 0; I <iCount; I ++)
{
CheckCode + = oCharacter [random. Next (oCharacter. Length)];
// Pure number
// Number = random. Next (10 );
// Number = oCharacter [random. Next (oCharacter. Length)];
// CheckCode + = number. ToString ();
}
Return checkCode;
}
Private byte [] CreateCheckCodeImage (string checkCode)
{
If (checkCode = null | checkCode. Trim () = String. Empty)
{
Return null;
}
Int iWordWidth = 20;
Int iImageWidth = checkCode. Length * iWordWidth;
Bitmap image = new Bitmap (iImageWidth, 30 );
Graphics g = Graphics. FromImage (image );
Try
{
// Generate a random Generator
Random random = new Random ();
// Clear the background color of the image
G. Clear (Color. White );
// Specifies the background noise of the image.
For (int I = 0; I <20; I ++)
{
Int x1 = random. Next (image. Width );
Int x2 = random. Next (image. Width );
Int y1 = random. Next (image. Height );
Int y2 = random. Next (image. Height );
G. DrawLine (new Pen (Color. Silver), x1, y1, x2, y2 );
}
// Draw the background noise line of the image
For (int I = 0; I <2; I ++)
{
Int x1 = 0;
Int x2 = image. Width;
Int y1 = random. Next (image. Height );
Int y2 = random. Next (image. Height );
If (I = 0)
{
G. DrawLine (new Pen (Color. Gray, 2), x1, y1, x2, y2 );
}
}
For (int I = 0; I <checkCode. Length; I ++)
{
String Code = checkCode [I]. ToString ();
Int xLeft = iWordWidth * (I );
Random = new Random (xLeft );
Int iSeed = DateTime. Now. Millisecond;
Int iValue = random. Next (iSeed) % 4;
If (iValue = 0)
{
Font font = new Font ("Arial", 16, (FontStyle. Bold | System. Drawing. FontStyle. Italic ));
Rectangle rc = new Rectangle (xLeft, 0, iWordWidth, image. Height );
LinearGradientBrush brush = new LinearGradientBrush (rc, Color. Blue, Color. Red, 1.5f, true );
G. DrawString (Code, font, brush, xLeft, 2 );
}
Else if (iValue = 1)
{
Font font = new System. Drawing. Font ("", 16, (FontStyle. Bold ));
Rectangle rc = new Rectangle (xLeft, 0, iWordWidth, image. Height );
LinearGradientBrush brush = new LinearGradientBrush (rc, Color. Blue, Color. DarkRed, 1.3f, true );
G. DrawString (Code, font, brush, xLeft, 2 );
}
Else if (iValue = 2)
{
Font font = new System. Drawing. Font ("", 16, (System. Drawing. FontStyle. Bold ));
Rectangle rc = new Rectangle (xLeft, 0, iWordWidth, image. Height );
LinearGradientBrush brush = new LinearGradientBrush (rc, Color. Green, Color. Blue, 1.2f, true );
G. DrawString (Code, font, brush, xLeft, 2 );
}
Else if (iValue = 3)
{
Font font = new System. Drawing. Font ("", 16, (System. Drawing. FontStyle. Bold |
System. Drawing. FontStyle. Bold ));
Rectangle rc = new Rectangle (xLeft, 0, iWordWidth, image. Height );
LinearGradientBrush brush = new LinearGradientBrush (rc, Color. Blue, Color. Green, 1.8f, true );
G. DrawString (Code, font, brush, xLeft, 2 );
}
}
//// Foreground noise of the image. The Code does not seem to have changed.
For (int I = 0; I <8; I ++)
{
Int x = random. Next (image. Width );
Int y = random. Next (image. Height );
Image. SetPixel (x, y, Color. FromArgb (random. Next ()));
}
// Draw the border line of the image
G. DrawRectangle (new Pen (Color. Silver), 0, 0, image. Width-1, image. Height-1 );
MemoryStream MS = new MemoryStream ();
Image. Save (MS, System. Drawing. Imaging. ImageFormat. Jpeg );
Return ms. ToArray ();
// Response. ClearContent ();
// Response. ContentType = "image/jpeg ";
// Response. BinaryWrite (ms. ToArray ());
}
Finally
{
G. Dispose ();
Image. Dispose ();
}
}
Well, it's here. If you have any questions or are incorrect, you can correct them. I will check them once a day. Thank you for your help.
It is too difficult to format the code. Each time a line of tab key is required.