asp.net implementation code and refresh Verification Code Small Example _ practical skills

Source: Internet
Author: User

Implementation code

Copy Code code as follows:

<summary>
Generate a Captcha picture, save the session name Verificationcode
</summary>
public static void Createverificationcode ()
{
int number;
String Checkcode = String. Empty;

Random Number of seeds
Random randoms = new Random ();

for (int i = 0; i < 4; i++)//Check code length is 4
{
A random integer
Number = Randoms. Next ();

The characters are randomly generated from the 0-9,a-z, and the corresponding ASCII code is
48-57,65-90
Number = number% 36;
if (number < 10)
{
Number = 48;
}
Else
{
Number = 55;
}
Checkcode + = ((char) number). ToString ();
}

Save the checksum code in session
system.web.httpcontext.current.session["Verificationcode"] = Checkcode;

If the check code is empty, return directly
if (Checkcode = null | | Checkcode.trim () = String.Empty)
{
Return
}
Determines the length of the output picture based on the length of the checksum code
System.Drawing.Bitmap image = New System.Drawing.Bitmap (a);//(int) math.ceiling (convert.todouble ( Checkcode.length * 15))
Creating graphics Objects
Graphics g = graphics.fromimage (image);
Try
{
Generate random number of seeds
Random Random = new Random ();
Empty picture background color
G.clear (Color.White);
Picture of the background noise line 10
//---------------------------------------------------
for (int i = 0; i < i++)
{
Noise line Start coordinates (X1,Y1), endpoint coordinates (X2,Y2)
int x1 = Random. Next (image. Width);
int x2 = random. Next (image. Width);
int y1 = random. Next (image. Height);
int y2 = random. Next (image. Height);

Draw the noise line with silver
G.drawline (New Pen (color.silver), x1, y1, x2, y2);
}
//---------------------------------------------------
Brush B = brushes.silver;
G.fillrectangle (b, 0, 0, image. Width, image. Height);
---------------------either of the above two options------------------------------
Output picture commander of the code: 12th number Arial, bold italic
Font font = new Font ("Arial", FontStyle.Bold | Fontstyle.italic));

Linear gradient Painting brush
LinearGradientBrush brush = new LinearGradientBrush (new Rectangle (0, 0, image. Width, image. Height), Color.Blue, Color.purple, 1.2f, true);
g.DrawString (Checkcode, Font, brush, 2, 2);

Picture the foreground noise point 50
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 ()));
}

Draw a picture's border line
G.drawrectangle (New Pen (Color.peru), 0, 0, image. Width-1, image. HEIGHT-1);

Create a memory stream to output a picture
using (MemoryStream ms = new MemoryStream ())
{
Picture format specified as PNG
Image. Save (MS, Imageformat.jpeg);
Clears all output from the buffer stream
System.Web.HttpContext.Current.Response.ClearContent ();
The HTTP MIME type of the output stream is set to "Image/png"
System.Web.HttpContext.Current.Response.ContentType = "Image/jpeg";
Output binary stream of picture
System.Web.HttpContext.Current.Response.BinaryWrite (Ms. ToArray ());
}
}
Finally
{
Releasing bitmap objects and graphics objects
G.dispose ();
Image. Dispose ();
}
}

Create an ASPX page

Copy Code code as follows:

<%@ Page language= "C #" autoeventwireup= "true" codefile= "AuthCode.aspx.cs" inherits= "Authcode"%>

<%help.createverificationcode (); %>

Add HTML code, reference

Copy Code code as follows:

<div class= "Positionr" >
<label> Verification Code:</label>
<span class= "Style1" > *</span>
<input type= "text" class= "YANZM" runat= "Server" reg= "^.+$" id= "Txtauthcode" tip= "Please enter the verification code!"/>
</div>

How do I implement a refresh?

Copy Code code as follows:

<script type= "Text/javascript" >
$ ("#imgAuthCode"). Click (function () {
$ (this). attr ("src", "authcode.aspx?code=" + (new Date ()). GetTime ());
});
</script>

Effect chart

Instance Downloads

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.