<summary>
Generate a Captcha picture, save the session name Verificationcode
</summary>
public static void Createverificationcode ()
{
int number;
String Checkcode = String. Empty;
Random Number of seeds
Random randoms = new Random ();
for (int i = 0; i < 4; i++)//Check code length is 4
{
A random integer
Number = Randoms. Next ();
The characters are randomly generated from the 0-9,a-z, and the corresponding ASCII code is
48-57,65-90
Number = number% 36;
if (number < 10)
{
Number = 48;
}
Else
{
Number = 55;
}
Checkcode + = ((char) number). ToString ();
}
Save the checksum code in session
system.web.httpcontext.current.session["Verificationcode"] = Checkcode;
If the check code is empty, return directly
if (Checkcode = null | | Checkcode.trim () = String.Empty)
{
Return
}
Determines the length of the output picture based on the length of the checksum code
System.Drawing.Bitmap image = New System.Drawing.Bitmap (a);//(int) math.ceiling (convert.todouble ( Checkcode.length * 15))
Creating graphics Objects
Graphics g = graphics.fromimage (image);
Try
{
Generate random number of seeds
Random Random = new Random ();
Empty picture background color
G.clear (Color.White);
Picture of the background noise line 10
//---------------------------------------------------
for (int i = 0; i < i++)
{
Noise line Start coordinates (X1,Y1), endpoint coordinates (X2,Y2)
int x1 = Random. Next (image. Width);
int x2 = random. Next (image. Width);
int y1 = random. Next (image. Height);
int y2 = random. Next (image. Height);
Draw the noise line with silver
G.drawline (New Pen (color.silver), x1, y1, x2, y2);
}
//---------------------------------------------------
Brush B = brushes.silver;
G.fillrectangle (b, 0, 0, image. Width, image. Height);
---------------------either of the above two options------------------------------
Output picture commander of the code: 12th number Arial, bold italic
Font font = new Font ("Arial", FontStyle.Bold | Fontstyle.italic));
Linear gradient Painting brush
LinearGradientBrush brush = new LinearGradientBrush (new Rectangle (0, 0, image. Width, image. Height), Color.Blue, Color.purple, 1.2f, true);
g.DrawString (Checkcode, Font, brush, 2, 2);
Picture the foreground noise point 50
for (int i = 0; i < i++)
{
int x = random. Next (image. Width);
int y = random. Next (image. Height);
Image. SetPixel (x, Y, Color.FromArgb) (random. Next ()));
}
Draw a picture's border line
G.drawrectangle (New Pen (Color.peru), 0, 0, image. Width-1, image. HEIGHT-1);
Create a memory stream to output a picture
using (MemoryStream ms = new MemoryStream ())
{
Picture format specified as PNG
Image. Save (MS, Imageformat.jpeg);
Clears all output from the buffer stream
System.Web.HttpContext.Current.Response.ClearContent ();
The HTTP MIME type of the output stream is set to "Image/png"
System.Web.HttpContext.Current.Response.ContentType = "Image/jpeg";
Output binary stream of picture
System.Web.HttpContext.Current.Response.BinaryWrite (Ms. ToArray ());
}
}
Finally
{
Releasing bitmap objects and graphics objects
G.dispose ();
Image. Dispose ();
}
}