C # code generation

Source: Internet
Author: User

Code:

Using System;
Using System. Data;
Using System. Configuration;
Using System. Collections;
Using System. Web;
Using System. Web. Security;
Using System. Web. UI;
Using System. Web. UI. WebControls;
Using System. Web. UI. WebControls. WebParts;
Using System. Web. UI. HtmlControls;
Using System. Drawing;
Public partial class inc_ValidCode: System. Web. UI. Page
{
Protected void Page_Load (object sender, EventArgs e)
{
VerifyCode v = new VerifyCode ();
V. Length = 4;
V. FontSize = 14;
V. Chaos = true;
V. BackgroundColor = Color. White;
V. ChaosColor = Color. LightGray;
// V. 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 ";
// V. colors = this. colors; '''''''''''''''''''''''''''''''''''''''' '''''''''''''''''''''''''''''''''''''''' '''''''''''''''''''''''''''''''''''''''' ''''''''''''''''''''''''''''''''''
// V. Fonts = this. fonts;
V. Padding = 2;
String code = v. CreateVerifyCode (); // obtain the random code
V. CreateImageOnPage (code, this. Context); // output image
// Response. Cookies. Add (new HttpCookie ("fcvalidtcode", code. ToUpper (); // use Cookies to obtain the verification code value
Session ["bao_code"] = code. ToUpper ();
}
Public class VerifyCode
{
# Region verification code length (6 Verification codes by default) # region verification code length (6 Verification codes by default)
Int length = 6;
Public int Length
{
Get {return length ;}
Set {length = value ;}
}
# Endregion
# Region Verification Code font size (40 pixels by default for displaying distortion effects) # region Verification Code font size (40 pixels by default for displaying distortion effects)
Int fontSize = 30;
Public int FontSize
{
Get {return fontSize ;}
Set {fontSize = value ;}
}
# Endregion
# Region border population (1 pixel by default) # 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 dryness 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
{Www.jbxue.com
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 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 = 3.1415926535897932384626433832795;
Private const double pi2= 6.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 ++)
{// Www.jbxue.com
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 generating Verification Code image # region generating a 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, 0, 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 a random Verification 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
}
}

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.