C# 按百分比膨胀矩形

C# 按百分比膨胀矩形,c#,system.drawing,C#,System.drawing,我知道如何按像素对矩形进行充气,如何按百分比进行充气 例如:rect.Inflate(45%,105%)整数值应作为百分比值而不是像素值传递 怎么做?没有函数可以执行此操作。您需要根据百分比计算像素,并使用它们。一个选项是使用SizeF结构,允许您通过百分比值计算宽度和高度值,如下所示: SizeF theSize = new SizeF(rect.Width * .45, rect.Height * 1.05); // Round the Size Size roundedSize = Si

我知道如何按像素对矩形进行充气,如何按百分比进行充气

例如:
rect.Inflate(45%,105%)
整数值应作为百分比值而不是像素值传递


怎么做?

没有函数可以执行此操作。您需要根据百分比计算像素,并使用它们。

一个选项是使用
SizeF
结构,允许您通过百分比值计算宽度和高度值,如下所示:

SizeF theSize = new SizeF(rect.Width * .45, rect.Height * 1.05);
// Round the Size
Size roundedSize = Size.Round(theSize);

// Truncate the Size
Size truncatedSize = Size.Truncate(theSize);
rect.Inflate(roundedSize);
SizeF
保存浮点值,但不幸的是,接受
SizeF
Inflate()
没有重载,但它确实有一个接受
Size
结构的重载。因此,我们需要将
SizeF
转换为
Size
,如下所示:

SizeF theSize = new SizeF(rect.Width * .45, rect.Height * 1.05);
// Round the Size
Size roundedSize = Size.Round(theSize);

// Truncate the Size
Size truncatedSize = Size.Truncate(theSize);
rect.Inflate(roundedSize);
最后,我们可以使用转换后的
大小
(四舍五入或截断),如下所示:

SizeF theSize = new SizeF(rect.Width * .45, rect.Height * 1.05);
// Round the Size
Size roundedSize = Size.Round(theSize);

// Truncate the Size
Size truncatedSize = Size.Truncate(theSize);
rect.Inflate(roundedSize);


直线充气(直线宽度*.45,直线高度*1.05)虽然不精确…是的,我知道了。。。简单的计算。。谢谢@退潮