Generate a 6-Digit Image verification code and a 6-digit verification code.

Source: Internet
Author: User

Generate a 6-Digit Image verification code and a 6-digit verification code.

/// <Summary> /// abstract description of PicHandler1 /// </summary> public class PicHandler1: IHttpHandler, IRequiresSessionState {private string mCheckNo = string. empty; protected ImgBuilder _ ImgBuilder = new ImgBuilder (); protected VryImgGen _ ImgBuilderNew = new VryImgGen (); private string _ text = string. empty; private string _ font = ""; private int _ fontSize = 8; private int _ padding = 2; public void Process Request (HttpContext context) {mCheckNo = DisCheckNo (); context. session ["CheckCode"] = mCheckNo; this. _ ImgBuilder. fontSize = this. _ fontSize; this. _ ImgBuilder. padding = this. _ padding; if (! String. isNullOrEmpty (this. _ font) {this. _ ImgBuilder. fonts = new string [] {this. _ font};} this. _ ImgBuilderNew. chaosWight = 40; this. _ ImgBuilderNew. fontSize = 25; this. _ ImgBuilderNew. padding = 3; System. drawing. bitmap image = this. _ ImgBuilderNew. createImage (mCheckNo); System. IO. memoryStream MS = new System. IO. memoryStream (); image. save (MS, System. drawing. imaging. imageFormat. jpeg); context. response. clearContent (); context. response. contentType = "image/Jpeg"; context. response. binaryWrite (ms. toArray (); context. session ["CheckCode"] = mCheckNo. toString (); // If the IRequiresSessionState is not implemented, an error occurs here and the context of the image cannot be generated. response. end () ;}// the verification code generates protected string DisCheckNo () {string hash = HashCode. getNext (); string CheckNo = string. empty; Random rd = new Random (DateTime. now. millisecond); for (int I = 0; I <6; I ++) {CheckNo + = hash. substring (rd. next (1, hash. length-1), 1);} CheckNo = CheckNo. replace ("0", rd. next (1, 9 ). toString (); CheckNo = CheckNo. replace ("o", rd. next (1, 9 ). toString (); CheckNo = CheckNo. replace ("O", rd. next (1, 9 ). toString (); return CheckNo;} public bool IsReusable {get {return false ;}}}

  

Called class

 

Using System; using System. drawing; using System. text; // <summary> /// summary of VryImgGen /// </summary> public class VryImgGen {public static string ChineseChars = String. empty; // <summary> // english and numeric string // </summary> protected static readonly string EnglishOrNumChars = "0123456789 bytes"; public VryImgGen () {rnd = new Random (unchecked (int) DateTime. now. tic Ks);} // <summary> // global Random number generator // </summary> private Random rnd; int length = 5; /// <summary >/// verification code Length (default length of 6 Verification codes) /// </summary> public int Length {get {return length ;} set {length = value ;}} int fontSize = 20; // <summary> // The font size of the Verification Code (30 pixels by default for displaying the distortion effect) /// </summary> public int FontSize {get {return fontSize;} set {fontSize = value ;}} int padding = 4; // <summary> /// Border population (4 pixels by default) /// </summary> public int Padding {get {return padding;} set {padding = value ;}} bool chaos = true; /// <summary> /// whether to output dry points (default output) /// </summary> public bool Chaos {get {return chaos ;} set {chaos = value ;}} Color chaosColor = Color. lightGray; // <summary> // the Color of the output dry point (gray by default) /// </summary> public Color ChaosColor {get {return chaosColor ;} set {chaosColor = value ;}} int ChaosWight = 1; // <summary> // output the dry point concentration /// </summary> public int ChaosWight {get {return chaosWight ;} set {chaosWight = value;} Color backgroundColor = Color. white; // <summary> /// custom background Color (White by default) /// </summary> public Color BackgroundColor {get {return backgroundColor ;} set {backgroundColor = value;} Color [] colors = {Color. black, Color. red, Color. darkBlue, Color. green, Color. ora Nge, Color. brown, Color. darkCyan, Color. purple}; // <summary> // custom random Color array // </summary> public Color [] Colors {get {return colors ;} set {colors = value ;}} string [] fonts = {"Arial", "Georgia "}; /// <summary> /// Custom font array /// </summary> public string [] Fonts {get {return fonts;} set {fonts = value ;}} # region generate waveform filter effect private const double PI = 3.1415926535897932384626433832795; pri Vate 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"> If the image is distorted, select true </param> /// <param name = "nMultValue"> waveform amplitude multiple, the greater the distortion, the higher the distortion. Generally, the start phase of the waveform is 3 </param> // <param name = "dPhase">. The value range is [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 with 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 & nOldY> = 0 & nOldY <destBmp. height) {destBmp. setPixel (nOldX, nOldY, color) ;}} return destBmp ;} # endregion // <summary> // generate the verification code image // </summary> // <param name = "code"> Verification code </param> /// <returns> </returns> public Bitmap CreateImage (string code) {int fSize = FontSize; int fWidth = fSize + Padding; int imageWidth = (int) (code. length * fWidth) + 4 + Padding * 2; int imageHeight = fSize * 2 + Padding * 2; System. drawing. bitmap image = new System. drawing. bitmap (imageWidth-10, imageHeight-10); Graphics g = Graphics. fromImage (image); g. clear (BackgroundColor); // Add randomly generated dry points to the background if (this. chaos) {Pen pen = new Pen (ChaosColor, 0); int c = ChaosWight * 10; for (int I = 0; I <c; I ++) {int x = rnd. next (image. width); int y = rnd. 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; // the character of the random font and color verification code for (int I = 0; I <code. length; I ++) {cindex = rnd. next (Colors. length-1); findex = rnd. 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 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 ;} /// <summary> /// generate a random escape code /// </summary> /// <param name = "codeLen"> String Length </param> /// <param name = "zhCharsCount"> Number of Chinese characters </param> // <returns> </returns> public string CreateVerifyCode (int codeLen, int zhCharsCount) {char [] chs = new char [codeLen]; int index; for (int I = 0; I <zhCharsCount; I ++) {index = rnd. next (0, codeLen); if (chs [index] = '\ 0') chs [index] = CreateZhChar (); else -- I ;} for (int I = 0; I <codeLen; I ++) {if (chs [I] = '\ 0') chs [I] = CreateEnOrNumChar ();} return new string (chs, 0, chs. length );} /// <summary> /// generate a random escape code with the default length of 5 /// </summary> /// <returns> </returns> public string CreateVerifyCode () {return CreateVerifyCode (Length, 0 );} /// <summary> /// generate English or numeric characters /// </summary> /// <returns> </returns> protected char CreateEnOrNumChar () {return EnglishOrNumChars [rnd. next (0, EnglishOrNumChars. length)] ;}/// <summary> /// generate Chinese characters /// </summary> /// <returns> </returns> protected char CreateZhChar () {// if a Chinese character set is provided, select if (ChineseChars. length> 0) {return ChineseChars [rnd. next (0, ChineseChars. length)];} // if no Chinese Character Set is provided, else {byte [] bytes = new byte [2] is constructed based on the encoding rules of GB2312 simplified Chinese character encoding table. // The value of the first byte is between 0xb0 and 0xf7. bytes [0] = (byte) rnd. next (0xb0, 0xf8); // the value of the second byte is between 0xa1 and 0xfe. bytes [1] = (byte) rnd. next (0xa1, 0xff); // decodes the Chinese character string str1 = Encoding Based on the byte array encoded by Chinese characters. getEncoding ("gb2312 "). getString (bytes); return str1 [0] ;}}

  

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.