Dynamic Verification Code Loading

Source: Internet
Author: User

1. Create a general application ashx

<% @ WebHandler Language = "C #" Class = "YZM" %>
 
Using System;
Using System. Web;
 
Public class YZM: IHttpHandler, System. Web. SessionState. IRequiresSessionState {

Public void ProcessRequest (HttpContext context ){
// Dynamically generate an image
Context. Response. ContentType = "image/JPEG ";
Using (System. Drawing. Bitmap bitmap = new System. Drawing. Bitmap (100, 30 ))
{
Using (System. Drawing. Graphics g = System. Drawing. Graphics. FromImage (bitmap ))
{
/*
G. drawString ("Verification Code", new System. drawing. font ("", 10), System. drawing. brushes. red, new System. drawing. pointF (0, 0 ));
Bitmap. Save (context. Response. OutputStream, System. Drawing. Imaging. ImageFormat. Jpeg );
*/
// Dynamically generate random numbers
Random rand = new Random ();
Int code = rand. Next ();
String strCode = code. ToString ();
HttpContext. Current. Session ["Code"] = strCode;
G. drawString (strCode, new System. drawing. font ("", 12), System. drawing. brushes. green, new System. drawing. pointF (0, 0 ));
Bitmap. Save (context. Response. OutputStream, System. Drawing. Imaging. ImageFormat. Jpeg );
}
}
}


Public bool IsReusable {
Get {
Return false;
}
}
 
 

}

2. Call the verification code stored in the Session in aspx

Using System;
Using System. Collections. Generic;
Using System. Linq;
Using System. Web;
Using System. Web. UI;
Using System. Web. UI. WebControls;
 
Public partial class verification code: System. Web. UI. Page
{
Protected void Page_Load (object sender, EventArgs e)
{
 
}
Protected void button#click (object sender, EventArgs e)
{
String yzm = Convert. ToString (Session ["Code"]);
Response. Write (yzm );
If (yzm = TextBox1. Text)
{
Response. Write ("correct Verification Code ");
}
Else
{
Response. Write ("the verification code is incorrect. Please enter it again ");
TextBox1.Text = "";
}
}
}


From programmers

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.