Image Verification Code Generation

Source: Internet
Author: User

Using System;
Using System. IO;
Using System. Web;
Using System. Text;
Using System. Drawing;
Using System. Drawing. Imaging;
/// <Summary>
/// Generate an image using Cls_PicVcode
/// </Summary>
Public class ValidateCode
{
/// <Summary>
/// This method is used to generate a random number of specified digits.
/// </Summary>
/// <Param name = "VcodeNum"> the parameter is the number of digits of a random number. </param>
/// <Returns> returns a random number string </returns>
Private string RndNum (int VcodeNum)
{
String Vchar = "1, 2, 3, 4, 5, 6, 7, 8, 9, A, B, C, D, E, F, G, H, I, J, K, L, M, n, P "+
", Q, R, S, T, U, V, W, X, Y, Z ";
String [] VcArray = Vchar. Split (new Char [] {,}); // Split it into an array
String VNum = ""; // random number generated
Int temp =-1; // record the previous random value. Avoid producing several identical 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 (33 );
If (temp! =-1 & temp = t)
{
Return RndNum (VcodeNum );
}
Temp = t;
VNum + = VcArray [t];
}
Return VNum;
}
/// <Summary>
/// This method writes the generated random number to the image file
/// </Summary>
/// <Param name = "VNum"> VNum is a random number </param>
Public MemoryStream Create (out string VNum)
{
VNum = RndNum (4 );
Bitmap Img = null;
Graphics g = null;
MemoryStream MS = null;
System. Random random = new Random ();
// Set of verification code colors
Color [] c = {Color. Black, Color. Red, Color. DarkBlue, Color. Green, Color. Orange, Color. Brown, Color. DarkCyan, Color. Purple };
// Set of verification code Fonts
String [] fonts = {"Verdana", "Microsoft Sans Serif", "Comic Sans MS", "Arial", ""};


// Define the image size and generate an image instance
Img = new Bitmap (100, 25 );

G = Graphics. FromImage (Img); // generate a new Graphics object from the Img object

G. Clear (Color. White); // The background is White.

// Draw background points at random positions
For (int I = 0; I <100; I ++)
{
Int x = random. Next (Img. Width );
Int y = random. Next (Img. Height );
G. DrawRectangle (new Pen (Color. LightGray, 0), x, y, 1, 1 );
}
// The verification code is drawn in g
For (int I = 0; I <VNum. Length; I ++)
{
Int cindex = random. Next (7); // random color index value
Int findex = random. Next (5); // random font index value
Font f = new System. Drawing. Font (fonts [findex], 14, System. Drawing. FontStyle. Bold); // Font
Brush B = new System. Drawing. SolidBrush (c [cindex]); // color
Int ii = 4;
If (I + 1) % 2 = 0) // The control Verification Code is not in the same height
{
Ii = 2;
}
G. DrawString (VNum. Substring (I, 1), f, B, 3 + (I * 20), ii); // draw a verification character
}
MS = new MemoryStream (); // generates a memory stream object
Img. Save (MS, ImageFormat. Jpeg); // Save this image as a jpg image file to the stream

// Reclaim Resources
G. Dispose ();
Img. Dispose ();
Return MS;

}
}

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.