VB.Net/2D/Line

Материал из VB Эксперт
Перейти к: навигация, поиск

Draw Colorful Line and Dash Line

<source lang="vbnet"> Imports System Imports System.Drawing Imports System.Drawing.Drawing2D Imports System.Windows.Forms Imports System.Drawing.Text Public Class MainClass

  Shared Sub Main()
       Dim myform As Form = New FrmDrawShapes()
       Application.Run(myform)
  End Sub " Main

End Class

Public Class FrmDrawShapes

  Inherits System.Windows.Forms.Form
  1. Region " Windows Form Designer generated code "
  Public Sub New()
     MyBase.New()
     "This call is required by the Windows Form Designer.
     InitializeComponent()
     "Add any initialization after the InitializeComponent() call
  End Sub
  "Form overrides dispose to clean up the component list.
  Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
     If disposing Then
        If Not (components Is Nothing) Then
           components.Dispose()
        End If
     End If
     MyBase.Dispose(disposing)
  End Sub
  "Required by the Windows Form Designer
  Private components As System.ruponentModel.Container
  "NOTE: The following procedure is required by the Windows Form Designer
  "It can be modified using the Windows Form Designer.  
  "Do not modify it using the code editor.
  <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
     "
     "frmDrawShapes
     "
     Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
     Me.ClientSize = New System.Drawing.Size(520, 197)
     Me.Name = "frmDrawShapes"
     Me.Text = "Drawing Shapes"
  End Sub
  1. End Region
  " draw various shapes on form
  Protected Overrides Sub OnPaint(ByVal e As PaintEventArgs)
     Dim graphicsObject As Graphics = e.Graphics
     " bitmap texture
     Dim textureBitmap As Bitmap = New Bitmap(10, 10)
     Dim graphicsObject2 As Graphics = _
        Graphics.FromImage(textureBitmap) " get bitmap graphics
     " brush and pen used throughout program
     Dim solidColorBrush As SolidBrush = _
        New SolidBrush(Color.Red)
     Dim coloredPen As Pen = New Pen(solidColorBrush)
     " draw pie-shaped arc in white
     coloredPen.Color = Color.White
     coloredPen.Width = 6
     graphicsObject.DrawPie(coloredPen, 240, 30, 75, 100, 0, 270)
     " draw lines in green and yellow
     coloredPen.Color = Color.Green
     coloredPen.Width = 5
     graphicsObject.DrawLine(coloredPen, 395, 30, 320, 150)
     " draw a rounded, dashed yellow line
     coloredPen.Color = Color.Yellow
     coloredPen.DashCap = LineCap.Round
     coloredPen.DashStyle = DashStyle.Dash
     graphicsObject.DrawLine(coloredPen, 320, 30, 395, 150)
  End Sub " OnPaint

End Class

      </source>


Draw lines to connect rectangles

<source lang="vbnet"> Imports System Imports System.Drawing Imports System.Windows.Forms Imports System.Drawing.Text Public Class MainClass

  Shared Sub Main()
       Dim myform As Form = New FrmDrawing()
       Application.Run(myform)
  End Sub " Main

End Class

Public Class FrmDrawing

  Inherits System.Windows.Forms.Form
  1. Region " Windows Form Designer generated code "
  Public Sub New()
     MyBase.New()
     "This call is required by the Windows Form Designer.
     InitializeComponent()
     "Add any initialization after the InitializeComponent() call
  End Sub
  "Form overrides dispose to clean up the component list.
  Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
     If disposing Then
        If Not (components Is Nothing) Then
           components.Dispose()
        End If
     End If
     MyBase.Dispose(disposing)
  End Sub
  "Required by the Windows Form Designer
  Private components As System.ruponentModel.Container
  "NOTE: The following procedure is required by the Windows Form Designer
  "It can be modified using the Windows Form Designer.  
  "Do not modify it using the code editor.
  <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
     "
     "frmDrawing
     "
     Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
     Me.BackColor = System.Drawing.SystemColors.AppWorkspace
     Me.ClientSize = New System.Drawing.Size(472, 157)
     Me.Name = "frmDrawing"
     Me.Text = "Drawing lines, rectangles and ovals"
  End Sub
  1. End Region
  " display ovals lines, and rectangles
  Protected Overrides Sub OnPaint( _
     ByVal paintEvent As PaintEventArgs)
     " get graphics object
     Dim g As Graphics = paintEvent.Graphics
     Dim brush As SolidBrush = New SolidBrush(Color.Blue)
     Dim pen As Pen = New Pen(Color.AliceBlue)
     " draw lines to connect rectangles
     g.DrawLine(pen, 90, 30, 110, 40)
     g.DrawLine(pen, 90, 120, 110, 130)
     g.DrawLine(pen, 240, 30, 260, 40)
     g.DrawLine(pen, 240, 120, 260, 130)
     " draw top rectangle
     g.DrawRectangle(pen, 110, 40, 150, 90)
  End Sub " OnPaint

