ValidNumber. ashx Verification Code

Source: Internet
Author: User

ValidNumber. ashx code:

<% @ WebHandler Language = "C #" Class = "ValidNumber" %>

Using System;
Using System. Web;
Using System. Web. SessionState; // Step 1: import the namespace
Public class ValidNumber: IHttpHandler, IRequiresSessionState
{

Public void ProcessRequest (HttpContext context)
{
// Seed
String [] strlist = new []
{
"A", "B", "c", "d", "1", "2", "3", "4", "5", "6 ", "7", "8", "9", "0", "q", "w", "e", "r", "t", "y ", "u", "I", "o", "p", "z", "x", "n", "v", "W", "Q ", "B", "A", "D", "D", "F", "G", "H", "J", "K", "L"
};

// Generate a random number
String [] list = new string [4];
Random rd = new Random (DateTime. Now. Millisecond * 100000 );
For (int I = 0; I <4; I ++)
{
List [I] = strlist [rd. Next (strlist. Length)];
System. Threading. Thread. Sleep (5 );
}

System. Text. StringBuilder sb = new System. Text. StringBuilder ();
For (int I = 0; I <4; I ++)
{
Sb = sb. Append (list [I]);
}
HttpContext. Current. Session ["validNumber"] = sb;
// Create an image
System. Drawing. Bitmap image = new System. Drawing. Bitmap (list. Length * 15, 40 );
// Create a canvas
System. Drawing. Graphics g = System. Drawing. Graphics. FromImage (image );
// Clear the background color and add it to white
G. Clear (System. Drawing. Color. White );

// Noise line
For (int I = 0; I <20; I ++)
{
Random rds = new Random ();

Float x1 = rds. Next (image. Width-1 );
Float x2 = rds. Next (image. Width-1 );
Float y1 = rds. Next (image. Height-1 );
Float y2 = rds. Next (image. Height-1 );
System. Threading. Thread. Sleep (5 );
G. DrawLine (new System. Drawing. Pen (System. Drawing. Color. Silver), x1, y1, x2, y2 );
}
System. Drawing. Font f = new System. Drawing. Font ("", 25, System. Drawing. GraphicsUnit. Pixel );
System. Drawing. Brush bs = new System. Drawing. SolidBrush (System. Drawing. Color. Red );

G. DrawString (sb. ToString (), f, bs, 5, 5 );
// Current request output type
HttpContext. Current. Response. ContentType = "image/jpeg ";
// Save it to the output stream
Image. Save (HttpContext. Current. Response. OutputStream, System. Drawing. Imaging. ImageFormat. Jpeg );

G. Dispose ();
Image. Dispose ();

}

Public bool IsReusable
{
Get
{
Return false;
}
}

}

but the page image does not change when the session value is sent back. There is no ideal solution at the moment. Response is used in the background for submission. write ("<script> window. location. href = window. location. href </script> ") refresh the page. Thank you for your advice. Is there any better usage.

 


From column BQL_Email

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.