RectangleF.Union Method (RectangleF, RectangleF)


The .NET API Reference documentation has a new home. Visit the .NET API Browser on to see the new experience.

Creates the smallest possible third rectangle that can contain both of two rectangles that form a union.

Namespace:   System.Drawing
Assembly:  System.Drawing (in System.Drawing.dll)

public static RectangleF Union(
	RectangleF a,
	RectangleF b


Type: System.Drawing.RectangleF

A rectangle to union.

Type: System.Drawing.RectangleF

A rectangle to union.

Return Value

Type: System.Drawing.RectangleF

A third RectangleF structure that contains both of the two rectangles that form the union.

When one of the two rectangles is empty, meaning all of its values are zero, the Union method returns a rectangle with a starting point of (0, 0), and the height and width of the non-empty rectangle. For example, if you have two rectangles A = (0, 0; 0, 0) and B = (1, 1; 2, 2), then the union of A and B is (0, 0; 2, 2).

This example is designed for use with Windows Forms, and it requires PaintEventArgs e, an OnPaint event object. The code creates two RectangleF s and draws them to the screen in black and red. Notice that they have to be converted to Rectangle s for drawing purposes. Then the code creates a third RectangleF using the Union method, converts it to a Rectangle, and draws it to the screen in blue. Notice the third (blue) rectangle is the smallest possible rectangle that can contain both of the other two rectangles:

public void RectangleFUnionExample(PaintEventArgs e)

    // Create two rectangles and draw them to the screen.
    RectangleF firstRectangleF = new RectangleF(0, 0, 75, 50);
    RectangleF secondRectangleF = new RectangleF(100, 100, 20, 20);

    // Convert the RectangleF structures to Rectangle structures and draw them to the

    // screen.
    Rectangle firstRect = Rectangle.Truncate(firstRectangleF);
    Rectangle secondRect = Rectangle.Truncate(secondRectangleF);
    e.Graphics.DrawRectangle(Pens.Black, firstRect);
    e.Graphics.DrawRectangle(Pens.Red, secondRect);

    // Get the union rectangle.
    RectangleF unionRectangleF = RectangleF.Union(firstRectangleF,

    // Draw the unionRectangleF to the screen.
    Rectangle unionRect = Rectangle.Truncate(unionRectangleF);
    e.Graphics.DrawRectangle(Pens.Blue, unionRect);

.NET Framework
Available since 1.1
Return to top