------Generation Method One
Pageload (object sender, Eventarges e)
{
String Chkcode = String. Empty;
List of colors for verification codes, noise lines, noise points
color[] color = {Color.Black, color.red, Color.Blue, Color.green, Color.orange, Color.brown, Color.brown, Color.DarkBlue };
Font list, for verification code
string[] Font = {"Times New Roman", "MS Mincho", "Book Antiqua", "Gungsuh", "PMingLiU", "Impact"};
The character set of the captcha, removing some easily confusing characters
char[] character = {' 2 ', ' 3 ', ' 4 ', ' 5 ', ' 6 ', ' 8 ', ' 9 ', ' A ', ' B ', ' C ', ' D ', ' E ', ' F ', ' G ', ' H ', ' J ', ' K ', ' L ', ' M ', ' N ', ' P ', ' R ', ' S ', ' T ', ' W ', ' X ', ' Y '};
Random rnd = new Random ();
Generate Authenticode String
for (int i = 0; i < 4; i++)
{
Chkcode + = Character[rnd. Next (character. Length)];
}
Bitmap bmp = new Bitmap (100, 40);
Graphics g = graphics.fromimage (BMP);
G.clear (Color.White);
Draw Noise Line
for (int i = 0; i <; i++)
{
int x1 = rnd. Next (100);
int y1 = rnd. Next (40);
int x2 = rnd. Next (100);
int y2 = rnd. Next (40);
Color clr = color[rnd. Next (color. Length)];
G.drawline (new Pen (CLR), x1, y1, x2, y2);
}
Draw a CAPTCHA string
for (int i = 0; i < chkcode.length; i++)
{
String fnt = Font[rnd. Next (font. Length)];
Font ft = new Font (FNT, 18);
Color clr = color[rnd. Next (color. Length)];
g.DrawString (Chkcode[i]. ToString (), FT, new SolidBrush (CLR), (float) I * 8, (float) 8);
}
Draw Noise points
for (int i = 0; i <; i++)
{
int x = rnd. Next (BMP. Width);
int y = rnd. Next (BMP. Height);
Color clr = color[rnd. Next (color. Length)];
Bmp. SetPixel (x, Y, CLR);
}
Clear the page output cache, set the page without caching
Response.Buffer = true;
Response.ExpiresAbsolute = System.DateTime.Now.AddMilliseconds (0);
Response.Expires = 0;
Response.CacheControl = "No-cache";
Response.appendheader ("Pragma", "No-cache");
Writes a CAPTCHA picture to the memory stream and outputs it in "image/png" format
MemoryStream ms = new MemoryStream ();
Try
{
Bmp. Save (MS, Imageformat.png);
Response.clearcontent ();
Response.ContentType = "Image/png";
Response.BinaryWrite (Ms. ToArray ());
}
Finally
{
Explicitly releasing resources
Bmp. Dispose ();
G.dispose ();
}
}
------Generation Method Two
<%@ WebHandler language= "C #" class= "Yzcodehandler"%>
Using System;
Using System.Web;
Using System.Drawing;
public class Yzcodehandler:ihttphandler, System.Web.SessionState.IRequiresSessionState
{
public string charSet = "2,3,4,5,6,8,9,a,b,c,d,e,f,g,h,j,k,m,n,p,r,s,u,w,x,y";
public void ProcessRequest (HttpContext context) {
String validatecode = Createrandomcode (4);
Context. session["Validatecode"] = Validatecode;
CreateImage (Validatecode, context);
}
public bool IsReusable {
get {
return false;
}
}
<summary>
Generate Verification Code
<param name= "n" > number of digits </param>
<returns> Verification Code String </returns>
private string Createrandomcode (int n)
{
string[] Chararray = Charset.split (', ');
String randomcode = "";
int temp =-1;
Random rand = new Random ();
for (int i = 0; i < n; i++)
{
if (temp! =-1)
{
Rand = new Random (i * temp * ((int) DateTime.Now.Ticks));
} int t = rand. Next (chararray.length-1);
if (temp = = t) {
return Createrandomcode (n);
} temp = t;
Randomcode + = chararray[t];
}
return randomcode;
}
private void CreateImage (String checkcode, HttpContext context)
{
int iwidth = (int) (CHECKCODE.LENGTH * 13);
System.Drawing.Bitmap image = New System.Drawing.Bitmap (iwidth, 23);
Graphics g = graphics.fromimage (image);
Font f = new System.Drawing.Font ("Arial", A, (System.Drawing.FontStyle.Italic | System.Drawing.FontStyle.Bold)); Front view
Brush B = new System.Drawing.SolidBrush (color.black); Background color
G.clear (Color.White); Fill text
g.DrawString (Checkcode, F, b, 0, 1); Random lines
Pen Linepen = new Pen (color.gray, 0); Random rand = new Random ();
for (int i = 0; i < 5; i++) {
int x1 = Rand. Next (image. Width);
int y1 = rand. Next (image. Height);
int x2 = rand. Next (image. Width);
int y2 = rand. Next (image. Height);
G.drawline (Linepen, x1, y1, x2, y2);
}//Random point
for (int i = 0; i <; i++)
{
int x = rand. Next (image. Width);
int y = rand. Next (image. Height);
Image. SetPixel (x, y, Color.gray);
}//Border
G.drawrectangle (New Pen (Color.gray), 0, 0, image. Width-1, image. HEIGHT-1); Output picture
System.IO.MemoryStream ms = new System.IO.MemoryStream ();
Image. Save (MS, System.Drawing.Imaging.ImageFormat.Jpeg);
Context. Response.clearcontent ();
Context. Response.ContentType = "Image/jpeg";
Context. Response.BinaryWrite (Ms. ToArray ());
G.dispose ();
Image. Dispose ();
}
}
----------------
Validation methods
if (TxtValidateCode.Text.ToUpper (). Trim () = = session["Validatecode"]. ToString (). ToUpper (). Trim ())
{
True
}
Else
{
False
}
C # Generate verification code