GetBounds and Other Methods of the Region class in GDI+


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

The IsEmpty method takes a Graphics object as an argument and returns true if a region is empty. Otherwise it returns false. IsInfinite returns true is a region is infinite (otherwise it returns false), and it takes a Graphics object as the only argument.

The MakeEmpty and MakeInfinite methods make a region empty or infinite, respectively. An infitinite region complete covers the area of a control.

The GetBound method returns the bounds of a region. The method also takes a Graphics object as an argument.

The code in Listing 6.10 uses the methods. It makes rgn2 infinite and fills it with a red pen, which fills the entire form with red.

LISTING 6.10: Using GetBounds and other methods of the Region class


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

            //Create rectangles and regions
            Rectangle rect1 = new Rectangle(20, 20, 60, 80);
            Rectangle rect2 = new Rectangle(50, 30, 60, 80);
            Region rgn1 = new Region(rect1);
            Region rgn2 = new Region(rect2);

            //If region is not empty, empty it

            if (!rgn1.IsEmpty(g))
                rgn1.MakeEmpty();

            //If region is not infinite, make it infinite

            if (!rgn2.IsInfinite(g))
                rgn2.MakeInfinite();

            //Get bounds of the infinite region
            RectangleF rect = rgn2.GetBounds(g);

            //Display
            MessageBox.Show(rect.ToString());

            //Fill the region
            g.FillRegion(Brushes.Red, rgn2);

            //Dispose of object
            g.Dispose();


An infinite region's starting coordinates are negative number, and its height and width are large positive numbers, as figure 6.10 shows. Using FillRegion on an infinite region fills the entire form.

Figure 6.10.gif

FIGURE 6.10: Bounds of an infinite region

Conclusion

Hope the article would have helped you in understanding t
he
GetBounds and Other Methods of the Region class 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.


Similar Articles
Mindcracker
Founded in 2003, Mindcracker is the authority in custom software development and innovation. We put best practices into action. We deliver solutions based on consumer and industry analysis.