Пример #1
0
        public static PaddedBox Pad(
            Size source,
            Size bounds,
            CropAnchor anchor = CropAnchor.Center,
            bool upscale      = false)
        {
            var size = Fit(source, bounds, upscale); // Fit if it doesn't fit or needs upscaling...

            // Distribute the padding
            var extraWidth  = bounds.Width - size.Width;
            var extraHeight = bounds.Height - size.Height;

            int top    = extraHeight / 2,
                right  = extraWidth / 2,
                bottom = extraHeight / 2,
                left   = extraWidth / 2;

            if (anchor.HasFlag(CropAnchor.Left))
            {
                right = extraWidth;
                left  = 0;
            }
            else if (anchor.HasFlag(CropAnchor.Right))
            {
                left  = extraWidth;
                right = 0;
            }
            else
            {
                // If we split on a pixel, add 1px to the left
                if (extraWidth % 2 == 1)
                {
                    left += 1;
                }
            }

            if (anchor.HasFlag(CropAnchor.Top))
            {
                bottom = extraHeight;
                top    = 0;
            }
            else if (anchor.HasFlag(CropAnchor.Bottom))
            {
                top    = extraHeight;
                bottom = 0;
            }
            else
            {
                // If we split on a pixel, add 1px to the top
                if (extraHeight % 2 == 1)
                {
                    top += 1;
                }
            }

            var padding = new Padding(top, right, bottom, left);

            return(new PaddedBox(size, padding));
        }
Пример #2
0
        // Align the box within the bounds

        public static void Align(ref Rectangle box, Size bounds, CropAnchor anchor)
        {
            double x = 0d,
                   y = 0d;

            // There's horizontal space
            if (box.Width < bounds.Width)
            {
                if (anchor.HasFlag(CropAnchor.Left))
                {
                    x = 0;
                }
                else if (anchor.HasFlag(CropAnchor.Right))
                {
                    x = bounds.Width - box.Width;
                }
                else // center
                {
                    x = (bounds.Width - box.Width) / 2d;
                }
            }

            // There's vertical space
            if (box.Height < bounds.Height)
            {
                if (anchor.HasFlag(CropAnchor.Top))
                {
                    y = 0;
                }
                else if (anchor.HasFlag(CropAnchor.Bottom))
                {
                    y = bounds.Height - box.Height;
                }
                else // Center
                {
                    y = (bounds.Height - box.Height) / 2d;
                }
            }

            box.X = (int)x;
            box.Y = (int)y;
        }
Пример #3
0
        // source is ALWAYS bigger than bounds
        private static Rectangle CalculateCropRectangle(Size source, Size target, CropAnchor anchor)
        {
            int x = 0,
                y = 0;

            // There's veritical space to distribute
            if (source.Height > target.Height)
            {
                if (anchor.HasFlag(CropAnchor.Top))
                {
                    y = 0;
                }
                else if (anchor.HasFlag(CropAnchor.Bottom))
                {
                    y = source.Height - target.Height;
                }
                else // Center
                {
                    y = (int)((source.Height - target.Height) / 2d);
                }
            }

            // There's horizontal space to distribute
            if (source.Width > target.Width)
            {
                if (anchor.HasFlag(CropAnchor.Left))
                {
                    x = 0;
                }
                else if (anchor.HasFlag(CropAnchor.Right))
                {
                    x = source.Width - target.Width;
                }
                else // center
                {
                    x = (int)((source.Width - target.Width) / 2d);
                }
            }

            return(new Rectangle(x, y, target.Width, target.Height));
        }