Regions can be made up of Rectangles (both Rectangle and RectangleF), GraphicsPaths, or RegionData. A Region can be "unioned" with Rectangles (both Rectangle and RectangleF), GraphicsPaths, or another Region. In the case of forming the Union of two or more Regions, it is important to make the receiving Region empty.
In the following example, we have an array of Rectangles that contains labels. We want to react to a MouseClick event on any of the labels (later we would determine on which label the MouseClick occurred). A region is the perfect data structure for hit testing. So we want a Region that contains all of the Rectangles that make up labels. Assume that the array of label Rectangles has already been populated.
Rectangle [ ] label;
Region new_label_union ( )
{
Region region = new Region ( );
region.MakeEmpty ( );
for ( int i = 0; ( i < label.Length ); i++ )
{
if ( label [ i ] != null )
{
if ( label [ i ] != Rectangle.Empty )
{
region.Union ( label [ i ] );
}
}
}
return ( region );
}
Note that as soon as we create the Region, we make it empty. Although not necessary for Region.Union ( Rectangle ) it is absolutely necessary for Region.Union ( Region ). So I always use Region.MakeEmpty.
Hope that helps.