Using System; Using System.Collections.Generic; Using System.ComponentModel; Using System.Data; Using System.Drawing; Using System.Linq; Using System.Text; Using System.Windows.Forms; namespace Validatecode {public partial class Login:form {//Random code length Private Const int codelength = 6;//Random Code private Stri ng Randomcode = ""; Random code corresponding to the picture public Login () {InitializeComponent (); Updatepic ();}/update Authenticode private void Updatepic () {Randomcode = Createra Ndomcode (codelength); CreateImage (Randomcode); private string Createrandomcode (int length) {int rand; Char code; string randomcode = String.Empty;//Generate a certain length of captcha System . Random Random = new Random (); for (int i = 0; i < length; i++) {rand = random. Next (); If (rand% 3 = 0) {code = (char) (' A ' + (char) (rand%));} else {code = (char) (' 0 ' + (char) (% rand));} Randomcode + = code. ToString (); return randomcode; ///create random code picture private void CreateImage (string randomcode) {try {int randangle = 45;//random rotation angle int mapwidth = (int) (random Code.lEngth * 21); Bitmap map = new Bitmap (Mapwidth, 28);//Create picture background Graphics graph = graphics.fromimage (map); Graph. Clear (Color.aliceblue);//erase screen, fill background graph. DrawRectangle (New Pen (Color.Black, 0), 0, 0, map. Width-1, map. HEIGHT-1);//Draw a border graph. SmoothingMode = system.drawing.drawing2d.smoothingmode.antialias;//mode Random rand = new Random (); Background noise generation pen Blackpen = new Pen (color.lightgray, 0); for (int i = 0; i < i++) {int x = rand. Next (0, map. Width); int y = rand. Next (0, map. Height); Graph. DrawRectangle (Blackpen, x, Y, 1, 1); }//Verify code rotation to prevent machine recognition char[] chars = Randomcode.tochararray ();//split string into single character array//text distance StringFormat format = new StringFormat (stri Ngformatflags.noclip); Format. Alignment = Stringalignment.center; Format. LineAlignment = Stringalignment.center; Define color color[] c = {color.black, color.red, Color.darkblue, Color.green, Color.orange, Color.brown, Color.darkcyan, color . Purple}; Define font string[] Font = {"Verdana", "Microsoft Sans Serif", "Comic Sans MS", "Arial", "Song Body"}; for (int i = 0; I < chars. Length; i++) {int cindex = rand. Next (7); int findex = rand. Next (5); Font f = new System.Drawing.Font (Font[findex], System.Drawing.FontStyle.Bold);//font style (parameter 2 is font size) Brush B = new SYSTEM.D Rawing. SolidBrush (C[cindex]); Point dot = new Point (16, 16); float angle = rand. Next (-randangle, randangle);//rotation degree graph. TranslateTransform (dot. X, Dot. Y);//move the cursor to the specified position graph. RotateTransform (angle); Graph. DrawString (Chars[i]. ToString (), F, B, 1, 1, format); Graph. RotateTransform (-angle);/Turn back to graph. TranslateTransform (2,-dot. Y);/move cursor to specified position} pbverifycode.image = map; The catch (ArgumentException) {MessageBox.Show ("Create a picture error.") "); } } private void Btlogin_click (object sender, EventArgs e) {if (Randomcode.equals (TbVerifyCode.Text.Trim ())) {if (Tbusernam E.text.trim (). Equals ("Stdmanager") && TbPassword.Text.Trim (). Equals ("Stdmanager")) {MessageBox.Show ("Welcome to the online bookstore management system.") "); } } else {MessageBox.Show ("Authentication code error, please re-enter.", "input Error"); Updatepic (); tbverifycode.text = ""; Tbverifycode.focus (); } private void Pbverifycode_click (object sender, EventArgs e) {updatepic ();} private void Btexit_click (object sender, EventArgs e) {if MessageBox.Show (OK exit? ", Exit", Messageboxbuttons.okcancel, messageboxicon.exclamation) = = DialogResult.OK) {application.exit ();}} } }