vb.net drawing graphics method

Briefly describe drawing graphics in VB.NET

  When learning VB.NET, you may encounter the problem of drawing graphics in VB.NET. Here we will introduce the solution to the problem of drawing graphics in VB.NET, and share it with you here.

  1. Create a Graphics object

  To draw graphics in VB.NET, you need to specify 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 creation method, 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 the 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 Image object to create, then use Graphics.FromImage method

  2. VB.NET draws graphics firstly using brushes

  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 pen
  Format: Dim pen name As New Pen (color [, width]) where the color is the color of the line drawn by the pen, and the width is the width of the line drawn by the pen, in pixels. The default value for width is 1.
  For example: Dim mypen As New Pen(Color.Blue) or Dim mypen As Pen = New Pen(Color.Blue)

  2> Drawing lines or hollow shapes
  After creating a brush, 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 (pen name, X1, Y1, X2, Y2) where (X1, Y1) and (X2, Y2) are the coordinates of the start and end points of the line, they can be Integer value, which can also be a Single value. 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) where (X, Y) is the coordinates 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 polygon DrawPolygon (pen name, vertex)
  Among them, vertex is an array, the array type is Point or PointF structure, 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 of defining a point with the Point or PointF structure is: Dim point name As New Point/PointF(x,y) The function of the DrawPolygon method is to connect the vertices of the array to form a polygon, and draw an edge between two consecutive vertices .
  (4) DrawEllipse method - drawing circles and ellipses 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 (pen name, X, Y, width, height, starting angle, scanning angle)   Compared with the DrawEllipse method, this method has two more parameters, the starting angle and the scanning angle, which can It is regarded as an arc formed by intercepting a circle or 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, starting angle, scanning angle) pie chart is also called a fan chart. This method has the same parameters as the DrawArc method, but the pie chart has two more radii than the arc.

        (7) Drawstring method - draw a string DrawString(String, Font, Brush, PointF) draws the specified text string at the specified position and with the specified Brush and Font objects.

For details, please refer to: https://msdn.microsoft.com/zh-cn/library/system.drawing.graphics(v=vs.110).aspx

Guess you like

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