/// <summary>
        /// Inflates the rectangle by specified width and height (can be negative) and automatically clamps rectangle coordinates.
        /// </summary>
        /// <param name="rect">Rectangle to inflate.</param>
        /// <param name="width">Horizontal amount.</param>
        /// <param name="height">Vertical amount.</param>
        /// <param name="constrainedArea">If specified rectangle region will be clamped.</param>
        /// <returns>Inflated rectangle.</returns>
        public static Rectangle Inflate(this Rectangle rect, int width, int height, Size constrainedArea = default(Size))
        {
            Rectangle newRect = new Rectangle
            {
                X = rect.X - width,
                Y = rect.Y - height,
                Width = rect.Width + 2 * width,
                Height = rect.Height + 2 * height
            };

            if (constrainedArea.IsEmpty == false)
                newRect.Intersect(new Rectangle(new Point(), constrainedArea));

            return newRect;
        }
        /// <summary>
        /// Inflates the rectangle by specified width and height (can be negative) and automatically clamps rectangle coordinates.
        /// </summary>
        /// <param name="rect">Rectangle to inflate.</param>
        /// <param name="widthScale">Horizontal scale.</param>
        /// <param name="heightScale">Vertical scale.</param>
        /// <param name="constrainedArea">If specified rectangle region will be clamped.</param>
        /// <returns>Inflated rectangle.</returns>
        public static Rectangle Inflate(this Rectangle rect, double widthScale, double heightScale, Size constrainedArea = default(Size))
        {
            Rectangle newRect = new Rectangle
            {
                X = (int)(rect.X - rect.Width * widthScale / 2),
                Y = (int)(rect.Y - rect.Height * heightScale / 2),
                Width = (int)(rect.Width + rect.Width * widthScale),
                Height = (int)(rect.Height + rect.Height * heightScale)
            };

            if (constrainedArea.IsEmpty == false)
                newRect.Intersect(new Rectangle(new Point(), constrainedArea));

            return newRect;
        }
Esempio n. 3
0
        /// <summary>
        /// Calculates intersected rectangle from specified area (transformed into rectangle with location (0,0))
        /// which can be useful for image area intersections.
        /// </summary>
        /// <param name="rect">Rectangle to intersect.</param>
        /// <param name="area">Maximum bounding box represented as size.</param>
        /// <param name="preserveScale">
        /// If true the size components will be cropped by equal amount.
        /// If false the size ratio will not be checked.
        /// </param>
        /// <returns>Intersected rectangle.</returns>
        public static Rectangle Intersect(this Rectangle rect, Size area, bool preserveScale = false)
        {
            Rectangle newRect = rect.Intersect(new Rectangle(0, 0, area.Width, area.Height), preserveScale);

            return(newRect);
        }