End Class " FrmDrawing

      </source>


Paint Lines

<source lang="vbnet"> Imports System Imports System.Drawing Imports System.Drawing.Printing Imports System.Collections Imports System.ruponentModel Imports System.Windows.Forms Imports System.Drawing.Drawing2D Class MyFirstForm

   Inherits Form
   Public Sub Draw(ByVal g As Graphics)
       g.SmoothingMode = SmoothingMode.AntiAlias
       Dim rect As Rectangle = Me.ClientRectangle
       Dim cx As Integer = rect.Width
       Dim cy As Integer = rect.Height
       Dim scale As Single = CSng(cy) / CSng(cx)
       Dim _brush As LinearGradientBrush = New LinearGradientBrush(Me.ClientRectangle, Color.Empty, Color.Empty, 45)
       Dim blend As ColorBlend = New ColorBlend()
       blend.Colors = New Color() {Color.Red, Color.Green, Color.Blue}
       blend.Positions = New Single() {0.0, 0.5, 1.0}
       _brush.InterpolationColors = blend
       Dim _pen As Pen = New Pen(_brush)
       Dim x As Integer
       For x = 0 To cx Step 7
           g.DrawLine(_pen, 0, x * scale, cx - x, 0)
           g.DrawLine(_pen, 0, (cx - x) * scale, cx - x, cx * scale)
           g.DrawLine(_pen, cx - x, 0 * scale, cx, (cx - x) * scale)
           g.DrawLine(_pen, cx - x, cx * scale, cx, x * scale)
       Next
       Dim format As StringFormat = New StringFormat()
       format.Alignment = StringAlignment.Center
       format.LineAlignment = StringAlignment.Center
       Dim s As String = "Ain"t graphics cool?"
       g.DrawString(s, Me.Font, _brush, rect.X, rect.Y, format)
   End Sub
   Private Sub MyPaint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles MyBase.Paint
       Dim g As Graphics = e.Graphics
       Draw(g)
   End Sub
   Shared Sub Main()
       Dim myform As Form = New MyFirstForm()
       Application.Run(myform)
   End Sub

End Class

      </source>


Use Texture Brush to Draw a Line

<source lang="vbnet"> Imports System Imports System.Windows.Forms Imports System.Drawing Imports System.Drawing.Drawing2D Imports System.Drawing.Text

Public Class MainClass

 Shared Sub Main()
    Dim form1 As Form1 = new Form1
    Application.Run(form1)
 End Sub
 

End Class


Public Class Form1

 Inherits System.Windows.Forms.Form
  1. Region " Windows Form Designer generated code "
 Public Sub New()
   MyBase.New()
   "This call is required by the Windows Form Designer.
   InitializeComponent()
   "Add any initialization after the InitializeComponent() call
 End Sub
 "Form overrides dispose to clean up the component list.
 Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
   If disposing Then
     If Not (components Is Nothing) Then
       components.Dispose()
     End If
   End If
   MyBase.Dispose(disposing)
 End Sub
 "Required by the Windows Form Designer
 Private components As System.ruponentModel.IContainer
 "NOTE: The following procedure is required by the Windows Form Designer
 "It can be modified using the Windows Form Designer.  
 "Do not modify it using the code editor.
 <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
   Me.SuspendLayout()
   "
   "Form1
   "
   Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
   Me.ClientSize = New System.Drawing.Size(392, 213)
   Me.Controls.AddRange(New System.Windows.Forms.Control() {})
   Me.Name = "Form1"
   Me.Text = "Form1"
   Me.ResumeLayout(False)
 End Sub
  1. End Region
 Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
 End Sub
 Protected Overrides Sub OnPaint(ByVal e As PaintEventArgs)
   Dim G As Graphics = e.Graphics
   Dim P1 As Pen = New Pen(Color.Blue, 10)
   "pentype
   Dim P3 As Pen = New Pen(Color.Blue, 10)
   P3.Brush = New TextureBrush(New Bitmap("figure2.bmp"))
   G.DrawLine(P3, 20, CInt(Me.Height / 2), Me.Width - 20, CInt(Me.Height / 2))
 End Sub

End Class

      </source>