we are landing on the site, blog or submit comments, often encountered to fill in the verification code this item, the feeling is very magical appearance, and finally in the Beef Brisket Press release system contacted, here a small summary.
There are three things to use: An ASPX page, a picture
first, create an ASPX page.
<%@ WebHandler language= "C #" class= "picture"%> using System;
Using System.Web;
Using System.Drawing;
Using System.Drawing.Drawing2D;
Using System.Web.SessionState;
public class Picture:ihttphandler, IRequiresSessionState//To use session must implement this interface, remember to import System.Web.SessionState namespace { public void ProcessRequest (HttpContext context) {string checkcode = Gencode (5); Generates a 5-bit random character context. session["Code"] = Checkcode;
Save string to session for validation when needed System.Drawing.Bitmap image = new System.Drawing.Bitmap (70, 22);
Graphics g = graphics.fromimage (image);
try {//Generate a random generator Random Random = new Random ();
Clear the picture background color g.clear (color.white);
Picture of the background noise line int i; for (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 ("Arial", (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, brush, 2, 2); Picture the foreground noise point 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); Context.
Response.clearcontent (); Context.
Response.ContentType = "Image/gif"; Context. Response.BinaryWrite (Ms.
ToArray ());
finally {g.dispose (); Image.
Dispose ();
}
} <summary>///produces random strings///</summary>///<param name= "num" > random out a few characters </param> <returns> Random string </returns> private string gencode (int num) {string str = 0123456789ABCD
EFGHIJKLMNOPQRSTUVWXYZ "; char[] Chastr = str.
ToCharArray ();
String code = "";
Random rd = new Random ();
int i; for (i = 0; i < num i++) {//code + = Source[rd. Next (0, source.
Length)]; Code + = str. Substring (Rd. Next (0, str.
Length), 1);
} return code;
public bool IsReusable {get {return false;
}
}
}second, an IMG control is added to the interface used to verify the code, and the SRC attribute points to the. ashx file.
then, determine if the submitted verification code is correct.
Determine if the validation code entered the correct
string code = TxtCode.Text.Trim (). ToUpper ()//converts the input letters to uppercase and then compares
string rightcode = session["Code". ToString ();
if (Code!=rightcode)
{
Page.ClientScript.RegisterStartupScript (Page.gettype (), "message", "<script language= ' JavaScript ' Defer>alert (' validation code input error. ')//Prompt error to ensure that the background does not change white return;</script>
;
}
Finally, use Javascrip to refresh the verification code. Because the code is relatively small, it can be nested in code.
<asp:content id= "Content1" contentplaceholderid= "Head" runat= "Server" >
<script "javascript" Type= "Text/javascript" >
function ChangeCode () {
var imgnode = document.getElementById ("vimg");
IMGNODE.SRC = "handler/watermark.ashx?t=" + (new Date ()). valueof (); The additional time parameter here is to prevent browser caching problems
}
</script>
</asp:Content>