Blue Theme Orange Theme Green Theme Red Theme
 
MindFusion's Components
Home | Forums | Videos | Photos | Blogs | Beginners
 | Consulting  
Submit an Article Submit a Blog 
 Login Close
User Id:
Password:
 
Forgot Password
Forgot Username
Why Register
 Jump to
Skip Navigation Links
TechnologyExpand Technology
WebsiteExpand Website
 Resources  
Close
 Our Network  
Close
Search :       Advanced Search »
Home » GDI+ and Graphics » Fill Methods in GDI+

Fill Methods in GDI+

In this article I will explain about Fill Methods in GDI+.

Author Rank:
Total page views :  1809
Total downloads : 
   Print Read/Post comments Post a comment  Similar Articles  
   Email to a friend  Bookmark  Author's other articles  
 
Click Here for 6 Months Free! Powerful ASP.NET Hosting at your Fingertips!
Become a Sponsor


So far we have seen only the draw methods of the Graphics class. As we discussed earlier, pens are used to draw the outer boundary of graphics, shapes, and brushes are used to fill the interior of graphics shapes. In this section we will cover the Fill methods of the Graphics class. You can fill only certain graphics shapes; hence there are only a few Fill methods available in the Graphics class. Table 3.5 lists them.

The FillCloseCurve Method

FillCloseCurve fills the interior of a closed curve. The first parameter of FillClosedCurve is a brush. It can be solid brush, a hatch brush, or a gradient brush. The second parameter is an array of points. The third and fourth parameters are optional. The third parameter is a fill mode, which is resented by the FillMode enumeration.

The FillMode enumeration specifies the way the interior of a closed path is filled. It has two modes: alternate or winding. The values for alternate and winding are Alternate and Winding, respectively. The default mode is Alternate. The fill mode matters only if the curve intersects itself (see Section 3.2.1.10).

To fill a closed curve using FillClosed Curve, an application first creates a Brush object and an array of points for the curve. The application can then set the fill mode and tension (which is optional) and call FillClosedCurve.

Listing 3.24 creates an array of PointF structures and a SolidBrush object, and calls FillClosedCurve.

LISTING 3.24: Using FillClosedCurve to fill a closed curve


    Private Sub Form1_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs)
        ' Create a pen
        Dim bluePen As New Pen(Color.Blue, 1)
        ' Create an array of points
        Dim pt1 As New PointF(40.0F, 50.0F)
        Dim pt2 As New PointF(50.0F, 75.0F)
        Dim pt3 As New PointF(100.0F, 115.0F)
        Dim pt4 As New PointF(200.0F, 180.0F)
        Dim pt5 As New PointF(200.0F, 90.0F)
 
        Dim ptsArray As PointF() = {pt1, pt2, pt3, pt4, pt5}

        ' Fill a closed curve
        Dim tension As Single = 1.0F
        Dim flMode As FillMode = FillMode.Alternate
        Dim blueBrush As New SolidBrush(Color.Blue)
        e.Graphics.FillClosedCurve(blueBrush, ptsArray, flMode, tension)
 
        ' Dispose of object
        blueBrush.Dispose()
    End Sub

TABLE 3.5: Graphics fill methods
 

Method Description

FillCloseCurve

Fills the interior of a closed cardinal spline curve defined by an array of Point structures.

FillEllipse

Fills the interior of an ellipse defined by a bounding rectangle specified by a pair of coordinates, a width and a height.

FillPath

Fills the interior of a GraphicsPath object.

FillPie

Fills the interior of a pie section defined by an ellipse specified by a pair of coordinates, a width, a height, and two radial lines.

FillPolygon

Fills the interior of a polygon defined by an array of points specified by Point structures.

FillRectangle

Fills the interior of a rectangle specified by a pair of a coordinates, a width, and a height.

FillRectangles

Fills the interiors of a series of rectangles specified by Rectangle structures.

FillRegion

Fills the interiors of a Region object.


Figure 3.36.jpg

FIGURE 3.36: Filing a closed curve

Figure 3.36 shows the output from Listing 3.24.

The FillEllipse Method

FillEllipse fills the interior of an ellipse. It takes a Brush object and rectangle coordinates.

To fill an ellipse using FillEllipse, an application creates a Brush and a rectangle and calls FillEllipse. Listing 3.25 creates three brushes and calls FillEllipse to fill an ellipse with a brush.

