Step: 1. Generate random numbers or characters and verification codes by random 2. Generate a picture of the specified size by the Captcha content length 3. Gets the graphics object that generated the picture. 4. Define the CAPTCHA font format 5. Draw the captcha to picture 6 by specifying a font. Add a background noise line to the picture 7. Add a foreground noise point
1 Private voidPictureBox1_Click (Objectsender, EventArgs e)2 {3Random r =NewRandom ();4 stringstr =NULL;5 for(inti =0; I <5; i++)6 {7 intRnumber = R.next (0,Ten);8str + =Rnumber;9 }Ten //MessageBox.Show (str); One //Creating GDI Objects ABitmap bmp=NewBitmap ( -, -); -Graphics g =graphics.fromimage (BMP); - the for(inti =0; I <5; i++) - { -Point p=NewPoint (i* -,0); - string[] fonts={"Microsoft Ya-Black","Song Body","blackbody","Official Script","Imitation"}; +Color [] colors={color.yellow,color.blue,color.black,color.red,color.green}; -g.DrawString (Str[i]. ToString (),NewFont (Fonts[r.next (0,4)], -, FontStyle.Bold),NewSolidBrush (Colors[r.next (0,5) ], p); + } A //draw lines, lines cannot exceed the range of Captcha at for(inti =0; I < -; i++) - { -Point p1=NewPoint (R.next (0Bmp. Width), R.next (0, BMP. Height)); -Point p2=NewPoint (R.next (0Bmp. Width), R.next (0, BMP. Height)); -G.drawline (NewPen (Brushes.green), p1, p2); - } in - for(inti =0; I < +; i++) to { +Point p=NewPoint (R.next (0Bmp. Width), R.next (0, BMP. Height)); - bmp. SetPixel (p.x, P.Y, color.black); the } * //mosaic the picture into the PictureBox $pictureBox1.Image =bmp;Panax Notoginseng -}
View Code
C # GDI Draw verification Code