Search - Articles
DevASP.NET for ASP.NET, VB.NET, XML and C# (C-Sharp) Developers Tuesday, March 03, 2009
Dev Articles
Search Directory
ASP.NET
VB.Net
C-Sharp
SQL Server
 

How to fill different Graphics shapes using System.Drawing.Graphics

Author: DevASP
Download Source Code : 514_fillpaths.zip

In this simple article you will learn that how you can fill different Graphics shapes using System.Drawing.Graphics namespace in Visual Basic.Net (VB.Net).

Create New Project of windows application in VB and drag two and a timer on form.  

Is create some global variables:

 

    Dim xaxis1 As Integer = 10

    Dim xaxis2 As Integer = 10

    Dim yaxis1 As Integer = 50

    Dim yaxis2 As Integer = 50

Now write code in following events:

 

Private Sub Form2_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

 

        Label1.ForeColor = Color.Wheat

        Label2.ForeColor = Color.Wheat

        Me.BackColor = Color.LightGoldenrodYellow

        Me.WindowState = FormWindowState.Maximized

        Me.Text = "DevAsp"

        Label1.Location = New System.Drawing.Point(Me.Width / 2 - Label1.Width / 2, 50)

        Label2.Location = New System.Drawing.Point(Me.Width / 2 - Label1.Width / 2, 150)

 

End Sub

 

 

 

Private Sub Form2_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles MyBase.Paint

 

        Dim myPen As Pen

        myPen = New Pen(Color.Blue, 2)

        Dim gr As System.Drawing.Graphics = e.Graphics

        Dim path As System.Drawing.Drawing2D.GraphicsPath

        Dim point1(4) As Point

        point1(0).X = 310

        point1(0).Y = 10

        point1(1).X = Me.MousePosition.X

        point1(1).Y = Me.MousePosition.Y

        point1(2).X = 130

        point1(2).Y = 230

        point1(3).X = Me.MousePosition.Y

        point1(3).Y = Me.MousePosition.X

        path = New System.Drawing.Drawing2D.GraphicsPath(Drawing2D.FillMode.Alternate)

        path.AddLines(point1)

        path.CloseAllFigures()

        'gr.DrawPath(myPen, path)

        gr.DrawArc(myPen, 230, 230, 550, 230, Me.MousePosition.Y, 180)

        gr.DrawBezier(myPen, Me.MousePosition.Y, 12, Me.MousePosition.Y, 44, Me.MousePosition.X, 66, Me.MousePosition.Y, 77)

        gr.DrawEllipse(myPen, 12, 12, Me.MousePosition.X, Me.MousePosition.X)

        gr.DrawPolygon(myPen, point1)

        Dim br1 As SolidBrush = New SolidBrush(Color.Red)

        Dim br2 As SolidBrush = New SolidBrush(Color.LightSalmon)

        Dim br3 As SolidBrush = New SolidBrush(Color.LightSteelBlue)

        ' functions for filling the graphics shape

        gr.FillPath(br1, path)

        gr.FillEllipse(br2, 12, 12, Me.MousePosition.X, Me.MousePosition.Y)

        gr.FillPolygon(br3, point1)

        Timer1.Start()

 

End Sub

 

 

 

Private Sub Timer1_Tick(ByVal sender As Object, ByVal e As System.EventArgs) Handles Timer1.Tick

 

        Me.BackColor = Color.Black

        Me.BackColor = Color.LightGreen

        Me.BackColor = Color.Black

 

End Sub

 

 

 

Private Sub Form2_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles MyBase.MouseDown

 

        If e.Button = MouseButtons.Left Then

            Timer1.Stop()

        End If

        If e.Button = MouseButtons.Right Then

            Timer1.Start()

        End If

 

End Sub

Article Comments
i can draw the circle in the mouse event.in my project ,after draw the circles the lines are automatically generated between the circles. i need a samples for this....

Posted on 12/30/2009 7:02:12 AM by YasikRaja

I Want Rectangle Filling ..
On Timmer Control In C#...
And When I Want At That Time Filling Start
And Fill Polygon Region.. Whatevere I Give Ratio
Say 50%,70%... etc

Posted on 2/25/2010 10:10:11 PM by vipul

   
Add Article Comment:
Name :
Email Address :
   
Comments :
 
   
<< Make your scheduler for window log off, restart, shutdown with respect to system time in VB.Net

Disclaimer - Privacy
© 2002-2017 DevASP.net