LISTING 3.25: Filling ellipses


    Private Sub Form1_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs)
        Dim g As Graphics = e.Graphics

        ' Create brushes
        Dim redBrush As New SolidBrush(Color.Red)
        Dim blueBrush As New SolidBrush(Color.Blue)
        Dim greenBrush As New SolidBrush(Color.Green)

        'Create a rectangle
        Dim rect As New Rectangle(80, 80, 50, 50)

        ' Fill ellipses
        g.FillEllipse(greenBrush, 40.0F, 40.0F, 130.0F, 130.0F)
        g.FillEllipse(blueBrush, 60, 60, 90, 90)
        g.FillEllipse(greenBrush, 100.0F, 90.0F, 10.0F, 30.0F)

        ' Dispose of objects
        blueBrush.Dispose()
        redBrush.Dispose()
        greenBrush.Dispose()
    End Sub

Figure 3.37 shows the output from Listing 3.25.

Fig3.37.gif

FIGURE 3.37: Filling ellipses

The FillPath Method

FillPath fills the interior of a graphics path. To do this, an application creates Brush and Graphics objects and the calls FillPath, which takes a brush and a graphics path as arguments. Listing 3.26 create GraphicsPath and SolidBrush objects and calls FillPath.

LISTING 3.26: Filling a graphic path


    Private Sub Form1_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs)

        ' Create a solid brush
        Dim greenBrush As New SolidBrush(Color.Green)

        ' Create a graphics path
        Dim path As New GraphicsPath()

        ' Add a line to the path
        path.AddLine(20, 20, 103, 80)

        ' Add an ellipse to the path
        path.AddEllipse(100, 50, 100, 100)

        ' Add three more lines
        path.AddLine(195, 80, 300, 80)
        path.AddLine(200, 100, 300, 100)
        path.AddLine(195, 120, 300, 120)

        ' Create a rectangle and call AddRectangle
        Dim rect As New Rectangle(50, 150, 300, 50)
        path.AddRectangle(rect)

        ' Fill path
        e.Graphics.FillPath(greenBrush, path)

        ' Dispose of object
        greenBrush.Dispose()
    End Sub

Figure 3.38 shows the output from Listing 3.26. As the figure shows, the fill method fills all the covered areas of a graphics path.

Figure 3.38.jpg

FIGURE: 3.38: Filling a graphics path

The FillPie Method

FillPie fills a pie section with a specified brush. It takes four parameters: a brush, the rectangle of the ellipse, and the start and sweep angles. The following code calls FillPie.


g.FillPie(New SolidBrush(Color.Red), 0F, 0F, 100, 60, 0F, _
90F)

The FillPolygon Method

FillPolygon fills a polygon with the specified brush. It takes three parameters: a brush, an array of points, and a fill mode. The FillMode enumeration defines the fill mode of the interior of the path. It provides two fill modes: Alternate and Winding. The default mode is Alternate.

In our application we will use a hatch brush. So far we have seen only a solid brush. A solid brush is a brush with one color only. A hatch brush is a brush with a hatch style and two colors. These colors work together to support the hatch style. The HatchBrush class represents a hatch brush.

The Code in Listing 3.27 uses FillPolygon to fill a polygon using the Winding mode.

LISTING 3.27: Filling a polygon


        Dim g As Graphics = e.Graphics
 
        ' Create a solid brush
        Dim greenBrush As New SolidBrush(Color.Green)

        ' Create points for polygon
        Dim p1 As New PointF(40.0F, 50.0F)
        Dim p2 As New PointF(60.0F, 70.0F)
        Dim p3 As New PointF(80.0F, 34.0F)
        Dim p4 As New PointF(120.0F, 180.0F)
        Dim p5 As New PointF(200.0F, 150.0F)
        Dim ptsArray As PointF() = {p1, p2, p3, p4, p5}

        ' Draw polygon
        e.Graphics.FillPolygon(greenBrush, ptsArray)

        ' Dispose of objects
        greenBrush.Dispose()


Figure 3.39 shows the output from Listing 3.27. As you can see, the fill method fills all the areas of a polygon.

Filling Rectangle and Regions

FillRectangle fills a rectangle with a brush. This method takes a brush and a rectangle as arguments. FillRectangles fills a specified series of rectangles with a brush, and it takes a brush and an array of rectangles. These methods also have overloaded forms with additional options. For instance, if you're using a HatchStyle brush, you can specify background and foreground colors.

Note: The HatchBrush class is defined in the System.Drawing.Drawing2D namespace.

The source code in Listing 3.28 uses FillRectangle to fill two rectangles. One rectangle is filled with a hatch brush, the other with a solid brush.

