Verification Code
#region "Custom"//Authentication Code
private void Createcheckcodeimage ()
{
session["Checkcode"]=null;
String Checkcode=this. Createcheckcode (4). Trim ();
if (Checkcode==null | | checkcode==string.empty)
{
Return
}
Else
session["Checkcode"]=checkcode;
System.Drawing.Bitmap image = new System.Drawing.Bitmap (int) math.ceiling ((double) checkcode.length * 13), 24);
Graphics g = graphics.fromimage (image);//http://localhost/dx/global.asax
Try
{
Generate a random generator
Random Random = new Random ();
Empty picture background color
G.clear (Color.White);
Background noise line for painting pictures
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 Rectangle (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 ()));
// }
Draw a picture's 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 ();
}
}
private string Createcheckcode (int length)
{
int number;
Char code= (char) 0;
string checkcode = String.Empty;
System.Random Random = new Random ();
for (int i=0; i<length;i++)
{
Number = random. Next ();
if (number% 2 = 0)
Code = (char) (' 0 ' + (char) (number% 10));
Else
Code = (char) (' A ' + (char) (number% 26);
Checkcode + = code. ToString ();
}
return checkcode;
}
#endregion