Get the App
SLTechnology News&Howtos  ›  Development  › 

How to implement asp.net to create bitmap and generate verification picture class

Shulou Source: shulou.com Published: 2022-06-03 07:44:18 09月13日 Update

This article introduces the relevant knowledge of "how to create bitmaps to generate and verify picture classes in asp.net". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

The copy code is as follows:

Public void ProcessRequest (HttpContext context)

{

Context.Response.ContentType = "image/jpeg"

/ / create a bitmap and specify the width and height of the frame

Using (Image img=new Bitmap (800.25))

{

/ / create a painter object and draw a string in the img object

Using (Graphics g=Graphics.FromImage (img))

{

/ / sets the background color of the bitmap. The default is black.

G.Clear (Color.White)

/ / set the width and height of the CAPTCHA. Img.Width-1 and img.Height-1 mainly cover the border with the background color.

G.DrawRectangle (Pens.Black, 0,0, img.Width-1, img.Height-1)

/ / transfer 100 noises, painter objects, bitmap objects

DrawPoint (100g, img)

/ / draw a string of 4 CAPTCHA codes

String vcode=GetCode (4); / / vcode can be assigned to Cookie here

G.DrawString (vcode

New Font ("Arial", 14, FontStyle.Strikeout | FontStyle.Strikeout), / / FontStyle font style, multiple styles are required | Line

Brushes.Black

New RectangleF (r.Next (20), r.Next (7), img.Width, img.Height))

Img.Save (context.Response.OutputStream, System.Drawing.Imaging.ImageFormat.Jpeg); / / Save the CAPTCHA object, specified in Jpeg format

}

}

}

/ / method of drawing noise

Void DrawPoint (int point,Graphics grecently Image img)

{

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

{

Int x = r.Next (img.Width)

Int y = r.Next (img.Width)

G.DrawLine (Pens.Red

New Point (x, y)

New Point (Xerox 2, YF2))

}

}

/ / Random number

Random r = new Random ()

/ / character creation

String GetCode (int point)

{

String txtStr = "ASF2345WE5R9F3HMBCZ455K"; / / the string string here will be converted into an char array, and the Arabic numeral 1 and the lowercase l had better not be written in it, it will be messy.

Char [] charArr = txtStr.ToArray ()

Int num = 0

String code = ""

For (int I = 0; I

Tags: Bitmap validation object character string picture generation code content more style painter knowledge background border color utility achievement next dilemma Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Docker vpn Shulou Information Redmi MySQL