A simple color background graphic verification code and graphic Verification Code

Source: Internet
Author: User

A simple color background graphic verification code and graphic Verification Code

Namespace to be added first

Using System. Web. UI. WebControls;
Using System. Drawing. Drawing2D;
Using System. Drawing. Imaging;

 

Compile a method to generate a Random number. This method is very simple. The returned result is a string that needs to be displayed in the verification code. The Random class is a pseudo-Random number generator, you can generate a string of the specified length based on any custom string. For more information, see https://msdn.microsoft.com/zh-cn/library/system.random.aspx

// Obtain a random number. One parameter that calls this method is to specify the length of the returned string.
Private string GetRandString (int len)
{
String s = "0123456789 zxcbvnmasdfghjklpoiuyrtewqQWERTYUIOPLKJHGFDSAXZCVNBM ";
String str = "";
Random r = new Random ();
For (int I = 0; I <len; I ++)
{
Str + = s. Substring (r. Next (s. Length), 1 );
}
Return str;
}

All right, the randomly generated string already exists, and the next step is to display it as a graph.

Here, you can directly write it into the Load event, or encapsulate the implementation process (as a class) to facilitate reuse. Here, I will directly write it into the Load event.

Protected void Page_Load (object sender, EventArgs e)
{

Random rand = new Random ();

// Obtain random characters
String str = GetRandString (5 );

// Create a canvas
Bitmap image = new Bitmap (100, 30 );
Graphics g = Graphics. FromImage (image );
G. TextRenderingHint = System. Drawing. Text. TextRenderingHint. AntiAlias;
G. SmoothingMode = SmoothingMode. AntiAlias;

// Draw the gradient background
Rectangle rect = new Rectangle (0, 0, image. Width, image. Height );
Brush brushBack = new LinearGradientBrush (rect, Color. FromArgb (rand. Next (150,256), 255,255 ),
Color. FromArgb (255, rand. Next (150,256), 255), rand. Next (90 ));
G. FillRectangle (brushBack, rect );

  

// Draw interference Curves
For (int I = 0; I <2; I ++)
{
Point p1 = new Point (0, rand. Next (image. Height ));
Point p2 = new Point (rand. Next (image. Width), rand. Next (image. Height ));
Point p3 = new Point (rand. Next (image. Width), rand. Next (image. Height ));
Point p4 = new Point (image. Width, rand. Next (image. Height ));
Point [] p = {p1, p2, p3, p4 };
Pen pen = new Pen (Color. Gray, 1 );
G. DrawBeziers (pen, p );
}

  

// Draw text one by one
For (int I = 0; I <str. Length; I ++)
{
String strChar = str. Substring (I, 1 );
Int deg = rand. Next (-15, 15 );
Float x = (image. Width/str. Length/2) + (image. Width/str. Length) * I;
Float y = image. Height/2;
// Random font size
Font font = new Font ("Consolas", rand. Next (16, 24), FontStyle. Regular );
SizeF size = g. MeasureString (strChar, font );
Matrix m = new Matrix ();
// Rotate
M. RotateAt (deg, new PointF (x, y), MatrixOrder. Append );
// Twist
M. Shear (rand. Next (-10, 10) * 0.03f, 0 );
G. Transform = m;
// Random gradient Paint Brush
Brush brushPen = new LinearGradientBrush (rect, Color. fromArgb (rand. next (0,256), 0, 0), Color. fromArgb (0, 0, rand. next (0,256), rand. next (90 ));
G. DrawString (str. Substring (I, 1), font, brushPen, new PointF (x-size. Width/2, y-size. Height/2 ));

G. Transform = new Matrix ();
}

G. Save ();
Response. ContentType = "image/jpeg ";
Response. Clear ();
Response. BufferOutput = true;
Image. Save (Response. OutputStream, ImageFormat. Jpeg );

// Release the resources used
G. Dispose ();
Image. Dispose ();
Response. Flush ();

}

Execution result

 

Here we only implement a simple graphic verification code. I only use two interference lines here. In fact, it can be more realistic and more complex, for example, you can add some noise points on the background so that the font is not displayed so obvious that it interferes with the font .....

Of course, this is just a display image. You must have a text box. How can you determine whether the entered verification code is correct? If all the verification codes are provided, the verification is simple, is the string returned by the GetRandString (int len) method in the randomly generated graphic verification code, if you compare the string returned by this method with the memory in the text box, you will find that the entered verification code is correct ....

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.