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 classFIGURE 6.9: Using the Intersect method of the Region classwith 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.ConclusionHope the article would have helped you in understanding the Xor and Intersect Methods in GDI+. Read other articles on GDI+ on the website.
You need to be a premium member to use this feature. To access it, you'll have to upgrade your membership.
Become a sharper developer and jumpstart your career.
$0
$
. 00
monthly
For Basic members:
$20
For Premium members:
$45
For Elite members: