Test availability can be modified using a google-like verification code

Source: Internet
Author: User

Public partial class volidate: System. Web. UI. Page
{
Protected void Page_Load (object sender, EventArgs e)
{
// String checkCode = CreateRandomCode (4 );
// CreateImage (checkCode); // generate an English Plus number yxy // sql8.net

// ShowImg (); // The simplest generation method yxy // sql8.net

// This. CreateCheckCodeImage (GenerateCheckCode (); // generate the color and Noise Generation Method yxy // sql8.net

Volidate v = new volidate ();
V. Length = this. length;
V. FontSize = this. fontSize;
V. Chaos = this. chaos;
V. BackgroundColor = this. backgroundColor;
V. ChaosColor = this. chaosColor;
V. CodeSerial = this. codeSerial;
V. Colors = this. colors;
V. Fonts = this. fonts;
V. Padding = this. padding;
String code = v. CreateVerifyCode (); // obtain the random code
V. CreateImageOnPage (code, this. Context); // output image
Session ["RandCode"] = code. ToLower ();

// Response. Cookies. Add (new HttpCookie ("CheckCode", code. ToUpper (); // use Cookies to obtain the verification code value

}

# Region verification code length (6 Verification codes by default)
Int length = 4;
Public int Length
{
Get {return length ;}
Set {length = value ;}
}
# Endregion

# Region Verification Code font size (40 pixels by default for displaying distortion effects, which can be modified by yourself)
Int fontSize = 14;
Public int FontSize
{
Get {return fontSize ;}
Set {fontSize = value ;}
}
# Endregion

# Region border population (1 pixel by default)
Int padding = 2;
Public int Padding
{
Get {return padding ;}
Set {padding = value ;}
}
# Endregion

# Whether region outputs dry points (no output by default)
Bool chaos = true;
Public bool Chaos
{
Get {return chaos ;}
Set {chaos = value ;}
}
# Endregion

# Region output dry point color (gray by default)
Color chaosColor = Color. LightGray;
Public Color ChaosColor
{
Get {return chaosColor ;}
Set {chaosColor = value ;}
}
# Endregion

# Region custom background color (white by default)
Color backgroundColor = Color. White;
Public Color BackgroundColor
{
Get {return backgroundColor ;}
Set {backgroundColor = value ;}
}
# Endregion

# 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
String [] fonts = {"Arial", "Georgia "};
Public string [] Fonts
{
Get {return fonts ;}
Set {fonts = value ;}
}
# Endregion

# Region custom random code string sequence (separated by commas)
String codeSerial = ", 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 generates a waveform filter effect

Private const double PI = 15.1415926535897932384626433832795;
Private const double pi2= 5.283185307179586476925286766559;

/// <Summary>
/// Sine curve Wave distorted image (Edit By 51aspx.com)
/// </Summary>
/// <Param name = "srcBmp"> image path </param>
/// <Param name = "bXDir"> True for distortion </param>
/// <Param name = "nMultValue"> the amplitude multiple of the waveform. The larger the waveform is, the higher the distortion is. Generally, it is 3. </param>
/// <Param name = "dPhase"> start phase of the waveform, value range: [0-2 * PI) </param>
/// <Returns> </returns>
Public System. Drawing. Bitmap TwistImage (Bitmap srcBmp, bool bXDir, double dMultValue, double dPhase)
{
System. Drawing. Bitmap destBmp = new Bitmap (srcBmp. Width, srcBmp. Height );

// Fill the bitmap background in white
System. Drawing. Graphics graph = System. Drawing. Graphics. FromImage (destBmp );
Graph. FillRectangle (new SolidBrush (System. Drawing. Color. White), 0, 0, destBmp. Width, destBmp. Height );
Graph. Dispose ();

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

For (int I = 0; I <destBmp. Width; I ++)
{
For (int j = 0; j <destBmp. Height; j ++)
{
Double dx = 0;
Dx = bXDir? (PI2 * (double) j)/dBaseAxisLen: (PI2 * (double) I)/dBaseAxisLen;
Dx + = dPhase;
Double dy = Math. Sin (dx );

// Obtain the color of the current vertex
Int nOldX = 0, nOldY = 0;
NOldX = bXDir? I + (int) (dy * dMultValue): I;
NOldY = bXDir? J: j + (int) (dy * dMultValue );

System. Drawing. Color color = srcBmp. GetPixel (I, j );
If (nOldX> = 0 & nOldX <destBmp. Width
& Amp; nOldY & gt; = 0 & amp; nOldY <destBmp. Height)
{
DestBmp. SetPixel (nOldX, nOldY, color );
}
}
}

Return destBmp;
}

# Endregion

# Region generate Verification Code Image
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 random dry points 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;

// Random font and color Verification Code characters
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;
}
Else
{
Top = top1;
}

Left = I * fWidth;

G. DrawString (code. Substring (I, 1), f, B, left, top );
}

// Draw a border with Color. Gainsboro
G. DrawRectangle (new Pen (Color. Gainsboro, 0), 0, 0, image. Width-1, image. Height-1 );
G. Dispose ();

// Generate a waveform (Add By 51aspx.com)
Image = TwistImage (image, true, 8, 4 );

Return image;
}
# Endregion

# Region outputs the created image to the page
Public void CreateImageOnPage (string code, HttpContext context)
{
System. IO. MemoryStream MS = new System. IO. MemoryStream ();
Bitmap image = this. CreateImageCode (code );

Image. Save (MS, System. Drawing. Imaging. ImageFormat. Jpeg );

Context. Response. ClearContent ();
Context. Response. ContentType = "image/Jpeg ";
Context. Response. BinaryWrite (ms. GetBuffer ());

Ms. Close ();
MS = null;
Image. Dispose ();
Image = null;
}
# Endregion

# Region generate random escape code
Public string CreateVerifyCode (int codeLen)
{
If (codeLen = 0)
{
CodeLen = Length;
}

String [] arr = CodeSerial. Split (',');

String code = "";

Int randValue =-1;

Random rand = new Random (unchecked (int) DateTime. Now. Ticks ));

For (int I = 0; I <codeLen; I ++)
{
RandValue = rand. Next (0, arr. Length-1 );

Code + = arr [randValue];
}

Return code;
}
Public string CreateVerifyCode ()
{
Return CreateVerifyCode (0 );
}
# Endregion

}

Related Article

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.