public static Rectangle GetRegionBounds(Rectangle[] rects, int startIndex, int length)
{
if (rects.Length == 0) {
return new Rectangle (0, 0, 0, 0);
}
int left = rects[startIndex].Left;
int top = rects[startIndex].Top;
int right = rects[startIndex].Right;
int bottom = rects[startIndex].Bottom;
for (int i = startIndex + 1; i < startIndex + length; ++i) {
Rectangle rect = rects[i];
if (rect.Left < left) {
left = rect.Left;
}
if (rect.Top < top) {
top = rect.Top;
}
if (rect.Right > right) {
right = rect.Right;
}
if (rect.Bottom > bottom) {
bottom = rect.Bottom;
}
}
return Rectangle.FromLTRB (left, top, right, bottom);
}