Verification Code
Collect a small section of the. NET validation code snippet for later reference.
protected void Page_Load (object sender, EventArgs e)
{
//Generate numeric string first
string checkcode = this. Createrandomcode (6);
//Save with session
session["Checkcode"] = Checkcode;
//Drawing
createimage (checkcode);
}
private void CreateImage ( String checkcode)
{
System.Drawing.Bitmap Image = New System.Drawing.Bitmap (Convert.ToInt32 (math.ceiling (decimal) (Checkcode.length * 14)), 22);
Graphics g = graphics.fromimage (image);
Try
{
Random Random = new Random ();
G.clear (Color.aliceblue);
for (int i = 0; i < 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 ("Comic Sans MS ", System.Drawing.FontStyle.Bold);
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, new SolidBrush (Color.Red), 2, 2);
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 ()));
}
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 ();
}
}
public string Createrandomcode (int codecount)
{
String Allchar = "0,1,2,3,4,5,6,7,8,9,a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z";
string[] Allchararray = Allchar.split (', ');
String randomcode = "";
int temp =-1;
Random rand = new Random ();
for (int i = 0; i < Codecount; i++)
{
if (temp!=-1)
{
Rand = new Random (i * temp * ((int) DateTime.Now.Ticks));
}
int t = rand. Next (36);
if (temp!=-1 && temp = t)
{
Return Createrandomcode (Codecount);
}
temp = t;
Randomcode + = allchararray[t];
}
return randomcode;
}