Implementation of verification code (Chinese character) in C#winform

Source: Internet
Author: User
Tags set background

The

Code is as follows:

Using System; Using System.Collections.Generic; Using System.Linq; Using System.Text; Using System.Windows.Forms; Using System.Drawing; Enum chinese{in, on, ancient, early, period, person, name, one, like, all, very, park, real, such as, Xia, Shang, two, generation, stay, under, Hole, A, shoes, Decyl, Wai, C, Yong, self, pan, Geng, Wu, Ding, Xiao, Xin, etc., To, dry, branch, can, can, with}; Class Drawverification {///<summary>///draws the captcha into a picture and sets it to the BackgroundImage///of the selected control this method applies only to controls that support background pictures///</ summary>///<param name= Control > controls to draw Authenticode </param>///<param name= "XPosition" > backgroundimage x coordinates </param>///<param name= "yposition" >backgroundimage y-coordinates </param>///<param Name= "width" > to draw the width of the picture </param>///<param name= "height" > to draw a picture of the high </param>///<returns> Successful execution return verification code </returns>///public static string Draw (control control, int xposition, int yposition, int width, int Heig HT) {string resultstr = string. Empty;System, prompt error, random return of a Chinese character (in case the verification code is empty can also be successfully validated, but of course there are random can write right, but to from the Int. The probability of one of the 4 MaxValue is also not large) if (control. Size.width < x | | Control. Size.Height < y | | Width <20 | | height<=0) {MessageBox.Show ("the location of the current validation code to be drawn is invalid", "Information Prompt", MessageBoxButtons.OK, MessageBoxIcon.Information); Random r = new Random (); Return ((char) r.next (0, Int.) MaxValue)). ToString () + ((char) r.next (0, Int.) MaxValue)). ToString () + ((char) r.next (0, Int.) MaxValue)). ToString () + ((char) r.next (0, Int.) MaxValue)). ToString (); } Random Random = new Random (); Bitmap image = New Bitmap (xposition + width, yposition + height); Create an empty diagram (the default location for BackgroundImage is the control's (0,0) coordinates) Graphics g = graphics.fromimage (image); The graph is the canvas Pen p = null; Create a brush for (int i = 0; i < WIDTH/20 i++)//random Draw (WIDTH/20) line {point P1 = new Point (random. Next (xposition, x), random. Next (YPosition, y)); Point P2 = new Point (random. Next (xposition, x), random. Next (YPosition, y)); p = new Pen (Color.FromArgb SolidBrush (random). Next (0, 256), random. Next (0, 256), random. Next (0, 2)), Random. Next (1, 3)); G.drawline (P, p1, p2); for (int j = 0; J < + j)/Draw 10 times times the line point (actually a dash) {int xpos = random. Next (xposition, x); int yPos = random. Next (yposition, y); G.drawline (P, New Point (Xpos, YPos), New Point (XPos-2, yPos-2)); } p.dispose (); Release for (int i = 0; i < 4; i++) {int strsize = random. Next (Width * 10/100, Width * 15/100); Font size if (Strsize > Height-10) {strsize-= if (strsize <= 0) {strsize + = 10;}}//* The Chinese character was also thought to be in the enumeration ((char) ran Dom. Next (19968, 40869)). ToString () to get Chinese characters, but the result is quite a lot of words do not know, 冏 can only write a few Chinese characters to use the * * string str = ((Chinese) random. Next (0, 39)). ToString (); ResultStr + + str; Draw a string to the canvas g.drawstring (str, New Font ("XXFarEastFont-Xingkai", Strsize, Fontstyle.italic), New SolidBrush (Color.FromArgb. Next (0, 256), random. Next (0, 256), random. Next (0, 256))), new PointF (xposition + i * WIDTH/4, yposition + HEIGHT/4)); } g.dispose (); Releases the try {control. BackgroundImageLayout = Imagelayout.none; Set background image Layout} catch {} control. BackgroundImage = Image; Set background image} return resultstr; Return string}}

If there is a PictureBox control on the form and the Name property is PictureBox1, call:

Drawverification.draw (pictureBox1, 0, 0, picturebox1.width, picturebox1.height);

If you draw directly onto a form, call:

Drawverification.draw (this, 100, 100, 200, 60);


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.