The simplest understanding of GDI + is used to draw. These include points, lines, rectangles, strings, and so on.
Let's start with an example that shows how to draw a line in the WinForm form, and the line doesn't disappear as the form moves.
usingSystem;usingSystem.Collections.Generic;usingSystem.ComponentModel;usingSystem.Data;usingSystem.Drawing;usingSystem.Linq;usingSystem.Text;usingSystem.Windows.Forms;namespacegdidemo{ Public Partial classForm1:form { PublicForm1 () {InitializeComponent (); } Private voidForm1_Load (Objectsender, EventArgs e) { //a pen, a piece of paper. Two-point drawing of a straight line object } Private voidButton1_Click (Objectsender, EventArgs e) { //Create a GDI objectGraphics g = This. CreateGraphics ();//new Graphics (); //Create a Brush objectPen pen =NewPen (Brushes.yellow); //Create two pointsPoint P1 =NewPoint ( -, -); Point P2=NewPoint ( -, -); G.drawline (pen, p1, p2); } /// <summary> ///Once the form is redrawn, the line is redrawn again/// </summary> /// <param name= "Sender" ></param> /// <param name= "E" ></param> Private voidForm1_paint (Objectsender, PaintEventArgs e) { //Create a GDI objectGraphics g = This. CreateGraphics ();//new Graphics (); //Create a Brush objectPen pen =NewPen (Brushes.yellow); //Create two pointsPoint P1 =NewPoint ( -, -); Point P2=NewPoint ( -, -); G.drawline (pen, p1, p2); } Private voidButton2_Click (Objectsender, EventArgs e) {Graphics g= This. CreateGraphics (); Pen Pen=NewPen (Brushes.yellow); Size Size=NewSystem.Drawing.Size ( the, -); Rectangle Rec=NewRectangle (NewPoint ( -, -), size); G.drawrectangle (pen, rec); } }}GDI drawing lines
Then with a basic understanding you can use a similar method to draw the verification code. Mainly include drawing the string, line, point three parts, draw on the bitmap bitmap, and then mosaic the entire picture into PictureBox, click the control love you, the verification code changes. The specific code is as follows
1 usingSystem;2 usingSystem.Collections.Generic;3 usingSystem.ComponentModel;4 usingSystem.Data;5 usingSystem.Drawing;6 usingSystem.Linq;7 usingSystem.Text;8 usingSystem.Windows.Forms;9 Ten namespaceClick Generate Verification Code One { A Public Partial classForm1:form - { - PublicForm1 () the { - InitializeComponent (); - } - + /// <summary> - ///Click Change Verification Code + /// </summary> A /// <param name= "Sender" ></param> at /// <param name= "E" ></param> - Private voidPictureBox1_Click (Objectsender, EventArgs e) - { -Random r =NewRandom (); - stringstr =NULL; - for(inti =0; I <5; i++) in { - intRnumber = R.next (0,Ten);//Generate 5 Verification code random number tostr + =rnumber.tostring (); + - } the *Bitmap BMP =NewBitmap ( Max, -); $ //Create a GDI objectPanax NotoginsengGraphics g = graphics.fromimage (BMP);//can be understood as a bitmap as a canvas - the //draw the number above the verification code + for(inti =0; I <5; i++) A { thePoint p=NewPoint (i* -,0);//the relative position here, not relative to the form, is relative to the PictureBox control's + string[] Font = {"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 (Font[r.next (0,5)], -, FontStyle.Bold),NewSolidBrush (Colors[r.next (0,5)], p);//note here that font objects include fonts, pixels, bold or not, and so on - - the } - Wuyi //draw the line above the verification code the for(inti =0; I < -; i++) - { WuPoint P1 =NewPoint (R.next (0Bmp. Width), R.next (0, BMP. Height)); -Point P2 =NewPoint (R.next (0Bmp. Width), R.next (0, BMP. Height)); AboutG.drawline (NewPen (Brushes.green), p1, p2); $ } - - //Add some small dots to the captcha. - for(inti =0; I < -; i++) A { +Point P3 =NewPoint (R.next (0Bmp. Width), R.next (0, BMP. Height)); the bmp. SetPixel (p3. X,p3. Y,color.yellow); - } $ //assign a well-drawn bitmap to the Image property, and the picture will be set to PictureBox thepictureBox1.Image =bmp; the } the } the}draw a digital verification code
Realize:
The use of GDI + in drawing verification codes