Get the App
SLTechnology News&Howtos  ›  Development  › 

How to make website CAPTCHA

Shulou Source: shulou.com Published: 2022-06-02 03:09:56 09月13日 Update

This article introduces you how to make the verification code of the website, the content is very detailed, interested friends can refer to, I hope it can be helpful to you.

Production of asp.net CAPTCHA

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

/ / Color list, which is used for verification code, noise line, noise point

Color [] color = {Color.Black, Color.Red, Color.Blue, Color.Green, Color.Orange, Color.Brown, Color.Brown, Color.DarkBlue}

/ / Font list, used for CAPTCHA

String [] font = {"Times New Roman", "MS Mincho", "Book Antiqua", "Gungsuh", "PMingLiU", "Impact"}

/ / the character set of the CAPTCHA, removing some characters that are easy to be confused

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' 'Xuan,'Y'}

Random rnd = new Random ()

/ / generate a CAPTCHA string

For (int I = 0; I < 4; iTunes +)

{

ChkCode + = "[rnd.Next (character.Length)]

}

/ / use session object to store CAPTCHA

Session ["name"] = chkCode

Bitmap bmp = new Bitmap (100,40)

Graphics g = Graphics.FromImage (bmp)

G.Clear (Color.White)

/ / draw noise lines

For (int I = 0; I < 10; iTunes +)

{

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 CAPTCHA string

For (int I = 0; I < chkCode.Length; iTunes +)

{

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 * 20 + 8, (float) 8)

}

/ / draw noise

For (int I = 0; I < 100; iTunes +)

{

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 and set the page to have no cache

Response.Buffer = true

Response.ExpiresAbsolute = System.DateTime.Now.AddMilliseconds (0)

Response.Expires = 0

Response.CacheControl = "no-cache"

Response.AppendHeader ("Pragma", "No-Cache")

/ / write the CAPTCHA image to the memory stream and output it in "p_w_picpath/Png" format

MemoryStream ms = new MemoryStream ()

Try

{

Bmp.Save (ms, ImageFormat.Png)

Response.ClearContent ()

Response.ContentType = "p_w_picpath/Png"

Response.BinaryWrite (ms.ToArray ())

}

Finally

{

/ / explicitly release resources

Bmp.Dispose ()

G.Dispose ()

}

}

}

}

About how to carry on the website CAPTCHA production to share here, hope that the above content can have some help to everyone, can learn more knowledge. If you think the article is good, you can share it for more people to see.

Tags: Validation characters production website content strings more cache help output good interests memory pictures fonts character sets objects guys buddies articles Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno MariaDB macOS OPPO Reno Shulou Technology Xiaomi