RectangleF.Union(RectangleF, RectangleF) 方法

定义

创建第三个矩形,它是能够同时包含形成并集的两个矩形的可能的最小矩形。

C#
public static System.Drawing.RectangleF Union (System.Drawing.RectangleF a, System.Drawing.RectangleF b);

参数

a
RectangleF

要合并的矩形。

b
RectangleF

要合并的矩形。

返回

第三个 RectangleF 结构,其中同时包含形成并集的两个矩形。

示例

此示例旨在与 Windows 窗体 一起使用,它需要 PaintEventArgs e,一个OnPaint事件对象。 该代码创建两 RectangleF 个 ,并将它们绘制到黑色和红色的屏幕上。 请注意,它们必须转换为 Rectangle ,以便进行绘制。 然后,代码使用 Union 方法创建第三RectangleFRectangle,将其转换为 ,并将其绘制到蓝色屏幕。 请注意,第三个 (蓝色) 矩形是可以同时包含另外两个矩形的最小矩形:

C#
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,
        secondRectangleF);
             
    // Draw the unionRectangleF to the screen.
    Rectangle unionRect = Rectangle.Truncate(unionRectangleF);
    e.Graphics.DrawRectangle(Pens.Blue, unionRect);
}

注解

当两个矩形中的一个为空,这意味着其所有值都为零时, Union 该方法将返回一个矩形,其起始点为 (0, 0) ,以及非空矩形的高度和宽度。 例如,如果有两个矩形 A = (0, 0;0, 0) ,B = (1, 1;2、2) ,则 A 和 B 的联合 (0、0:2、2) 。

适用于

产品 版本
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 2.0, 2.1