This article has been excerpted from book "Graphics Programming with GDI+".
The Xor method updates the union of both regions (or rectangles) except the intersection area of the rectangle itself. Replacing Exclude with Xor, as shown in Listing 6.9, generates Figure 6.8.
LISTING 6.9: Using the Xor method of the Region class
//Create Graphics object
Graphics g = this.CreateGraphics();
g.Clear(this.BackColor);
//Create rectangles
Rectangle rect1 = new Rectangle(20, 20, 60, 80);
Rectangle rect2 = new Rectangle(50, 30, 60, 80);
//Create regions
Region rgn1 = new Region(rect1);
Region rgn2 = new Region(rect2);
//Draw rectangles
g.DrawRectangle(Pens.Green, rect1);
g.DrawRectangle(Pens.Black, rect2);
//Xor two regions
rgn1.Xor(rgn2);
//Fill the region after Xoring
g.FillRegion(Brushes.Blue, rgn1);
//Dispose of object
g.Dispose();
The Intersect method is the reverse of Xor. It updates only the intersection region of two regions or rectangles, For example, if you replace line with the following code:
rgn1.Xor(rgn2);
FIGURE 6.8: Using the Xor method of the Region class
FIGURE 6.9: Using the Intersect method of the Region class
with the following code:
rgn1.Intersect (rgn2);
Listing will be as:
//Create Graphics object
Graphics g = this.CreateGraphics();
g.Clear(this.BackColor);
//Create rectangles
Rectangle rect1 = new Rectangle(20, 20, 60, 80);
Rectangle rect2 = new Rectangle(50, 30, 60, 80);
//Create regions
Region rgn1 = new Region(rect1);
Region rgn2 = new Region(rect2);
//Draw rectangles
g.DrawRectangle(Pens.Green, rect1);
g.DrawRectangle(Pens.Black, rect2);
// intersect
rgn1.Intersect(rgn2);
//Fill the region after Xoring
g.FillRegion(Brushes.Blue, rgn1);
//Dispose of object
g.Dispose();
the new output will look like Figure 6.9.
Conclusion
Hope the article would have helped you in understanding the Xor and Intersect Methods in GDI+. Read other articles on GDI+ on the website.
|
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. |