asp.net generate authentication Code source code

Source: Internet
Author: User
The code is as follows Copy Code

<%@ WebHandler language= "C #" class= "Verifycode"%>

Using System;
Using System.Web;
Using system.web.sessionstate;//the first step to import namespaces
Using System.Drawing;

public class Verifycode:ihttphandler, IRequiresSessionState
{//Step two to implement the interface as usual you can use session

public void ProcessRequest (HttpContext context)
{
String Checkcode = this. Createrandomcode (4). ToLower ();
Context. session["Checkcode"] = Checkcode;
This. CreateImage (context, checkcode);
}

public bool IsReusable
{
Get
{
return false;
}
}

<summary>
Generate random by bit
</summary>
<param name= "Codecount" ></param>
<returns></returns>
private string Createrandomcode (int codecount)
{
int number;
string checkcode = String.Empty;
Random Random = new Random ();
for (int i = 0; i < Codecount; i++)
{
Number = random. Next (100);
Switch (number% 3)
{
Case 0:
Checkcode + = ((char) (' 0 ' + (char) (number% 10)). ToString ();
Break
Case 1:
Checkcode + = ((char) (' A ' + (char) (number% 26)). ToString ();
Break
Case 2:
Checkcode + = ((char) (' A ' + (char) (number% 26)). ToString ();
Break
Default
Break
}
}
return checkcode;
}

<summary>
Generate pictures from characters
</summary>
<param name= "Context" ></param>
<param name= "Checkcode" ></param>
private void CreateImage (HttpContext context,string Checkcode)
{
int randangle = 45;//random rotation angle
int iwidth = (int) (CHECKCODE.LENGTH * 23);
Encapsulates a GDI + bitmap, which consists of the pixel data of a graphics image and its attributes, specifying the width and height. In pixels
System.Drawing.Bitmap image = New System.Drawing.Bitmap (iwidth, 28);

Encapsulates a GDI + drawing surface. This class cannot be inherited. Creates a new Graphics from the specified image
Graphics g = graphics.fromimage (image);

Clears the entire drawing surface and fills with the specified background
G.clear (Color.aliceblue);

Draw a border
G.drawrectangle (New Pen (Color.Black, 0), 0, 0, image. Width-1, image. HEIGHT-1);

Defines the objects that draw lines and curves. (Just the color of the pen, indicating the value of the pen's width)
Pen Blackpen = new Pen (color.lightgray, 0);

Random rand = new Random ();

The number of lines can be drawn according to your own requirements

for (int i = 0; i < i++)
{
Random height
int y = rand. Next (image. Height);
/* Draws a line with two points specified by the coordinate pair
Line color, width, and style, the x and Y coordinates of the first point, and the x and Y coordinates of the second point.
G.drawline (blackpen, 0, y, image. Width, y);
int x = rand. Next (0, image. Width);
int y = rand. Next (0, image. Height);
Draw rectangle, coordinate (x,y) width (1,1)
G.drawrectangle (Blackpen, x, Y, 1, 1);
}

To break a string into a single character array
char[] chars = Checkcode.tochararray ();

Center text
StringFormat format = new StringFormat (stringformatflags.noclip);
Format. Alignment = Stringalignment.center;
Format. LineAlignment = Stringalignment.center;

Define Color
Color[] C = {color.black, Color.darkgray, Color.darkorange, Color.Red, Color.darkblue, Color.green, Color.Orange, Color. Brown, Color.darkcyan, color.purple};
Defining fonts
string[] Font = {"Verdana", "Microsoft Sans Serif", "Comic Sans MS", "Arial", "Song Body", "Arial Baltic"};

for (int i = 0; i < chars. Length; i++)
{
int cindex = rand. Next (c.length);
int findex = rand. Next (font. Length);

Font encapsulates the textures and resources (font, size, font style) that are required to render a particular font on a specific device
Font f = new System.Drawing.Font (Font[findex], System.Drawing.FontStyle.Bold);

/*brush defines internal objects for populating graphic images (such as rectangles, ellipses, circles, polygons, and closed paths)
SolidBrush (Color.White) initializes the specified color to specify a brush color of white * *
Brush B = new System.Drawing.SolidBrush (C[cindex]);

Point dot = new Point (16, 16);

Degrees of rotation
float angle = rand. Next (-randangle, Randangle);

Move the cursor to the specified position
G.translatetransform (dot. X, Dot. Y);
G.rotatetransform (angle);
/* The specified text string is drawn at the specified location and with the specified brush and font objects
(The specified string, the text format of the string, the color and texture of the text drawn, the x-coordinate of the upper-left corner of the drawn text, coordinates) * *
g.DrawString (Chars[i]. ToString (), F, B, 1, 1, format);
Turn back.
G.rotatetransform (-angle);
Move cursor Specify position
G.translatetransform (2,-dot. Y);
}
Create a store as a memory stream
System.IO.MemoryStream ms = new System.IO.MemoryStream ();

Saves this image to the specified stream in the specified format (saves it in the memory stream, in the format of the image)
Image. Save (MS, System.Drawing.Imaging.ImageFormat.Jpeg);

Clears the buffer to output the content in the stream
Context. Response.clearcontent ();

Gets the type of the output stream
Context. Response.ContentType = "Image/jpeg";

Writes a binary string to the HTTP output stream
Context. Response.BinaryWrite (Ms. ToArray ());

G.dispose ();

Image. Dispose ();
}
}

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.