Drawing Other Objects with Line Caps and Styles in VB.NET

In this article I will explain about image class methods and properties in GDI+.
  • 2406
So far we have applied line caps and line styles only to lines, but these effects can also be applied to other objects, including curves, rectangles, and ellipses. However, some of these objects impose limitations. For example, rectangles, ellipses, and closed curves do not have starting and ending caps, so the StartCap and EndCap properties of a pen will not affect them.
 
Let's add one more menu item to MainMenu, called OtherObjects. The code for its menu item click event handler is given in Listing 9.5. We create three pens with different colors and widths; set their line cap, dash style, and dash cap properties; and draw a rectangle, an ellipse, and a curve.
 
LISTING 9.5: Drawing other objects using line caps, dash styles, and dash caps 

Imports System.Collections.Generic

Imports System.ComponentModel

Imports System.Data

Imports System.Drawing

Imports System.Linq

Imports System.Text

Imports System.Drawing.Drawing2D

Imports System.Windows.Forms

Namespace DrawingOtherObjectsLine

    Partial Public Class Form1

        Inherits Form

        Public Sub New()

            InitializeComponent()

        End Sub

        Private Sub Form1_Paint(ByVal sender As Object, ByVal e As PaintEventArgs)

            Dim g As Graphics = Me.CreateGraphics()

            g.Clear(Me.BackColor)

            g.SmoothingMode = SmoothingMode.AntiAlias

            ' Create pen objects

            Dim blackPen As New Pen(Color.Black, 5)

            Dim bluePen As New Pen(Color.Blue, 8)

            Dim redPen As New Pen(Color.Red, 4)

            ' Set DashCap styles

            blackPen.StartCap = LineCap.DiamondAnchor

            blackPen.EndCap = LineCap.SquareAnchor

            blackPen.DashStyle = DashStyle.DashDotDot

            blackPen.DashPattern = New Single() {10}

            blackPen.DashCap = DashCap.Triangle

            ' Set blue pen dash style and dash cap

            bluePen.DashStyle = DashStyle.DashDotDot

            bluePen.DashCap = DashCap.Round

            ' Set red pen line cap and line dash styles

            redPen.StartCap = LineCap.Round

            redPen.EndCap = LineCap.DiamondAnchor

            redPen.DashCap = DashCap.Triangle

            redPen.DashStyle = DashStyle.DashDot

            redPen.DashOffset = 3.4F

            ' Draw a rectangle

            g.DrawRectangle(blackPen, 20, 20, 200, 100)

            ' Draw an ellipse

            g.DrawEllipse(bluePen, 20, 150, 200, 100)

             Draw a curve

            Dim pt1 As New PointF(90.0F, 40.0F)

            Dim pt2 As New PointF(130.0F, 80.0F)

            Dim pt3 As New PointF(200.0F, 100.0F)

            Dim pt4 As New PointF(220.0F, 120.0F)

            Dim pt5 As New PointF(250.0F, 250.0F)

            Dim ptsArray As PointF() = {pt1, pt2, pt3, pt4, pt5}

            g.DrawCurve(redPen, ptsArray)

            ' Dispose of objects

            blackPen.Dispose()

        End Sub

    End Class

End Namespace

Figure 9.7 shows the output from Listing 9.5. Each graphics object rectangle, ellipse, and curve has a different style.
 
FIGURE-9_7.gif
 
FIGURE 9.7: A rectangle, an ellipse, and a curve with different line styles
 
Conclusion
 
Hope the article would have helped you in understanding image class methods and properties in GDI+. Read other articles on GDI+ on the website.

Categories

More Articles

© 2020 DotNetHeaven. All rights reserved.