Website Verification Code Making

Source: Internet
Author: User

ASP. NET Verification Code production

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;


Namespace WebApplication1

{

public partial class Yzm:System.Web.UI.Page

{

protected void Page_Load (object sender, EventArgs e)

{

String Chkcode = String. Empty;

List of colors for verification codes, noise lines, noise points

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 captcha, removing some easily 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 ();

Generate Authenticode String

for (int i = 0; i < 4; i++)

{

Chkcode + = Character[rnd. Next (character. Length)];

}

Store verification code with session object

session["name"] = Chkcode;

Bitmap bmp = new Bitmap (100, 40);

Graphics g = graphics.fromimage (BMP);

G.clear (Color.White);

Draw 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 a CAPTCHA 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 points

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);

}

Clear the page output cache, set 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 a 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

{

Explicitly releasing resources

Bmp. Dispose ();

G.dispose ();

}

}

}

}



Website Verification Code Making

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.