I know to Inflate the Rectangle by Pixels, How to by Percentage?

Ex: rect.Inflate(45%,105%) the integer value should be passed as percent value not pixel value

How??

有帮助吗?

解决方案

One option is to use the SizeF structure to allow for you to calculate the width and height values by percentage values, like this:

SizeF theSize = new SizeF(rect.Width * .45, rect.Height * 1.05);

SizeF holds floating point values, but unfortunately there is no overload for Inflate() that accepts a SizeF, but it does have an overload that accepts a Size structure. So we need to convert a SizeF to a Size, like this:

// Round the Size
Size roundedSize = Size.Round(theSize);

// Truncate the Size
Size truncatedSize = Size.Truncate(theSize);

Finally, we can use our converted Size (either the rounded or truncated), like this:

rect.Inflate(roundedSize);

OR

rect.Inflate(truncatedSize);

其他提示

There is no function to do this. You will need to calculate the pixels from the percentage and use those instead.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top