C # generate a verification code,

Source: Internet
Author: User
Tags getcolor

C # generate a verification code,

Verification code generation class:

Using System; using System. collections. generic; using System. drawing; using System. text; namespace Controllers. core. util {// <summary> /// Verification Code /// </summary> public class VerifyCodeHelper: adminBaseController {# region variable // <summary> // Color table // </summary> private static Color [] colors = new Color [] {Color. fromArgb (, 60), Color. fromArgb (128, 0,), Color. fromArgb (1, 65,105,225), Color. fromArgb (70, 130,180), Color. fromArgb (46,139, 87), Color. fromArgb (184,134, 11), Color. fromArgb (255,140, 0), Color. fromArgb (139,69, 19), Color. fromArgb (1, 0,191,255), Color. fromArgb (1, 95,158,160), Color. fromArgb (255, 20, 147), Color. fromArgb (255,165, 0 )}; /// <summary> /// font table /// </summary> private static string [] fonts = new string [] {"Arial", "Verdana ", "Georgia", ""}; // <summary> // font size // </summary> private Static int fontSize = 22; # endregion # region generate the verification code image /// <summary> // generate the verification code image /// </summary> public static Bitmap CreateVerifyCodeBmp (out string code) {int width = 120; int height = 40; Bitmap bmp = new Bitmap (width, height); Graphics g = Graphics. fromImage (bmp); Random rnd = new Random (); // background color g. fillRectangle (new SolidBrush (Color. white), new Rectangle (0, 0, width, height); // text StringBuilder SbCode = new StringBuilder (); for (int I = 0; I <4; I ++) {string str = GetChar (rnd); Font font = GetFont (rnd ); color color = GetColor (rnd); g. drawString (str, font, new SolidBrush (color), new PointF (float) (I * width/4.0), 0); sbCode. append (str);} code = sbCode. toString (); // noise line for (int I = 0; I <10; I ++) {int x1 = rnd. next (bmp. width); int x2 = rnd. next (bmp. width); int y1 = rnd. next (bmp. he Ight); int y2 = rnd. next (bmp. height); Pen p = new Pen (GetColor (rnd), 1); g. drawLine (p, x1, y1, x2, y2);} // twist bmp = TwistImage (bmp, true, 3, rnd. nextDouble () * Math. PI * 2); g = Graphics. fromImage (bmp); // noise for (int I = 0; I <100; I ++) {int x1 = rnd. next (bmp. width); int y1 = rnd. next (bmp. height); Pen p = new Pen (GetColor (rnd), 1); g. drawRectangle (p, x1, y1, 1, 1);} // border g. drawRectangle (new Pe N (new SolidBrush (Color. fromArgb (153,153,153), new Rectangle (0, 0, width-1, height-1); return bmp ;} # endregion # region obtain Random characters /// <summary> // obtain Random characters /// </summary> private static string GetChar (Random rnd) {int n = rnd. next (0, 61); if (n <= 9) {return (char) (48 + n )). toString ();} else if (n <= 35) {return (char) (65 + n-10 )). toString ();} else {return (char) (97 + n-36 )). toStr Ing () ;}# endregion # region obtain Random fonts // <summary> // obtain Random fonts /// </summary> private static Font GetFont (Random rnd) {return new Font (fonts [rnd. next (0, fonts. length)], fontSize, FontStyle. bold) ;}# endregion # region obtain Random Color // <summary> // obtain Random Color /// </summary> private static Color GetColor (Random rnd) {return colors [rnd. next (0, colors. length)] ;}# endregion # region sine curve Wave distorted image // <summary>/ // Sine 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> private static System. drawing. bitmap TwistImage (Bitmap srcBmp, bool bXDir, double dMultValue, double dPhase) {System. drawing. bitmap destBmp = new Bitma P (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? (Math. PI * 2 * (double) j)/dBaseAxisLen: (Math. PI * 2 * (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 }}View Code

Verification code page Action:

Public ActionResult VerifyCode () {string code; Bitmap bmp = VerifyCodeHelper. createVerifyCodeBmp (out code); Bitmap newbmp = new Bitmap (bmp, 108, 36); HttpContext. session ["VerifyCode"] = code; Response. clear (); Response. contentType = "image/bmp"; newbmp. save (Response. outputStream, System. drawing. imaging. imageFormat. bmp); return View ();}View Code

Note: The cshtml page is blank on the front-end page. The value of the verification code is placed in the Session.

 

Verification code page:

The img that displays the verification code:

 

After the page is loaded, the verification code is displayed. (Note that a timestamp must be added. Otherwise, the verification code is not refreshed when the page is refreshed ):

$ (Function () {// refresh Verification Code $ ("# refreshVerifyCode "). click (function () {refreshVerifyCode (); // refresh Verification Code}); $ ("# verifyCode "). click (function () {refreshVerifyCode (); // refresh Verification Code}); refreshVerifyCode ();});

Refresh verification code:

// Refresh the verification code function refreshVerifyCode () {$ ("# verifyCode"). attr ("src", "VerifyCode? T = "+ new Date (). valueOf ());}

 

Determine whether the text entered by the user is the same as the Action of the Verification Code:

Public ActionResult CheckVCode (string vcode) {if (HttpContext. session ["VerifyCode"]. toString (). toLower () = vcode. toLower () {Dictionary <string, object> dic = new Dictionary <string, object> (); dic ["OK"] = true; return Content (JsonConvert. serializeObject (dic);} else {Dictionary <string, object> dic = new Dictionary <string, object> (); dic ["OK"] = false; return Content (JsonConvert. serializeObject (dic ));}}View Code

 

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.