Joke Collection Website - Blessing messages - How does ASP.NET design a simple verification code?

How does ASP.NET design a simple verification code?

Create a new Image.aspx page without adding any elements to the foreground. The background code is

Static read-only string str = "0123456789 abcdefghijklmnopqrstuvwxyzabc defghijklmnopqrstuvwxyXYZ";

Protected void Page_Load (object sender, EventArgs e)

{

Random rd = new random ();

int a = rd。 Next (100);

int b = rd。 Next (1000);

int m = rd。 Next (10000);

int n = rd。 Next (100000);

String result = STR. substring(a % 62, 1);

Result+= string. substring(b % 62, 1);

Result+= string. substring(m % 62, 1);

Result+= string. substring(n % 62, 1);

session【“check“】= result;

CreateImage (result);

}

///& lt; Summary & gt

///Create a verification picture of the specified content and output it.

///& lt; /summary & gt;

///& lt; param name =“content“& gt; & lt/param & gt;

Private void CreateImage (string content)

{

//The judgment string is not equal to null and null.

if(content = = null | | content。 trim()= = String。 Empty)

Return;

//Create a bitmap object

Bitmap image = new bitmap ((int) math. Ceiling ((content. Length * 12.5)), 22);

//Create a graph

Graph g = graph. FromImage;

attempt

{

//Generate a random generator

Random Random = new Random();

//Clear the background color of the picture.

G. transparent (color. White);

//Draw the background interference line of the picture.

for(int I = 0; I<2; i++)

{

int x 1 = random。 Next (picture. Width);

Int x2 = random. Next (picture. Width);

int y 1 = random。 Next (picture. Height);

Int y2 = random. Next (picture. Height);

G. new pen (color. Black), x 1, y 1, x2, y2);

}

Font font = new font ("Arial", 12, (FontStyle. Bold);

System. Drawing.drawing2d.lineargradientbrush = new system. Drawing.drawing2d.lineargradientbrush (new rectangle (0,0, image). Width, image. Height), color. Black, color. DarkRed, 1.2f,true);

G. draw rope (content, font, brush, 2, 2);

//Draw the foreground interference point of the picture

for(int I = 0; I< 100; i++)

{

int x = random。 Next (picture. Width);

int y = random。 Next (picture. Height);

Image. set pixel(x,y,Color。 from argb(random。 next()));

}

image = twist image(image,true,4,4);

//Draw the border line of the picture

G.DrawRectangle (new pen (color. Silver), 0, 0, image. Width-1, image. Height-1);

//output the picture to the page

System. IO. memory flow ms = new system. IO . memory stream();

Image. Save (ms, system. drawing . imaging . imageformat . gif);

Response. clear content();

Response. content type =“image/Gif“;

Response. binary write(ms . ToArray());

}

finally

{

g.dispose();

Image. dispose();

}

}

# region produces filter effect.

private const double PI = 3. 14 1592653589793238462643832795;

private const double PI2 = 6.283 185307 17958647692528676559;

///& lt; Summary & gt

///sine wave distortion picture

///& lt; /summary & gt;

///& lt; param name =“srcBmp“& gt; Image path

///& lt; param name =“bXDir“& gt; If it is distorted, please select True.

///& lt; param name =“nMultValue“& gt; The greater the amplitude multiple of the waveform, the higher the distortion, usually 3.

///& lt; param name =“d phase“& gt; Initial phase of waveform, value interval [0-2 * pi]

///& lt; Returns & gt& lt/returns & gt;;

Common bitmap TwistImage (bitmap srcBmp, boolbdxdir, double dMultValue, double dPhase)

{

Bitmap destBmp = new bitmap (srcBmp. Width, srcBmp. Height);

//Fill the bitmap background with white.

System. Drawing. Graphics graphics = graphics. from image(dest BMP);

Charts. FillRectangle (new SolidBrush (color. White), 0,0, destBmp. Width, destBmp. Height);

Charts. dispose();

double dBaseAxisLen = bXDir? (double) destBmp. Height: (double) destBmp. Width;

for(int I = 0; Me & ltdestBmp. Width; i++)

{

for(int j = 0; j & ltdestBmp。 Height; j++)

{

double dx = 0;

dx = bXDir? (PI2 *(double)j)/dBaseAxisLen:(PI2 *(double)I)/dBaseAxisLen;

dx+= d phase;

Double dy = math. sin(dx);

//Get the color of the current point.

int nOldX = 0,nOldY = 0;

nOldX = bXDir? I+(int)(dy * dMultValue):I;

nOldY = bXDir? j:j+(int)(dy * dMultValue);

System. Drawing. Color color = srcBmp。 get pixel(I,j);

if(nOldX & gt; = 0 & amp& ampnOldX & ltdestBmp。 width

& amp& ampnOldY & gt= 0 & amp& ampnOldY & ltdestBmp。 Height)

{

destBmp。 set pixel(nOldX,nOldY,color);

}

}

}

return destBmp

}

# End area

When referenced on another page,

& ltimg id = "img" alt = "verification code" src = "image.aspx"/>.

& ltinput type =“button“onclick =“JavaScript:var img = document . getelementbyid(‘img‘); img . src = img . src+‘? ‘。 ; "value=" I can't see clearly, change it to "/>

In your registration button, you can use the session [Check] to determine whether the verification code is correct.