VB.Net/2D/Graphic Path

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

Create a Graphics Path and Draw Path

Imports System
Imports System.Windows.Forms
Imports System.Drawing.Text
Imports System.Drawing
Public Class MainClass
    
    Shared Sub Main(ByVal args As String())
        Dim myform As Form = New Form1()
        Application.Run(myform)
    End Sub
End Class

Public Class Form1
    Private Sub Form1_Paint(ByVal sender As Object, _
     ByVal e As System.Windows.Forms.PaintEventArgs) Handles MyBase.Paint
        " Create a GraphicsPath.
        Dim graphics_path As New Drawing2D.GraphicsPath
        " Add some text to the path.
        graphics_path.AddString("www.vbex.ru", _
            New FontFamily("Times New Roman"), _
            CInt(FontStyle.Bold), _
            80, New Point(10, 10), _
            StringFormat.GenericTypographic)
        " Draw the path.
        e.Graphics.SmoothingMode = Drawing2D.SmoothingMode.AntiAlias
        e.Graphics.DrawPath(New Pen(Color.Black, 3), graphics_path)
    End Sub
End Class

<Global.Microsoft.VisualBasic.rupilerServices.DesignerGenerated()> _
Partial Public Class Form1
    Inherits System.Windows.Forms.Form
    "Form overrides dispose to clean up the component list.
    <System.Diagnostics.DebuggerNonUserCode()> _
    Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
        If disposing AndAlso components IsNot Nothing Then
            components.Dispose()
        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.AutoScaleDimensions = New System.Drawing.SizeF(6.0!, 13.0!)
        Me.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font
        Me.ClientSize = New System.Drawing.Size(364, 509)
        Me.Name = "Form1"
        Me.Text = "Title"
        Me.ResumeLayout(False)
    End Sub
End Class


Create a Graphics Path and Fill Path

Imports System
Imports System.Windows.Forms
Imports System.Drawing.Text
Imports System.Drawing
Public Class MainClass
    
    Shared Sub Main(ByVal args As String())
        Dim myform As Form = New Form1()
        Application.Run(myform)
    End Sub
End Class

Public Class Form1
    Private Sub Form1_Paint(ByVal sender As Object, _
     ByVal e As System.Windows.Forms.PaintEventArgs) Handles MyBase.Paint
        " Create a GraphicsPath.
        Dim graphics_path As New Drawing2D.GraphicsPath
        " Add some text to the path.
        graphics_path.AddString("www.vbex.ru", _
            New FontFamily("Times New Roman"), _
            CInt(FontStyle.Bold), _
            80, New Point(10, 10), _
            StringFormat.GenericTypographic)
        " Draw the path.
        e.Graphics.SmoothingMode = Drawing2D.SmoothingMode.AntiAlias
        e.Graphics.FillPath(Brushes.Red, graphics_path)
    End Sub
End Class

<Global.Microsoft.VisualBasic.rupilerServices.DesignerGenerated()> _
Partial Public Class Form1
    Inherits System.Windows.Forms.Form
    "Form overrides dispose to clean up the component list.
    <System.Diagnostics.DebuggerNonUserCode()> _
    Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
        If disposing AndAlso components IsNot Nothing Then
            components.Dispose()
        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.AutoScaleDimensions = New System.Drawing.SizeF(6.0!, 13.0!)
        Me.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font
        Me.ClientSize = New System.Drawing.Size(364, 309)
        Me.Name = "Form1"
        Me.Text = "Title"
        Me.ResumeLayout(False)
    End Sub
End Class


Create a Graphics Path, Draw and Fill the Path

Imports System
Imports System.Windows.Forms
Imports System.Drawing.Text
Imports System.Drawing
Public Class MainClass
    
    Shared Sub Main(ByVal args As String())
        Dim myform As Form = New Form1()
        Application.Run(myform)
    End Sub
End Class

Public Class Form1
    Private Sub Form1_Paint(ByVal sender As Object, _
     ByVal e As System.Windows.Forms.PaintEventArgs) Handles MyBase.Paint
        " Create a GraphicsPath.
        Dim graphics_path As New Drawing2D.GraphicsPath
        " Add some text to the path.
        graphics_path.AddString("www.vbex.ru", _
            New FontFamily("Times New Roman"), _
            CInt(FontStyle.Bold), _
            80, New Point(10, 10), _
            StringFormat.GenericTypographic)
        " Draw the path.
        e.Graphics.SmoothingMode = Drawing2D.SmoothingMode.AntiAlias
        e.Graphics.FillPath(Brushes.Red, graphics_path)
        e.Graphics.DrawPath(New Pen(Color.Black, 3), graphics_path)
    End Sub
End Class

