The verification code that I feel good (the code is not written by myself)

Source: Internet
Author: User
Tags rand
I feel the verification Code, verification code, not written by myself, I hereby release, please forgive the author
The class of the CAPTCHA code, placed under the App_Code folder
Using System;
Using System.Web;
Using System.IO;
Using System.Drawing;
/**/
<summary>
Summary description of the Verifycode.
</summary>
public class Verifycode
{
Properties

#region the length of the authentication code (the length of the default 4 Authenticode) #region The Verification code length (the length of the default 4 Authenticode)
int length = 4;
public int Length
{
get {return length;}
set {length = value;}
}
#endregion

#region Authenticode Font size (default 12 pixels) #region Authenticode font size (default 12 pixels)
int fontsize = 12;
public int FontSize
{
get {return fontsize;}
set {fontsize = value;}
}
#endregion

#region Border complement (default 2 pixels) #region Border complement (default 2 pixels)
int padding = 2;
public int Padding
{
get {return padding;}
set {padding = value;}
}
#endregion

#region whether to output dryness point (default does not output) #region output dryness point (default does not output)
BOOL chaos = false;
public bool Chaos
{
get {return chaos;}
set {chaos = value;}
}
#endregion

#region the color of the output dryness point (default gray) #region The color of the output dryness point (default gray)
Color chaoscolor = Color.lightgray;
Public Color Chaoscolor
{
get {return chaoscolor;}
set {Chaoscolor = value;}
}
#endregion

#region Custom background color (default white) #region Custom background color (default white)
Color backgroundcolor = Color.White;
Public Color BackgroundColor
{
get {return backgroundcolor;}
set {backgroundcolor = value;}
}
#endregion

#region custom Random color array #region custom random color array
color[] Colors = {color.black, color.red, Color.darkblue, Color.green, Color.orange, Color.brown, Color.darkcyan, Color . Purple};
Public color[] Colors
{
get {return colors;}
set {colors = value;}
}
#endregion

#region Custom font array #region custom font array
string [] fonts = {"Arial", "Georgia"};
public string [] Fonts
{
get {return fonts;}
set {fonts = value;}
}
#endregion

#region custom Random code string sequences (separated by commas) #region a sequence of custom random code strings (separated by commas)
String codeserial = "0,1,2,3,4,5,6,7,8,9,a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z,a,b,c,d,e,f,g,h,i,j, K,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z ";
public string codeserial
{
get {return codeserial;}
set {codeserial = value;}
}
#endregion

#region Generate check code picture #region Generate check code picture
/**/
<summary>
Generate Check code picture
</summary>
<param name= "code" > already generated random code </param>
<returns> Bitmap </returns>
Public Bitmap Createimagecode (string code)
{
int fsize = fontsize;
int fwidth = fsize + Padding;

int imagewidth = (int) (code. Length * fwidth) + 4 + Padding * 2;
int imageheight = fsize * 2 + Padding;

System.Drawing.Bitmap image = New System.Drawing.Bitmap (imagewidth, imageheight);

Graphics g = graphics.fromimage (image);

G.clear (BackgroundColor);

Random rand = new Random ();

Add a randomly generated dryness point to the background
if (this. Chaos)
{

Pen pen = new Pen (chaoscolor, 0);
int c = Length * 10;

for (int i = 0; i < C; i + +)
{
int x = rand. Next (image. Width);
int y = rand. Next (image. Height);

G.drawrectangle (pen, x, Y, 1, 1);
}
}

int left = 0, top = 0, Top1 = 1, top2 = 1;

int n1 = (imageheight-fontsize-padding * 2);
int n2 = N1/4;
Top1 = n2;
Top2 = n2 * 2;

Font F;
Brush b;

int CIndex, Findex;

Validation code characters for random fonts and colors
for (int i = 0; i < code. Length; i + +)
{
CIndex = Rand. Next (colors.length-1);
Findex = Rand. Next (fonts.length-1);

f = new System.Drawing.Font (Fonts[findex], fsize, System.Drawing.FontStyle.Bold);
b = new System.Drawing.SolidBrush (Colors[cindex]);

if (i% 2 = 1)
{
top = TOP2;
}

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.