WPF verification code, a small part of the original author's content, the original author of the wpf Verification Code
Address: http://www.cnblogs.com/tianguook/p/4142346.html
First of all, I would like to thank aparche for his post. because I may have to log on to the page in two days, I need to use the verification code to see this post from the great god. Thank you very much.
ValidCode
Public class ValidCode {public enum CodeType {Words, Numbers, Characters, Alphas} private const double PI = 3.1415926535897932384626433832795; private const double PI2 = 6.283185307179586476925286766559; private int _ len; private CodeType _ codetype; private readonly Single _ jianju = (float) 18.0; private readonly Single _ height = (float) 24.0; private string _ checkCode; public string CheckCode {g Et {return _ checkCode;} private string returnCode = ""; public string ReturnCode {get {return returnCode;} set {returnCode = value ;}} /// <summary> /// constructor /// </summary> /// <param name = "len"> Verification code length </param> /// <param name = "ctype"> Verification code type: letters, numbers, letters + numbers </param> public ValidCode (int len, CodeType ctype) {this. _ len = len; this. _ codetype = ctype;} private string GenerateNumbers () {Random ran Dom = new Random (); for (int I = 0; I <_ len; I ++) {string num = Convert. toString (random. next (10000) % 10); ReturnCode + = num;} return ReturnCode. trim ();} private string GenerateCharacters () {string num = ""; Random random = new Random (); for (int I = 0; I <_ len; I ++) {if (random. next (500) % 2 = 0) {num = Convert. toString (char) (65 + random. next (10000) % 26);} else {num = Convert. toStri Ng (char) (97 + random. next (10000) % 26);} ReturnCode + = num;} return ReturnCode. trim ();} private string GenerateAlphas () {string num = ""; Random random = new Random (); for (int I = 0; I <_ len; I ++) {if (random. next (500) % 3 = 0) {num = Convert. toString (random. next (10000) % 10);} else if (random. next (500) % 3 = 1) {num = Convert. toString (char) (65 + random. next (10000) % 26);} else {Num = Convert. toString (char) (97 + random. next (10000) % 26);} ReturnCode + = num;} return ReturnCode. trim ();} private Bitmap TwistImage (Bitmap srcBmp, bool bXDir, double dMultValue, double dPhase) {Bitmap destBmp = new Bitmap (srcBmp. width, srcBmp. height); // fill the bitmap background with white Graphics graph = Graphics. fromImage (destBmp); graph. fillRectangle (new SolidBrush (System. drawing. color. white), 0, 0, destBmp. wid Th, 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;} public Stream CreateCheckCodeImage () {string checkCode; switch (_ codetype) {case CodeType. alphas: checkCode = GenerateAlphas (); break; case CodeType. numbers: checkCode = GenerateNumbers (); break; case CodeType. characters: checkCode = GenerateCharacters (); break; default: checkCode = GenerateAlphas (); break;} this. _ checkCode = checkCode; MemoryStream MS = null; if (checkCode = null | checkCode. trim () = string. empty) return null; Bitmap image = new Bitmap (int) Math. ceiling (checkCode. length * _ jianju), (int) _ height); Graphics g = Graphics. fromImage (image); tr Y {Random random = new Random (); g. clear (Color. white); // specifies the background noise line of the image. for (int I = 0; I <18; I ++) {int x1 = random. next (image. width); int x2 = random. next (image. width); int y1 = random. next (image. height); int y2 = random. next (image. height); g. drawLine (new Pen (Color. fromArgb (random. next (), 1), x1, y1, x2, y2);} Font font = new Font ("Times New Roman", 14, FontStyle. bold); LinearGradientBrush = New LinearGradientBrush (new Rectangle (0, 0, image. Width, image. Height), Color. Blue, Color. DarkRed, 1.2f, true); if (_ codetype! = CodeType. words) {for (int I = 0; I <checkCode. length; I ++) {g. drawString (checkCode. substring (I, 1), font, brush, 2 + I * _ jianju, 1) ;}} else {g. drawString (checkCode, font, brush, 2, 2) ;}// foreground noise of the image to be painted for (int I = 0; I <150; I ++) {int x = random. next (image. width); int y = random. next (image. height); image. setPixel (x, y, Color. fromArgb (random. next ();} // draw the image's waveform filter effect if (_ codetype! = CodeType. words) {image = TwistImage (image, true, 3, 1) ;}// draw the border line g of the image. drawRectangle (new Pen (Color. silver), 0, 0, image. width-1, image. height-1); MS = new MemoryStream (); image. save (MS, ImageFormat. gif);} finally {g. dispose (); image. dispose ();} return MS ;}}
The modification content is as follows:
1. case-sensitive mixed random appearance
The original texts are in uppercase, while common verification codes are in uppercase and lowercase. Therefore, we wrote "num = Convert. toString (char) (97 + random. next (10000) % 26); "97 is the ASCII value of.
2. Add a returned property ReturnCode
The original text declares a strOut in each code segment, and the final result is an image. If you parse the image again, the loss will be worth the candle. Therefore, add an attribute and add the corresponding part, to facilitate the number of calls.
Call the CS code on the page and write a MouseLeftButtonUp event. You can change the verification code when you click the mouse.
Public partial class MainWindow: Window {ValidCode validCode; public MainWindow () {InitializeComponent (); validCode = new ValidCode (4, ValidCode. codeType. alphas); image1.Source = BitmapFrame. create (validCode. createCheckCodeImage ();} private void image=mouseleftbuttonup (object sender, MouseButtonEventArgs e) {validCode = new ValidCode (4, ValidCode. codeType. alphas); image1.Source = BitmapFrame. create (validCode. createCheckCodeImage ();} private void btn1_Click (object sender, RoutedEventArgs e) {if (txtbox1.Text. toUpper () = validCode. returnCode. toUpper () MessageBox. show ("correct Verification Code"); else MessageBox. show ("Incorrect verification code ");}}
Implementation