The Complement, Exclude, and Union Methods in GDI+


This article has been excerpted from book "Graphics Programming with GDI+".

We saw the Region class methods in Table 6.1. Now let's use these methods in our applications.

The Complement method updates the portion of a Region object (specified by a rectangle or a region) that does not intersect the specified region. It takes an argument of type Rectangle, RectangleF, GraphicsPath, or Region and updates the region. Listing 6.7 creates two Region objects and draws rectangles with different pens. The Complement method updates only the portion of the first region that falls within the second region.

LISTING 6.7: Using the Complement method of the Region class

      //Create a Graphics object
      Graphics g = this.CreateGraphics();

      //Create two rectangles
      Rectangle rect1 = new Rectangle (20, 20, 60, 80);
      Rectangle rect2 = new Rectangle (50, 30, 60, 80);

      //Create two regions
      Region rgn1 = new Region (rect1);
      Region rgn2 = new Region (rect2);

      //Draw rectangles
      g.DrawRectangle (Pens.Green, rect1);
      g.DrawRectangle (Pens.Black, rect2);

      //Complement can take Rectangle, RectangleF,
      //Region, or GraphicsPath as an argument

      rgn1.Complement (rect2);
      g.FillRegion(Brushes.Blue, rgn1);

      //Dispose of object
      g.Dispose();


Figure 6.5 shows the output from Listing 6.7. Our code updates a portion of rgn1 that doesn't intersect with rgn2. It is useful when you need to update only a specific part of a region. For example, suppose you're writing a shooting game application and you program updates the targets only after gunfire. In this scenario you need to update only the target region, not the entire form.

figure-6.5.gif

FIGURE 6.5: Complementing regions

Figure 6.6.gif

FIGURE 6.6: Excluding regions

The Exclude method updates the part of a region that does not interact with the specified region or rectangle. Like Complement, Exclude takes an argument of type Rectangle, RectangleF, GraphicsPath, or Region and updates the region. Listing 6.8 creates two Region objects and draws rectangles with different pens, then call Exclude.

LISTING 6.8: Using the Exclude method of the Region class


            Graphics g = e.Graphics;
            Rectangle rect1 = new Rectangle(20, 20, 60, 80);
            Rectangle rect2 = new Rectangle(20, 20, 60, 80);
            Region rgn1 = new Region(rect1);
            Region rgn2 = new Region(rect2);
            g.DrawRectangle(Pens.Green, rect1);
            g.DrawRectangle(Pens.Black, rect2);
            rgn1.Exclude(rgn2);
            g.FillRegion(Brushes.Blue, rgn1);


Figure 6.6 shows the output from Listing 6.8. Only the excluded part of the region is updated.

From the code Listing 6.8, replacing the line


            rgn1.Exclude(rect2);

with

            rgn1.Union(rgn2);


produces Figure 6.7, which updates the union of both regions (or rectangles). Like Exclude and Complement, the Union method can take Rectangle, RectangleF, GraphicsPath, or Region as an argument.

Figure 6.7.gif

FIGURE 6.7: Applying Union or regions

Conclusion

Hope the article would have helped you in understanding
the Complement, Exclude, and Union Methods in GDI+. Read other articles on GDI+ on the website.

bookGDI.jpg This book teaches .NET developers how to work with GDI+ as they develop applications that include graphics, or that interact with monitors or printers. It begins by explaining the difference between GDI and GDI+, and covering the basic concepts of graphics programming in Windows.

Up Next
    Ebook Download
    View all
    Learn
    View all