asp.net (C #) code for generating random verification codes _ Practical Tips

Source: Internet
Author: User
Commonly used to generate the verification Code program, the picture effect is as follows:

The source program is as follows:
Copy Code code as follows:

Using System;
Using System.IO;
Using System.Drawing;
Using System.Drawing.Imaging;
Using System.Text;
Using System.Collections;
Using System.Web;
Using System.Web.UI;
Using System.Web.UI.WebControls;
Using System.Web.UI.WebControls.WebParts;
Using System.Web.UI.HtmlControls;
/**////<summary>
///
* * asp.net (C #) Generate Verification code * *
///
File:GenerateCheckCode.aspx.cs
///
Author: Zhou Zhenxing (Zxjay drifting away)
///
E-mail:tda7264@163.com
///
Date:07-04-10
///
</summary>
public partial class GenerateCheckCode:System.Web.UI.Page
... {
protected void Page_Load (object sender, EventArgs e)
... {
String Chkcode = String. Empty;
A list of colors used to verify code, noise, and noise.
color[] color = ... {color.black, color.red, Color.Blue, Color.green, Color.orange, Color.brown, Color.brown, Color.DarkBlue};
Font list, for verification code
string[] Font = ... {"Times New Roman", "MS Mincho", "Book Antiqua", "Gungsuh", "PMingLiU", "Impact"};
The character set of the verification code, removing some confusing characters
char[] character = ... {' 2 ', ' 3 ', ' 4 ', ' 5 ', ' 6 ', ' 8 ', ' 9 ', ' A ', ' B ', ' ' C ', ' D ', ' E ', ' F ', ' G ', ' H ', ' J ', ' K ', ' L ', ' M ', ' N ', ' P ', ' R ', ' S ', ' T ', ' W ', ' X ', ' Y '};
Random rnd = new Random ();
To generate a validation code string
for (int i = 0; i < 4; i++)
... {
Chkcode + = Character[rnd. Next (character. Length)];
}
Bitmap bmp = new Bitmap (100, 40);
Graphics g = graphics.fromimage (BMP);
G.clear (Color.White);
Draw a noise line
for (int i = 0; i < i++)
... {
int x1 = rnd. Next (100);
int y1 = rnd. Next (40);
int x2 = rnd. Next (100);
int y2 = rnd. Next (40);
Color clr = color[rnd. Next (color. Length)];
G.drawline (new Pen (CLR), x1, y1, x2, y2);
}
Draw Verification Code string
for (int i = 0; i < chkcode.length; i++)
... {
String fnt = Font[rnd. Next (font. Length)];
Font ft = new Font (FNT, 18);
Color clr = color[rnd. Next (color. Length)];
g.DrawString (Chkcode[i]. ToString (), FT, new SolidBrush (CLR), (float) i * + 8, (float) 8);
}
Draw noise.
for (int i = 0; i < i++)
... {
int x = rnd. Next (BMP. Width);
int y = rnd. Next (BMP. Height);
Color clr = color[rnd. Next (color. Length)];
Bmp. SetPixel (x, Y, CLR);
}
Clears the page output cache, setting the page without caching
Response.Buffer = true;
Response.ExpiresAbsolute = System.DateTime.Now.AddMilliseconds (0);
Response.Expires = 0;
Response.CacheControl = "No-cache";
Response.appendheader ("Pragma", "No-cache");
Writes the CAPTCHA picture to the memory stream and outputs it in "image/png" format
MemoryStream ms = new MemoryStream ();
Try
... {
Bmp. Save (MS, Imageformat.png);
Response.clearcontent ();
Response.ContentType = "Image/png";
Response.BinaryWrite (Ms. ToArray ());
}
Finally
... {
Explicit release of resources
Bmp. Dispose ();
G.dispose ();
}
}
}
Use the following methods:
Create a new file named Generatecheckcode.aspx, and copy the above code to the code file GenerateCheckCode.aspx.cs
Place the statement <asp:image id= "IMG1" runat= "Server" imageurl= "~/generatecheckcode.aspx"/> Where the authentication code is needed.

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.