How to implement a window verification code in the WinForm login form of C #

Source: Internet
Author: User

C # WinForm can develop QQ chat program, chat login Form Verification code is how to achieve it? In WinForm, you need to add your own control to a PictureBox control pbverifycode a button Butupdateverifycode code as follows:  public partial  class frmlogin : form    {        / /Random Code length         private const int iverifycodelength  = 6;        //Random Code          private String strVerifyCode =  "";         Public frmlogin ()         {             initializecomponent ();             updateverifycode ();        }         //Update Verification Code          privatE void updateverifycode ()         {             strverifycode = createrandomcode (iVerifyCodeLength);             createimage (StrVerifyCode);         }        private string  createrandomcode (int ilength)         {             int rand;             char code;             string randomCode = String.Empty;             //generate a certain length of verification code              System.random random&nbsP;= new random ();            for  ( int i = 0; i < ilength; i++)              {                 rand = random. Next ();           if  (rand % 3 ==  0)                 {                      code =  (char) (' A '  +  (char) (rand % 26));                 }                 else                {                     code =  (char) (' 0 '  +  (char) (RAND&NBSP;%&NBSP;10));                 }                 randomcode += code. ToString ();            }             return randomCode;         }        ///   Create random code images          private void createimage (String strverifycode)            {            try            {                 int iRandAngle =  45;    //Random Rotation angle                  int iMapWidth =  (int) (STRVERIFYCODE.LENGTH&NBSP;*&NBSP;21);                 bitmap map =  new bitmap (imapwidth, 28);      //Create picture background                                   graphics graph = graphics.fromimage (map);                 graph. Clear (Color.aliceblue);//Erase PictureFace, fills the background                 graph. DrawRectangle (New pen (color.black, 0),  0, 0, map. Width - 1, map. HEIGHT&NBSP;-&NBSP;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 < 50;  i++)        {                     int x = rand. Next (0, map. Width);                     int y = rand. Next (0, map. Height);                     graph. DrawRectangle (blackpen, x, y, 1, 1);                 }                 //verification code rotation to prevent machine identification                  char[] chars = strverifycode.tochararray ();//break string into a single character array                  //          in text distance        stringformat format = new stringformat ( Stringformatflags.noclip);                 format. alignment = stringalignment.center;                 format. linealignment = stringalignment.center;                 //Defining Colors                  Color[] c = { Color.Black, Color.Red,  Color.darkblue, color.green, color.orange, color.brown, color.darkcyan, color.purple  };                //Defining Fonts                  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], 13, system.drawing.fontstyle.bold);//font style (parameter 2 is font size)                      brush  b = new system.drawing.solidbrush (C[cindex]);                     point dot = new  point (16, 16);                                          float angle = rand. Next (-irandangle, irandangle);//degree of rotation                      graph. TranslateTransform (dot. X, dot. Y);//move cursor to the specified position         &nbsP;           graph. RotateTransform (angle);                     graph. DrawString (Chars[i]. ToString (),  f, b, 1, 1, format);                                          graph. RotateTransform (-angle);//Turn Back                      graph. TranslateTransform (2, -dot. Y);//move cursor to the specified position                }                  pbverifycode.image = map;                             }            catch   (ArgumentException)             {                 messagebox.show ("Create picture error. ");            }         }         private void  Butupdateverifycode_click (object sender, eventargs e)          {            updateverifycode ();   The       }            code is all written.


How to implement a window verification code in the WinForm login form of C #

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.