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