<Global.Microsoft.VisualBasic.rupilerServices.DesignerGenerated()> _
Partial Public Class Form1
    Inherits System.Windows.Forms.Form
    "Form overrides dispose to clean up the component list.
    <System.Diagnostics.DebuggerNonUserCode()> _
    Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
        If disposing AndAlso components IsNot Nothing Then
            components.Dispose()
        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.AutoScaleDimensions = New System.Drawing.SizeF(6.0!, 13.0!)
        Me.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font
        Me.ClientSize = New System.Drawing.Size(564, 209)
        Me.Name = "Form1"
        Me.Text = "Title"
        Me.ResumeLayout(False)
    End Sub
End Class


Donut Path Demo

Imports System
Imports System.Collections
Imports System.ruponentModel
Imports System.Windows.Forms
Imports System.Data
Imports System.Configuration
Imports System.Resources
Imports System.Drawing
Imports System.Drawing.Drawing2D
Imports System.Drawing.Imaging
Public Class MainClass
    Shared Sub Main()
        Dim myform As Form = New Form1()
        Application.Run(myform)
    End Sub
End Class
Public Class Form1
    Inherits System.Windows.Forms.Form
#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
        Me.SetStyle(ControlStyles.ResizeRedraw, True)
        Me.SetStyle(ControlStyles.DoubleBuffer, True)
        Me.SetStyle(ControlStyles.AllPaintingInWmPaint, True)
    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()
        components = New System.ruponentModel.Container()
        Me.Text = "Form1"
    End Sub
#End Region
   
    Private Function GetDonutPath(ByVal rect As Rectangle, ByVal holeRadius As Integer) As GraphicsPath
        Dim path As GraphicsPath = New GraphicsPath()
        path.AddEllipse(rect)
        Dim centerPoint As Point = New Point(rect.Left + rect.Width / 2, rect.Top + rect.Height / 2)
        Dim holeRect As Rectangle = New Rectangle(centerPoint.X - holeRadius, centerPoint.Y - holeRadius, holeRadius * 2, holeRadius * 2)
        path.AddEllipse(holeRect)
        Dim format As StringFormat = New StringFormat()
        format.Alignment = StringAlignment.Center
        format.LineAlignment = StringAlignment.Center
        path.AddString("Hi", Me.Font.FontFamily, CInt(Me.Font.Style), Me.Font.Height, RectangleF.op_Implicit(rect), format)
        Return path
    End Function
    Private Sub Form1_Paint(ByVal sender As Object, ByVal e As PaintEventArgs) Handles MyBase.Paint
        Dim g As Graphics = e.Graphics
        Dim width As Integer = Me.ClientRectangle.Width
        Dim height As Integer = Me.ClientRectangle.Height
        Dim rect As Rectangle = New Rectangle(10, 10, width - 20, height - 20)
        Dim format As StringFormat = New StringFormat()
        format.Alignment = StringAlignment.Center
        format.LineAlignment = StringAlignment.Center
        Dim path As GraphicsPath = GetDonutPath(rect, width / 5)
        g.FillPath(Brushes.Yellow, path)
        g.DrawPath(Pens.Black, path)
    End Sub
End Class


GraphicsPath: Draw with FillMode = Alternate

Imports System.Drawing.Drawing2D
Imports System
Imports System.Drawing.Text
Imports System.Drawing
Imports System.Windows.Forms
Imports System.Math
Public Class MainClass
   Shared Sub Main()
       Dim form1 As Form = New Form1()
       Application.Run(form1)
   End Sub " ValidationError
End Class
 
 

Public Class Form1
    Private Sub Form1_Paint(ByVal sender As Object, _
     ByVal e As System.Windows.Forms.PaintEventArgs) Handles Me.Paint
        " Generate star points.
        Dim cx As Integer = Me.ClientSize.Width \ 4
        Dim cy As Integer = Me.ClientSize.Height \ 2
        Dim radius As Integer = CInt(cy * 0.9)
        Dim star_pts(4) As Point
        Dim angle As Double = -PI / 2
        For i As Integer = 0 To 4
            star_pts(i).X = cx + CInt(radius * Cos(angle))
            star_pts(i).Y = cy + CInt(radius * Sin(angle))
            angle += 4 * PI / 5
        Next i
        " Move to the right.
        e.Graphics.TranslateTransform(Me.ClientSize.Width \ 2, 0)

        " Add the star to the path.
        Dim star_path As New GraphicsPath
        star_path.AddPolygon(star_pts)
        
        " Make a StringFormat and LayoutRectangle.
        Dim string_format As New StringFormat
        string_format.LineAlignment = StringAlignment.Far
        string_format.Alignment = StringAlignment.Center
        Dim layout_rectangle As New RectangleF( _
            0, 0, Me.ClientSize.Width \ 2, Me.ClientSize.Height)
        e.Graphics.SmoothingMode = SmoothingMode.AntiAlias
        e.Graphics.TextRenderingHint = System.Drawing.Text.TextRenderingHint.AntiAliasGridFit
        " Draw with FillMode = Alternate.
        star_path.FillMode = FillMode.Alternate
        e.Graphics.DrawString(star_path.FillMode.ToString, _
            Me.Font, Brushes.Black, _
            layout_rectangle, string_format)
        e.Graphics.FillPath(Brushes.White, star_path)
        e.Graphics.DrawPath(Pens.Black, star_path)
        
    End Sub