LISTING 3.28: Filling rectangle


    Private Sub Form1_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs)

        ' Create brushes
        Dim blueBrush As New SolidBrush(Color.Blue)

        ' Create a rectangle
        Dim rect As New Rectangle(10, 20, 100, 50)

        ' Fill rectangle
        e.Graphics.FillRectangle(New HatchBrush(HatchStyle.BackwardDiagonal, Color.Yellow, Color.Black), rect)
        e.Graphics.FillRectangle(blueBrush, New Rectangle(150, 20, 50, 100))

        ' Dispose of object
        blueBrush.Dispose()
    End Sub

FillRegion fills a specified region with a brush. This method takes a brush and a region as input parameters. Listing 3.29 creates a Region object from a rectangle and calls FillRegion to fill the region.

LISTING 3.29: Filling regions


Dim rect As New Rectangle(20, 20, 150, 100)
Dim rgn As New Region(rect)
e.Graphics.FillRegion(New SolidBrush(Color.Green), rgn)


Conclusion

Hope the article would have helped you in understanding Fill Methods in GDI+. Read other articles on GDI+ on the website.


Login to add your contents and source code to this article
 About the author
 
Dinesh Beniwal
Looking for C# Consulting?
C# Consulting is founded in 2002 by the founders of C# Corner. Unlike a traditional consulting company, our consultants are well-known experts in .NET and many of them are MVPs, authors, and trainers. We specialize in Microsoft .NET development and utilize Agile Development and Extreme Programming practices to provide fast pace quick turnaround results. Our software development model is a mix of Agile Development, traditional SDLC, and Waterfall models.
Click here to learn more about C# Consulting.
 
Introducing MaxV - one click. infinite control. Hyper-V Hosting from MaximumASP.
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.
Dynamic PDF
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.
Go.NET
Build custom interactive diagrams, network, workflow editors, flowcharts, or software design tools. Includes many predefined kinds of nodes, links, and basic shapes. Supports layers, scrolling, zooming, selection, drag-and-drop, clipboard, in-place editing, tooltips, grids, printing, overview window, palette. 100% implemented in C# as a managed .NET Control. Document/View/Tool architecture with many properties&events. Optional automatic layout.
Dundas Software
Dundas Chart for .NET is the most advanced .NET charting package available today.  With an extremely complete feature set, elegant architecture and easy implementation, Dundas Chart can quickly add advanced Charting functionality to enhance and transform ASP.NET and Windows Forms applications.  Whether you are implementing charting into internal projects, or building applications for clients, Dundas Chart offers advanced technology and advanced results to get the most out of data.
Clickatell's SMS Gateway
Clickatell's Developer Solutions allow you to SMS enable any website or application via a range of API's. Learn More about our API connections.
Free access to .NET Memory Management video
Everything you need to know about Garbage Collection, Temporary Objects, Fragmentation, Finalization and common causes of memory leaks in .NET. Watch the video here.
Microsoft Visual Studio 2010 Professional
Microsoft Visual Studio 2010 Professional will launch on April 12, but you can beat the rush and secure your copy today by pre-ordering at the affordable estimated retail price of $549 (US). Pre-order now.
Nevron Chart for .NET 2010.1 Now Available
The leading .NET charting control now features PDF, Flash and Silverlight export, visualization of large datasets and more. Deliver true charting functionality to your BI, Scorecard, Presentation or Scientific apps. Download evaluation now.
Developer-Ready ASP.NET 2.0 Web Hosting with 3 MONTHS FREE
Now supporting .NET 3.0 Framework with Windows Workflow Foundation, Windows Communication Foundation (WCF), Windows Presentation Foundation (WPF), windows CardSpace (WCS)! Providing more flexibility for Developers with Web Services Support and a User/Permission Manger. Also supporting MS SQL 2005/2000 with Real-Time Backups, FREE Automated Attach .MDF Tool, FREE SQL Restore and Shrink SQL DB Tools, and SQL
 
   Print Read/Post comments Post a comment  Similar Articles  
   Email to a friend  Bookmark  Author's other articles  
 
 Post a Feedback, Comment, or Question about this article
Subject:  
Comment:  
Become a Sponsor
 Comments

 Hosted by MaximumASP  |  Found a broken link?  |  Contact Us  |  Terms & conditions  |  Privacy Policy  |  Site Map  |  Suggest an Idea  |  Media Kit
Current Version: 5.2009.6.2
 © 2010  contents copyright of their authors. Rest everything copyright Mindcracker. All rights reserved.