public class ValidateCode:System.Web.UI.Page
{
private void Page_Load (object sender, System.EventArgs e)
{
this. Createcheckcodeimage (Generatecheckcode ());
}
#region The code generated by the Web Forms Designer override
protected void OnInit (EventArgs e)
{
//
//CodeGen: This call is required for the ASP.net Web forms Designer.
//
InitializeComponent ();
base. OnInit (e);
}
///<summary>
///Designer supports the desired method-do not modify
with the Code Editor
///The contents of this method.
///</summary>
private void
InitializeComponent ()
{
this. Load + = new System.EventHandler (this. Page_Load);
}
#endregion
Private
string Generatecheckcode ()
{
int number;
char code;
string checkcode = String.Empty;
system.random Random = new Random ();
for (int i=0;i<5;i++)
{
number = random. Next ();
if (number% 2 = 0)
code = (char) (' 0 ' + (char) (number% 10));
Else
code = (char) (' A ' + (char) (number% 26));
Checkcode + = code. ToString ();
}
Response.Cookies.Add (New HttpCookie ("Checkcode", Checkcode));
return checkcode;
}
private void Createcheckcodeimage (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
{
//generating random generators
Random Random = new Random ();
//Empty picture background color
g.clear (Color.White);
//Picture background noise line
for (int i=0;i<25;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);
}
Font font = new System.Drawing.Font (Arial), (System.Drawing.FontStyle.Bold | System.Drawing.FontStyle.Italic));
System.Drawing.Drawing2D.LinearGradientBrush brush = new System.Drawing.Drawing2D.LinearGradientBrush (New Rec Tangle (0, 0, image. Width, image. Height), Color.Blue, color.darkred, 1.2f, true);
g.drawstring (checkcode, Font, brush, 2, 2);
//Picture of the foreground noise point
for (int i=0;i<100;i++)
{
int x = random. Next (image. Width);
int y = random. Next (image. Height);
image. SetPixel (x, Y, Color.FromArgb) (random. Next ()));
}
//Picture border line
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 ());
}
finally
{
G.dispose ();
image. Dispose ();
}
}
}