vb.NET drawing

Through the study of VB.NET, you can know that it is a very practical development language. And it plays a very important role in drawing. We can first understand some basic knowledge of VB.NET drawing through this article, and initially master the application skills in this area.

VB.NET drawing 1. Create Graphics object

In VB.Net, drawing graphics requires specifying the drawing surface. Among them, the form and all controls with the Text property can be used as the surface for drawing graphics. Because the Graphics object identifies the drawing surface of GDI+, the Graphics object must be created first to draw graphics. There are several ways to create a Graphics object.

1> Use the CreateGraphics method to create

This is a common way to create it, and its format is:

Dim object name As Graphics

object name = form name (or control name).CreateGraphics

2> Use the PaintEventArgs parameter to pass the Graphics object

Graphics can be drawn directly through the Paint event of a form or control. When writing the Paint event handler, the parameter PaintEventArgs provides the graphics object. E.g:

Private Sub Form1_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles MyBase.Paint

Dim g_paint As Graphics = e.Graphics

End Sub

3> Create from Image object

You can also use the Image object to create, then use the Graphics.FromImage method

VB.NET Drawing 2. Brush

A pen is a GDI+ object used to draw lines, and it is an instance of the Pen class. With the brush, you can draw straight lines, curves, and borders in the shape of rectangles, circles, and polygons.

1> Create a brush

Format:

Dim pen name As New Pen(color[, width])

The color is the color of the line drawn with the brush, and the width is the width of the line drawn by the brush, in pixels. The default value for width is 1.

E.g:

Dim mypen As New Pen(Color.Blue)

或Dim mypen As Pen = New Pen(Color.Blue)

2> Draw lines or hollow shapes

After the brush is established, you can use various methods of the Graphics class to draw lines, curves or hollow shapes such as rectangles and circles.

(1) DrawLine method - draw a straight line

DrawLine(brush name, X1, Y1, X2, Y2)

Among them, (X1, Y1) and (X2, Y2) are the coordinates of the starting point and ending point of the line, and they can be either Integer values ​​or Single values. When the line is short, it can be approximated as a point.

(2) DrawRectangle method - draw a rectangle

DrawRectangle(pen name, X, Y, width, height)

Among them, (X, Y) is the coordinate of the upper left corner of the rectangle, and the width and height specify the width and length of the rectangle.

(3) DrawPolygon method - draw polygons

DrawPolygon(brush name, vertex)

Among them, the vertex is an array, the type of the array is a Point or PointF structure, and each element of the array is used to specify the coordinates of each vertex of the polygon. The Point structure specifies the Integer type, and the PointF specifies the Single type.

The format for defining a point using the Point or PointF structure is:

Dim roll call As New Point/PointF(x,y)

The function of the DrawPolygon method is to connect a polygon in the order of the vertices of the array, and draw an edge between two consecutive vertices.

(4) DrawEllipse method - drawing circles and ellipses

DrawEllipse(brush name, x, y, width, height)

The rectangle defined by x, y, width, height in the method is the circumscribed rectangle of the circle or ellipse to be drawn, which determines the size and shape of the drawn ellipse. When the width and height are equal, what is drawn is a circle, otherwise it is an ellipse.

(5) DrawArc method - draw arc

DrawArc(brush name, X, Y, width, height, start angle, sweep angle)

Compared with the DrawEllipse method, this method has two more parameters, the starting angle and the scanning angle, which can be regarded as an arc formed by intercepting a circle or an ellipse. The starting angle and the scanning angle are both in degrees, generally the radius to the right is 0 degrees from the horizontal, and then the arc is drawn clockwise. The start angle is the angle at which to start drawing the arc, and the sweep angle is the angle that increases clockwise. When the scan angle is 360 degrees, what is drawn is a circle or an ellipse.

(6) DrawPie method - draw a pie chart

DrawPie(pen name, X, Y, width, height, start angle, sweep angle)

Pie charts are also called fan charts. This method has the same parameters as the DrawArc method, but the pie chart has two more radii than the arc.

VB.NET Drawing 3. Brush and Fill

In VB.Net, if you want to fill a color, pattern, or render text in a closed figure, you must first create a brush. Brushes are used in conjunction with drawing methods to fill shapes with colors or patterns. GDI+ provides 5 kinds of brushes, only two commonly used ones are introduced here.

1> Monochrome brush (SolidBrush)

Use the SolidBrush class to define a brush and initialize a specified single color. The format is:

Dim brush name As New SolidBrush (color)

or

Dim brush name As Brush

brush name = New SolidBrush(color)

2> Pattern fill brush (HatchBrush)

Use the HatchBrush class to define a brush that fills a shape with a specific pattern. The format is:

Dim brush name As New HatchBrush (type, foreground color [, background color])

Among them, the type is used to specify the filling pattern, which is the HatchStyle enumeration type, which has more than 50 members, and each member provides a pattern. After entering HatchStyle in the code editing window, the system will automatically pop up a list of HatchStyle enumeration members for the user to choose.

 


 

 

VB.NET drawing 4. Drawing text

In VB.Net, the text in the form or picture box is treated as graphics. In the text box, label, list box and other controls, text and graphics can be used for text output.

The output of graphics text can be realized through the DrawString method in the Graphics class. When using it, you must first define a brush, and then use the brush to draw the text.

1> Font

Create a font object before outputting text, and specify the font name, size, style, etc. Font objects are created through the Font class in the format:

Dim font object As New Font(name, size[, style[, unit of measure]])

where name is a string specifying the font name, such as Lishu, Symbol, etc.

Styles are of the FontStyle enumeration type. Its members are:

Bold, Italic, Regular, Strikeout, Underline

The unit of measure is the unit used to specify the font size, and it is of the GraphicsUnit enumeration type. Its members are:

Display 1/75 inch

Document Document unit (1/300 inch)

Inch

Millimeter mm

pixel

Point printer point (1/75 inch)

World Universal

The parameter style and measure can be omitted, and the font defaults to regular style and Point size unit. You can specify multiple styles at the same time, and use Or to connect them.

2> DrawString method

The format is:

DrawString(string, font, brush, point)

DrawString(string, font, brush, rectangle)

DrawString(string, font, brush, X, Y)

Point is used to specify the starting position of text output, it is of type PointF structure. The rectangle specifies the text output location, which is of type RectangleF. XY specifies the starting position of the text output, both of the Single type.

VB.NET drawing 5. Delete Graphics object and clear screen

1> Delete the Graphics object

After the Graphics object is used, it should be deleted in time to release the resources occupied by the object, and use the Dispose method.

2> Clear the screen

Use the Clear method of the Graphics class to clear all the contents of the drawing workspace and fill it with the specified background color.

Format: Clear(color)

Clears the screen of the form and fills the entire form with color.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324403671&siteId=291194637