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)代码>虽然不精确…是的,我知道了。。。简单的计算。。谢谢@退潮