ARTICLE

Drawing Other Objects with Line Caps and Styles in GDI+

Posted by Dinesh Beniwal Articles | GDI+ in VB.NET June 03, 2010
In this article I will explain about image class methods and properties in GDI+.
 
Reader Level:

HTML clipboard

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.

Login to add your contents and source code to this article
share this article :
post comment
 
Become a Sponsor
PREMIUM SPONSORS
  • ceTE software specializes in components for dynamic PDF generation and manipulation. The DynamicPDF™ product line allows you to dynamically generate PDF documents, merge PDF documents and new content to existing PDF documents from within your applications. Visit DynamicPDF here
    Finally – a virtual platform that delivers next-generation Windows Server 2008 Hyper-V virtualization technology from a managed hosting partner you can truly depend on. Visit www.maximumasp.com/max for a FREE 30 day trial. Hurry offer ends soon. Climb aboard the MaxV platform and take advantage of High Availability, Intelligent Monitoring, Recurrent Backups, and Scalability – with no hassle or hidden fees. As a managed hosting partner focused solely on Microsoft technologies since 2000, MaximumASP is uniquely qualified to provide the superior support that our business is built on. Unparalleled expertise with Microsoft technologies lead to working directly with Microsoft as first to offer IIS 7 and SQL 2008 betas in a hosted environment; partnering in the Go Live Program for Hyper-V; and product co-launches built on WS 2008 with Hyper-V technology.
6 Months Free & No Setup Fees ASP.NET Hosting!
Become a Sponsor