GDI+ Xor and Intersect Methods in VB.NET

In this article I will explain about the Xor and Intersect Methods in GDI+.
  • 3897
 
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

Public Class Form1

    Private Sub Form1_Paint(ByVal sender As System.Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles MyBase.Paint
        'Create Graphics object
        Dim g As Graphics = Me.CreateGraphics()
        g.Clear(Me.BackColor)

        'Create rectangles
        Dim rect1 As New Rectangle(20, 20, 60, 80)
        Dim rect2 As New Rectangle(50, 30, 60, 80)

        'Create regions
        Dim rgn1 As New Region(rect1)
        Dim rgn2 As 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()
    End Sub
End Class

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)

figure6_8.gif

FIGURE 6.8: Using the Xor method of the Region class

figure6_9.gif

FIGURE 6.9: Using the Intersect method of the Region class

with the following code:

rgn1.Intersect (rgn2)

Listing will be as:

Public Class Form1

    Private Sub Form1_Paint(ByVal sender As System.Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles MyBase.Paint
        'Create Graphics object
        Dim g As Graphics = Me.CreateGraphics()
        g.Clear(Me.BackColor)

        'Create rectangles
        Dim rect1 As New Rectangle(20, 20, 60, 80)
        Dim rect2 As New Rectangle(50, 30, 60, 80)

        'Create regions
        Dim rgn1 As New Region(rect1)
        Dim rgn2 As 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()
    End Sub
End Class

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.

Categories

More Articles

© 2020 DotNetHeaven. All rights reserved.