End Class
 
 
<Global.Microsoft.VisualBasic.rupilerServices.DesignerGenerated()> _
Partial Public Class Form1
    Inherits System.Windows.Forms.Form
    "Form overrides dispose to clean up the component list.
    <System.Diagnostics.DebuggerNonUserCode()> _
    Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
        If disposing AndAlso components IsNot Nothing Then
            components.Dispose()
        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.AutoScaleDimensions = New System.Drawing.SizeF(6.0!, 13.0!)
        Me.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font
        Me.ClientSize = New System.Drawing.Size(350, 350)
        Me.Name = "Form1"
        Me.Text = "TransformArrow"
        Me.ResumeLayout(False)
    End Sub
End Class


GraphicsPath: Draw with FillMode = Winding

Imports System.Drawing.Drawing2D
Imports System
Imports System.Drawing.Text
Imports System.Drawing
Imports System.Windows.Forms
Imports System.Math
Public Class MainClass
   Shared Sub Main()
       Dim form1 As Form = New Form1()
       Application.Run(form1)
   End Sub " ValidationError
End Class
 
 

Public Class Form1
    Private Sub Form1_Paint(ByVal sender As Object, _
     ByVal e As System.Windows.Forms.PaintEventArgs) Handles Me.Paint
        " Generate star points.
        Dim cx As Integer = Me.ClientSize.Width \ 4
        Dim cy As Integer = Me.ClientSize.Height \ 2
        Dim radius As Integer = CInt(cy * 0.9)
        Dim star_pts(4) As Point
        Dim angle As Double = -PI / 2
        For i As Integer = 0 To 4
            star_pts(i).X = cx + CInt(radius * Cos(angle))
            star_pts(i).Y = cy + CInt(radius * Sin(angle))
            angle += 4 * PI / 5
        Next i
        " Move to the right.
        e.Graphics.TranslateTransform(Me.ClientSize.Width \ 2, 0)

        " Add the star to the path.
        Dim star_path As New GraphicsPath
        star_path.AddPolygon(star_pts)
        
        " Make a StringFormat and LayoutRectangle.
        Dim string_format As New StringFormat
        string_format.LineAlignment = StringAlignment.Far
        string_format.Alignment = StringAlignment.Center
        Dim layout_rectangle As New RectangleF( _
            0, 0, Me.ClientSize.Width \ 2, Me.ClientSize.Height)
        e.Graphics.SmoothingMode = SmoothingMode.AntiAlias
        e.Graphics.TextRenderingHint = System.Drawing.Text.TextRenderingHint.AntiAliasGridFit
        " Draw with FillMode = Winding.
        star_path.FillMode = FillMode.Winding
        e.Graphics.DrawString(star_path.FillMode.ToString, _
            Me.Font, Brushes.Black, _
            layout_rectangle, string_format)
        e.Graphics.FillPath(Brushes.White, star_path)
        e.Graphics.DrawPath(Pens.Black, star_path)
        
    End Sub
End Class
 
 
<Global.Microsoft.VisualBasic.rupilerServices.DesignerGenerated()> _
Partial Public Class Form1
    Inherits System.Windows.Forms.Form
    "Form overrides dispose to clean up the component list.
    <System.Diagnostics.DebuggerNonUserCode()> _
    Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
        If disposing AndAlso components IsNot Nothing Then
            components.Dispose()
        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.AutoScaleDimensions = New System.Drawing.SizeF(6.0!, 13.0!)
        Me.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font
        Me.ClientSize = New System.Drawing.Size(400, 400)
        Me.Name = "Form1"
        Me.Text = "TransformArrow"
        Me.ResumeLayout(False)
    End Sub
End Class


Path Flatten Demo

Imports System
Imports System.ruponentModel
Imports System.Windows.Forms
Imports System.Data
Imports System.Configuration
Imports System.Drawing
Imports System.Drawing.Drawing2D
Imports System.Drawing.Text
Imports System.Globalization
Imports System.Text
Imports System.Collections
Public Class MainClass
    
    Shared Sub Main()
        
        Dim myform As Form = New PathFlattenForm()
        Application.Run(myform)
    End Sub
End Class



Public Class PathFlattenForm
    Inherits System.Windows.Forms.Form
