質問

Given two System.Drawing.Rectangle's - how to determine what % of the first rectangle's area the second rectangle covers?

For example, if the second rectangle is half-way positioned to the first one, the result should be 50%.

役に立ちましたか?

解決

You can use the Rectangle.Intersect method to get the intersection rectangle:

Rectangle rect = new Rectangle(firstRect.Location, firstRect.Size);
rect.Intersect(secondRectangle);
var percentage = (rect.Width * rect.Height) * 100f/(firstRect.Width * firstRect.Height);
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top