C # applets-randomly generate verification Codes

Source: Internet
Author: User

I had nothing to do at home recently. I had read books for a few days and started to look a little big. Write code.

1. Purpose

A Random verification code is generated. Use C # GDI + to draw a graph. Since I only use a simple winform program, it will be displayed on winform.


2. Ideas

Select a few random characters, and then randomly generate the attributes (font, size, color) of each character. Then, draw them out.

3. Implement 3.1 to define variables

You can add the numbers of characters, fonts, and colors. Suggestion: It is recommended that the characters contained in the verification code be not ambiguous, such as numbers 1 and letters l.

Char [] DATA = new char [6]; // used to hold the generated random characters, variable Size // Character Color, Font, and other change scopes string [] myfont = {"Times New Roman", "Ms mincho", "Book antiqua", "gungsuh ", "pmingliu", "impact", "", ""}; color [] mycolor = {color. black, color. red, color. blue, color. green, color. orange, color. brown, color. brown, color. darkblue}; char [] mychar = {'2', '3', '4', '5', '6', '8', '9 ', 'A', 'B', 'C', 'D', 'E', 'E', 'F', 'G', 'h', 'J', 'k ', 'l', 'M', 'n', 'P', 'R', 's', 't', 'w', 'x', 'y ', 'Z', 'A', 'B', 'C', 'D', 'E', 'E', 'F', 'G', 'h', 'J ', 'k', 'M', 'n', 'P', 'R', 's', 't', 'w', 'x', 'y ', 'Z '};
3.2 define variables such as canvas and bitmap

The background is white. Create a canvas from mybitmap.

Graphics g = this.CreateGraphics();Bitmap myBitmap=new Bitmap(200,100);g = Graphics.FromImage(myBitmap);g.Clear(Color.White);

Next, define the random variable random.

Random r = new Random(DateTime.Now.Second);

The following random number generated by R is not important. The characters in the verification code can be the same.

Painting 3.3

The Verification Code also contains some other lines and points to confuse the line of sight.

// Draw Dry line for (INT I = 0; I <10; I ++) {int X1 = R. next (200); int Y1 = R. next (100); int X2 = R. next (200); int y2 = R. next (100); color CLR = mycolor [R. next (mycolor. length)]; G. drawline (new pen (CLR), new point (x1, Y1), new point (X2, Y2);} // draw the character for (INT I = 0; I <data. length; I ++) data [I] = (char) mychar [R. next (mychar. length)]; for (INT I = 0; I <data. length; I ++) {font tempfont = new font (myfont [R. next (myfont. length)], 18); G. drawstring (data [I]. tostring (), tempfont, new solidbrush (mycolor [R. next (mycolor. length)]), (float) I * 20 + 40, (float) 40);} // Draw Dry points for (INT I = 0; I <100; I ++) {int x = R. next (mybitmap. width); int y = R. next (mybitmap. height); color tempcolor = mycolor [R. next (mycolor. length)]; mybitmap. setpixel (X, Y, tempcolor );};
3.4 Display and save

But it is not displayed. In this case, it is only painted in mybitmap. to display it, define another canvas.

Graphics thisGr;thisGr = this.CreateGraphics();thisGr.DrawImage(myBitmap, new Point(90, 35));

This is done.


Do not forget to release resources before exiting.

4. Insufficiency

The generated verification code is not beautiful, and the characters in the Verification Code are not skewed.

5. Code

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; using system. io; using system. drawing. imaging; namespace ch53 _ randomly generated verification code {public partial class form1: FORM {public form1 () {initializecomponent (); thisgr = This. creategraphics ();} Char [] DATA = new char [6]; // used to hold the generated random characters, variable Size // Character Color, Font, and other change scopes string [] myfont = {"Times New Roman", "Ms mincho", "Book antiqua", "gungsuh ", "pmingliu", "impact", "", ""}; color [] mycolor = {color. black, color. red, color. blue, color. green, color. orange, color. brown, color. brown, color. darkblue}; char [] mychar = {'2', '3', '4', '5', '6', '8', '9 ', 'A', 'B', 'C', 'D', 'E', 'E', 'F', 'G', 'h', 'J', 'k ', 'l', 'M', 'n', 'P', 'R', 's', 't', 'w', 'x', 'y ', 'Z', 'A', 'B', 'C', 'D', 'E', 'E', 'F', 'G', 'h', 'J ', 'k', 'M', 'n', 'P', 'R', 's', 't', 'w', 'x', 'y ', 'Z'}; graphics thisgr; private void btnset_click (Object sender, eventargs e) {graphics G = This. creategraphics (); bitmap mybitmap = new Bitmap (200,100); G = graphics. fromimage (mybitmap); G. clear (color. white); random r = new random (datetime. now. second); // Draw Dry line for (INT I = 0; I <10; I ++) {int X1 = R. next (200); int Y1 = R. next (100); int X2 = R. next (200); int y2 = R. next (100); color CLR = mycolor [R. next (mycolor. length)]; G. drawline (new pen (CLR), new point (x1, Y1), new point (X2, Y2);} // draw the character for (INT I = 0; I <data. length; I ++) data [I] = (char) mychar [R. next (mychar. length)]; for (INT I = 0; I <data. length; I ++) {font tempfont = new font (myfont [R. next (myfont. length)], 18); G. drawstring (data [I]. tostring (), tempfont, new solidbrush (mycolor [R. next (mycolor. length)]), (float) I * 20 + 40, (float) 40);} // Draw Dry points for (INT I = 0; I <100; I ++) {int x = R. next (mybitmap. width); int y = R. next (mybitmap. height); color tempcolor = mycolor [R. next (mycolor. length)]; mybitmap. setpixel (X, Y, tempcolor);} mybitmap. save ("C: \ photo.gif"); thisgr. drawimage (mybitmap, new point (90, 35); mybitmap. dispose (); G. dispose ();} private void form=formclosing (Object sender, formclosingeventargs e) {thisgr. dispose ();}}}

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.