#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()
        "
        "PathFlattenForm
        "
        Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
        Me.ClientSize = New System.Drawing.Size(416, 126)
        Me.Name = "PathFlattenForm"
        Me.Text = "PathFlattenForm"
    End Sub
#End Region

    Sub PathFlattenForm_Paint(ByVal sender As Object, ByVal e As PaintEventArgs) Handles MyBase.Paint
        Dim g As Graphics = e.Graphics
        Dim width As Single = Me.ClientSize.Width
        Dim height As Single = Me.ClientSize.Height
        Dim rect As RectangleF = New RectangleF(0, 0, width, height)
        Dim format As StringFormat = New StringFormat()
        format.Alignment = StringAlignment.Center
        format.LineAlignment = StringAlignment.Center
        Dim path As GraphicsPath = New GraphicsPath()
        path.Reset()
        path.AddEllipse(rect)
        path.Flatten(New Matrix(), 10)
        g.DrawPath(Pens.Black, path)
        g.DrawString("Flattened", Me.Font, Brushes.Black, rect, format)
        g.TranslateTransform(width, 0)
    End Sub
End Class


Path Warp Demo

Imports System
Imports System.ruponentModel
Imports System.Windows.Forms
Imports System.Data
Imports System.Configuration
Imports System.Drawing
Imports System.Drawing.Drawing2D
Imports System.Drawing.Text
Imports System.Globalization
Imports System.Text
Imports System.Collections
Public Class MainClass
    
    Shared Sub Main()
        
        Dim myform As Form = New PathFlattenForm()
        Application.Run(myform)
    End Sub
End Class



Public Class PathFlattenForm
    Inherits System.Windows.Forms.Form
#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()
        "
        "PathFlattenForm
        "
        Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
        Me.ClientSize = New System.Drawing.Size(416, 126)
        Me.Name = "PathFlattenForm"
        Me.Text = "PathFlattenForm"
    End Sub
#End Region

    Sub PathFlattenForm_Paint(ByVal sender As Object, ByVal e As PaintEventArgs) Handles MyBase.Paint
        Dim g As Graphics = e.Graphics
        Dim width As Single = Me.ClientSize.Width
        Dim height As Single = Me.ClientSize.Height
        Dim rect As RectangleF = New RectangleF(0, 0, width, height)
        Dim format As StringFormat = New StringFormat()
        format.Alignment = StringAlignment.Center
        format.LineAlignment = StringAlignment.Center
        Dim path As GraphicsPath = New GraphicsPath()
        path.Reset()
        path.AddEllipse(rect)
        Dim destPoints As PointF() = New PointF() {New PointF(width / 2, 0), New PointF(width, height), New PointF(0, height / 2)}
        Dim srcRect As RectangleF = New RectangleF(0, 0, width, height / 2)
        path.Warp(destPoints, srcRect)
        g.DrawPath(Pens.Black, path)
        g.DrawString("Warped", Me.Font, Brushes.Black, rect, format)
        g.TranslateTransform(width, 0)
    End Sub
End Class


Path Widen Demo

Imports System
Imports System.ruponentModel
Imports System.Windows.Forms
Imports System.Data
Imports System.Configuration
Imports System.Drawing
Imports System.Drawing.Drawing2D
Imports System.Drawing.Text
Imports System.Globalization
Imports System.Text
Imports System.Collections
Public Class MainClass
    
    Shared Sub Main()
        
        Dim myform As Form = New PathFlattenForm()
        Application.Run(myform)
    End Sub
End Class



Public Class PathFlattenForm
    Inherits System.Windows.Forms.Form
#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()
        "
        "PathFlattenForm
        "
        Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
        Me.ClientSize = New System.Drawing.Size(416, 126)
        Me.Name = "PathFlattenForm"
        Me.Text = "PathFlattenForm"
    End Sub
#End Region

    Sub PathFlattenForm_Paint(ByVal sender As Object, ByVal e As PaintEventArgs) Handles MyBase.Paint
        Dim g As Graphics = e.Graphics
        Dim width As Single = Me.ClientSize.Width
        Dim height As Single = Me.ClientSize.Height
        Dim rect As RectangleF = New RectangleF(0, 0, width, height)
        Dim format As StringFormat = New StringFormat()
        format.Alignment = StringAlignment.Center
        format.LineAlignment = StringAlignment.Center
        Dim path As GraphicsPath = New GraphicsPath()
        path.Reset()
        path.AddEllipse(rect)
        Dim widenPen As Pen = New Pen(Color.Empty, 10)
        path.Widen(widenPen)
        g.DrawPath(Pens.Black, path)
        g.DrawString("Widened", Me.Font, Brushes.Black, rect, format)
        g.TranslateTransform(width, 0)
    End Sub
End Class