Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

c# System.Windows.Rect struct Width and Height

Tags:

c#

If I do this:

Rect rc = new Rect(0, 0, 10, 5);

I am creating a rectangle at position 0, 0 with a width of 10 and height of 5. So rc.Width is 10 and rc.Heightis 5.

But how come rc.Right is 9 instead of 10 and rc.Bottom is 5 instead of 4?

like image 956
Rex Hui Avatar asked Apr 30 '26 12:04

Rex Hui


1 Answers

EDIT: Actually, Rect also works as you expect, I tried in Visual Studio 2017:

enter image description here

Are you using some other Rect? Below is decompiled code for calculating Bottom:

/// <summary>
/// Bottom Property - This is a read-only alias for Y + Height
/// If this is the empty rectangle, the value will be negative infinity.
/// </summary>
public double Bottom
{
    get
    {
        if (IsEmpty)
        {
            return Double.NegativeInfinity;
        }

        return _y + _height; //notice it's just top + height, no magic
    }
}

Old answer - Use Rectangle. It works as you expect:

Rectangle rc = new Rectangle(0, 0, 10, 5);

enter image description here

like image 170
Neolisk Avatar answered May 02 '26 00:05

Neolisk