C # Generate good-looking verification code

Source: Internet
Author: User

Website Login always will use the verification code, generate verification code for C # is very simple, I use a general handler to write the verification code. The specific code is as follows:

Using System;
Using System.Collections.Generic;
Using System.Drawing;
Using System.Linq;
Using System.Web;

Namespace Ui.ajax
{
//<summary>
///Code Summary
//</summary>
public class Code:ihtt Phandler
{
//<summary>
//Authentication code type (0-alphanumeric mix, 1-digit, 2-letter)
//</summary>
private string Validatecodetype = "0";
public void ProcessRequest (HttpContext context)
{
context. Response.ContentType = "Text/plain";
Context. Response.bufferoutput = true;
Context. Response.Cache.SetExpires (DateTime.Now.AddMilliseconds (-1));
Context. Response.Cache.SetCacheability (Httpcacheability.nocache);
Context. Response.appendheader ("Pragma", "No-cache");
String validatecode = Generatecheckcode ();
//context. session["Checkcode"] = validatecode;//Saves the string to the session so that it can be validated when needed
Createcheckcodeimage (context, Validatecode);
}
public void Createcheckcodeimage (HttpContext context, string Checkcode)
{

int randangle = 40; Random Rotation angle
int mapwidth = (int) (CHECKCODE.LENGTH * 18);
Bitmap map = new Bitmap (mapwidth, 24);//Create picture background
Graphics graph = graphics.fromimage (map);
Graph. Clear (Color.White);//erase screen, fill background
Graph. DrawRectangle (New Pen (Color.lightgray, 0), 0, 0, map. Width-1, map. HEIGHT-1);//Draw a border

Random rand = new Random ();

Verification code rotation to prevent machine identification
char[] chars = Checkcode.tochararray ();//break string into a single character array
In text distance
StringFormat format = new StringFormat (stringformatflags.noclip);
Format. Alignment = Stringalignment.center;
Format. LineAlignment = Stringalignment.center;
Define Color
Color[] C = {color.black, color.red, Color.Blue, Color.green, Color.orange, Color.brown, color.darkblue};

Draw a picture of the background noise line
for (int i = 0; i < 2; i++)
{
int x1 = Rand. Next (10);
int x2 = rand. Next (map. WIDTH-10, map. Width);
int y1 = rand. Next (map. Height);
int y2 = rand. Next (map. Height);

Graph. DrawLine (New Pen (C[rand. Next (7)]), x1, y1, x2, y2);
}

for (int i = 0; i < chars. Length; i++)
{
int cindex = rand. Next (7);
int findex = rand. Next (5);
Font f = new System.Drawing.Font ("Arial", System.Drawing.FontStyle.Regular);//font style (parameter 2 is font size)
Brush B = new System.Drawing.SolidBrush (C[cindex]);
Point dot = new Point (12, 16);
float angle = rand. Next (-randangle, randangle);//degrees of rotation
Graph. TranslateTransform (dot. X, Dot. Y);//move cursor to specified position
Graph. RotateTransform (angle);
Graph. DrawString (Chars[i]. ToString (), F, B, 1, 1, format);
Graph. RotateTransform (-angle);//Turn Back
Graph. TranslateTransform (2,-dot. Y);//move cursor to specified position
}
Create a picture
System.IO.MemoryStream ms = new System.IO.MemoryStream ();
Map. Save (MS, SYSTEM.DRAWING.IMAGING.IMAGEFORMAT.GIF);
Context. Response.clearcontent ();
Context. Response.ContentType = "Image/gif";
Context. Response.BinaryWrite (Ms. ToArray ());
Graph. Dispose ();
Map. Dispose ();
}
private String Generatecheckcode ()
{
int number;
char code;
string checkcode = String.Empty;
System.Random random = new Random ();
for (int i = 0; i < 4; i++)
{
Number = random. Next ();
Code = (char) (' 0 ' + (char) (number% 10));
if (number% 2 = = 0)
Code = (char) (' 0 ' + (char) (number% 10));
Else
Code = (char) (' A ' + (char) (number% 26));
Remove 0,o,i,o,i,z,z
if (code. ToString () = = "O" | | Code. ToString () = = "I" | | Code. ToString () = = "O" | | Code. ToString () = = "I" | | Code. ToString () = = "Z" | | Code. ToString () = = "Z")
{
i--;
Continue
}
All numbers are required.
if (Validatecodetype = = "1")
{
if ((int) code < 48 | | (int) code > 57)
{
i--;
Continue
}
}
All letters are required.
if (Validatecodetype = = "2")
{
if ((int) code > 47 | | (int) Code < 58)
{
i--;
Continue
}
}
Checkcode + = code. ToString ();
}
return checkcode;
}


public bool IsReusable
{
Get
{
return false;
}
}
}
}

The specific effect is:

C # Generate good-looking verification code

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.