Example #1
0
        /// <inheritdoc />
        public override void Draw()
        {
            base.Draw();

            if (ImageSource == null)
            {
                return;
            }

            Rectangle rect;

            if (KeepAspectRatio)
            {
                // Figure out the ratio
                var size      = Size;
                var imageSize = ImageSource.Size;
                if (imageSize.LengthSquared < 1)
                {
                    return;
                }
                var ratio       = size / imageSize;
                var aspectRatio = ratio.MinValue;

                // Get the new height and width
                var newSize = imageSize * aspectRatio;

                // Calculate the X,Y position of the upper-left corner
                // (one of these will always be zero)
                var newPos = (size - newSize) / 2;

                rect = new Rectangle(newPos, newSize);
            }
            else
            {
                rect = new Rectangle(Vector2.Zero, Size);
            }

            Margin.ShrinkRectangle(ref rect);

            ImageSource.Draw(rect, IsMouseOver ? MouseOverColor : Color, WithAlpha);
        }