When doing a login, it is usually necessary to bring a verification code, which is called on the ASPX page:
<input type= "text" id= "Txtcode" style= "WIDTH:35PX;HEIGHT:22PX; padding-top:3px; "maxlength=" 4 "/>
Style= "width:50px; Cursor:pointer; height:22px; "/>
<a style= "color: #528311; Cursor:pointer; font-size:12px; "Onclick=" Getsessionimg ($ (' #logingImg ')); " > Change a new </a>
<script type= "Text/javascript" >
Toggle Call
function Getsessionimg (objthis) {
$ ("#txtCode"). Val ("");
$ (objthis). attr ("src", "*/load_img/ajax-loader_05.gif");//Loading thumbnails
$ (objthis). attr ("src", "*/checkcode.aspx?id=" + math.random ());
$ ("#txtCode"). focus ();
}
</script>
Post code in Checkcode.aspx:
Reference namespaces: Using System.Drawing;
Load Call
protected void Page_Load (object sender, EventArgs e)
{
if (! IsPostBack)
{
This. Createcheckcodeimage (Generatecheckcode ());
}
}
<summary>
Generate a 4-digit verification Code
</summary>
private String Generatecheckcode ()
{
int number;
char code;
String Chkcode= String.Empty;
System.Random random = new Random ();
for (int i = 0; i < 4; i++)
{//randomly generated
Number = random. Next ();
if (number% 2 = = 0)
Code = (char) (' 0 ' + (char) (number% 10));//divide by 10 to take remainder (random numbers like 876 is 6) 00-09 and cast
Else
Code = (char) (' A ' + (char) number% 26);//divided by 26 to get the remainder of the result count and then the number of a back is the resulting value such as number=28, that is code=c
chkcode+= code. ToString ();
}
if (Checkcode.trim (). Equals ("") | | Checkcode.trim (). Length! = 4)
{
Generatecheckcode ();//Not enough four bits to build
}
Else
{
session["User_code"] =chkcode;//save session to verify that the login matches
}
return chkcode;
}
<summary>
Output Verification Code
</summary>
private void Createcheckcodeimage (string checkcode)
{
if (Checkcode = = NULL | | Checkcode.trim () = = String.Empty)
{
Return
}
System.Drawing.Bitmap image = new Bitmap ((int) math.ceiling ((Checkcode.length * 12.5)), 22); Returns the smallest integer greater than or equal to the specified number.
Graphics g = graphics.fromimage (image);//Create Image Object
try {
Random random = new random ();
G.clear (Color.White);
for (int 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.greenyellow), x1, y1, x2, y2); Draws a line that connects two points specified by a coordinate pair
}
Font
Font font = new System.Drawing.Font ("Verdana", A, (System.Drawing.FontStyle.Bold | System.Drawing.FontStyle.Italic));
Pen Brush
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);
Draw the foreground noise point of the picture
for (int i = 0; i <; i++)
{
int x = random. Next (image. Width);
int y = random. Next (image. Height);
Image. SetPixel (x, Y, Color.FromArgb (random. Next ())); To draw a point of color in a line of coordinates
}
Draw the border line of a picture
G.drawrectangle (New Pen (Color.bisque), 0, 0, image. Width-1, image. HEIGHT-1);
System.IO.MemoryStream ms = new System.IO.MemoryStream ();
Image. Save (MS, SYSTEM.DRAWING.IMAGING.IMAGEFORMAT.GIF);
Response.clearcontent ();
Response.ContentType = "Image/gif";
Response.BinaryWrite (Ms. ToArray ());//writes a binary string to the HTTP output stream
} catch (Exception ex) {
Console.Write (ex. Message);
} finally {
Close or release unmanaged resources such as files, streams, and handles persisted by instances of classes that implement this interface
G.dispose ();
Image. Dispose ();
}
}
C # Generate verification code