As we discussed in Chapter 3, the Graphics class provides methods to clip regions. Using these methods, an application can restrict where graphics objects are drawn. One major use of clipping regions is to repaint only part of a control. In some cases painting an entire form is costly in terms of time and memory resources. Clipping plays a vital role by painting only the desired area. The Graphics class provides the SetClip, ResetClip, IntersectClip, ExcludeClip, and TranslateClip methods to use in clipping operations.
ExcludeClip excludes the area specified by an argument of type Rectangle or a Region and updates the clipping region. Listing 6.11 fills a rectangle, excluding one small rectangle and a region.
Listing 6.11 Using ExcludeClip to clip regions
// Create a Graphics object Graphics g = this.CreateGraphics(); g.Clear(this.BackColor); // Create rectangles Rectangle rect1 = new Rectangle(20, 20, 60, 80); Rectangle rect2 = new Rectangle(100, 100, 30, 40); // Create a region Region rgn1 = new Region(rect2); // Exclude clip g.ExcludeClip(rect1); g.ExcludeClip(rgn1); // Fill rectangle g.FillRectangle(Brushes.Red, 0, 0, 200, 200); // Dispose of object g.Dispose();
Figure 6.11 shows output from Listing 6.11. The small rectangle and small region are not updated.
Figure 6.11. ExcludeClip output
SetClip sets the clipping region of a Graphics object. This method has many overloaded forms and takes parameters of type Rectangle, RectangleF, Region, GraphicsPath, and Graphics with or without the CombineMode enumeration. The CombineMode enumeration defines how different clipping regions can be combined (see Table 6.2).
The ResetClip method resets the clipping region to infinity. Listing 6.12 uses the SetClip, ResetClip, and IntersectClip methods.
Listing 6.12 Using the SetClip, ResetClip, and IntersectClip methods
// Create a Graphics object Graphics g = this.CreateGraphics(); g.Clear(this.BackColor); // Create rectangles and regions Rectangle rect1 = new Rectangle(20, 20, 200, 200); Rectangle rect2 = new Rectangle(100, 100, 200, 200); Region rgn1 = new Region(rect1); Region rgn2 = new Region(rect2); // Call SetClip g.SetClip(rgn1, CombineMode.Exclude); // Call IntersectClip g.IntersectClip(rgn2); // Fill rectangle g.FillRectangle(Brushes.Red, 0, 0, 300, 300); // Call ResetClip g.ResetClip(); // Draw rectangles g.DrawRectangle(Pens.Green, rect1); g.DrawRectangle(Pens.Yellow, rect2); // Dispose of object g.Dispose();
Member |
Description |
---|---|
Complement |
The existing region is replaced by the result of the existing region being removed from the new region. |
Exclude |
The existing region is replaced by the result of the new region being removed from the existing region. |
Intersect |
Two clipping regions are combined, and the result is their intersection. |
Replace |
One clipping region replaces the other. |
Union |
Two clipping regions are combined, and the result is their union. |
Xor |
Two clipping regions are combined, and the result is their union minus their intersection. |
Note
The CombineMode enumeration is defined in the System.Drawing.Drawing2D namespace.
Figure 6.12 shows the output from Listing 6.12.
Figure 6.12. Using Clip methods
TranslateClip translates the clipping region as specified. Listing 6.13 uses the TranslateClip method to translate a region by 20 and 30 points.
Listing 6.13 Using TranslateClip to translate a region
// Create a Graphics object Graphics g = this.CreateGraphics(); g.Clear(this.BackColor); // Create a RectangleF rectangle RectangleF rect1 = new RectangleF(20.0f, 20.0f, 200.0f, 200.0f); // Create a region Region rgn1 = new Region(rect1); // Call SetClip g.SetClip(rgn1, CombineMode.Exclude); float h = 20.0f; float w = 30.0f; // Call TranslateClip with h and w g.TranslateClip(h, w); // Fill rectangle g.FillRectangle(Brushes.Green, 20, 20, 300, 300);
Figure 6.13 shows the output from Listing 6.13.
Figure 6.13. Using TranslateClip
GDI+: The Next-Generation Graphics Interface
Your First GDI+ Application
The Graphics Class
Working with Brushes and Pens
Colors, Fonts, and Text
Rectangles and Regions
Working with Images
Advanced Imaging
Advanced 2D Graphics
Transformation
Printing
Developing GDI+ Web Applications
GDI+ Best Practices and Performance Techniques
GDI Interoperability
Miscellaneous GDI+ Examples
Appendix A. Exception Handling in .NET