I have made several websites, but in the development process, I usually write the business logic, and I seldom talk about this. Today, it took several minutes to help a friend draw a verification code. In fact, there are many examples on the Internet. In the process of website development, the verification code technology is the most basic link to protect website security. It can prevent illegal personnel from using registration to attack the website by using public transit or login tools. I remember that a few months ago, I studied Image Recognition and OCR recognition with a few friends. It was mainly used to identify and automatically register these verification codes on the Internet. Of course, automatic identification is much more difficult than this. It is still being studied.
(1) GenerateCode method: Mainly used to generate random strings.
/// <Summary>
/// Randomly generate a string
/// </Summary>
/// <Param name = "number"> String Length </param>
/// <Returns> </returns>
Protected string GenerateCode (int number)
{
String checkCode = string. Empty;
String vChar = ", a, B, c, d, 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, F, G, h, I, J, K, L, M, N, O, P, Q, R, S, T, U, V, W, X, Y, Z ";
String [] vcArray = vChar. Split (,);
Int temp =-1; // record the last random value, and try to avoid several identical random numbers.
Random rand = new Random ();
For (int I = 1; I <number + 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 GenerateCode (number );
}
Temp = t;
CheckCode + = vcArray [t];
}
Response. Cookies. Add (new HttpCookie ("CheckCode", checkCode ));
(2) converting a randomly generated string into a verification code Image
/// <Summary>
/// Generate a verification code Image
/// </Summary>
/// <Param name = "checkCode"> random string </param>
Private void CreateCheckImage (string checkCode)
{
If (checkCode = null | checkCode. Trim () = string. Empty)
{
Return;
}
System. Drawing. Bitmap image = new System. Drawing. Bitmap (int) Math. Ceiling (checkCode. Length * 12.5), 22 );
Graphics g = Graphics. FromImage (image );
Try
{
Random radom = new Random ();
G. Clear (Color. White );
For (int I = 0; I <2; I ++)
{
Int x1 = radom. Next (image. Width );
Int x2 = radom. Next (image. Width );
Int y1 = radom. Next (image. Height );
Int y2 = radom. Next (image. Height );
G. DrawLine (new Pen (Color. Black), x1, y1, x2, y2 );
}
Font font = new Font ("Arial", 12, (FontStyle. Bold | FontStyle. Italic ));
LinearGradientBrush brush = new LinearGradientBrush (new Rectangle (0, 0, image. Width, image. Height), Color. Blue, Color. DarkRed, 1.2f, true );
G. DrawString (checkCode, font, brush, 2, 2 );
// Foreground noise of the image
For (int I = 0; I <50; I ++)
{
Int x = radom. Next (image. Width );
Int y = radom. Next (image. Height );
Image. SetPixel (x, y, Color. FromArgb (radom. Next ()));
}
// Draw the border line of the image
G. DrawRectangle (new Pen (Color. Silver), 0, 0, image. Width-1, image. Height-1 );
System. IO. MemoryStream MS = new System. IO. MemoryStream ();
Image. Save (MS, System. Drawing. Imaging. ImageFormat. Gif );
Response. ClearContent ();
Response. ContentType = "image/Gif ";
Response. BinaryWrite (ms. ToArray ());
}
Catch {}
Finally
{
G. Dispose ();
Image. Dispose ();
}
}
Finally, add an Image control to the webpage to display the verification code Image and set its ImageUrl attribute to the name of the verification code page.