コード例 #1
0
ファイル: ImageSizeHelper.cs プロジェクト: sonnemaf/uno
        public static Size MeasureSource(this Image image, Size finalSize, Size imageSize)
        {
            switch (image.Stretch)
            {
            case UniformToFill:
            {
                var childAspectRatio = imageSize.AspectRatio();
                var finalAspectRatio = finalSize.AspectRatio();
                if (childAspectRatio <= finalAspectRatio)
                {
                    // Child wider than parent, so we're using the width to fill
                    // It's also the default mode if aspect ratios are the same
                    imageSize.Width  = finalSize.Width;
                    imageSize.Height = finalSize.Width / childAspectRatio;
                }
                else
                {
                    // child is taller than parent, so where' using the height to fill
                    imageSize.Width  = finalSize.Height * childAspectRatio;
                    imageSize.Height = finalSize.Height;
                }

                break;
            }

            case Uniform:
            {
                var childAspectRatio = imageSize.AspectRatio();
                var finalAspectRatio = finalSize.AspectRatio();
                if (childAspectRatio <= finalAspectRatio)
                {
                    // Child wider than parent, so we're using the height to fill
                    imageSize.Width  = finalSize.Height * childAspectRatio;
                    imageSize.Height = finalSize.Height;
                }
                else
                {
                    // child is taller than parent, so where' using the width to fill
                    imageSize.Width  = finalSize.Width;
                    imageSize.Height = finalSize.Width / childAspectRatio;
                }

                break;
            }

            case Fill:
            {
                imageSize = finalSize;
                break;
            }

                // In case of None, there's no adjustment to make to the size of the image
            }

            return(imageSize);
        }
コード例 #2
0
        private Size GetArrangedImageSize(Size sourceSize, Size targetSize)
        {
            var sourceAspectRatio = sourceSize.AspectRatio();
            var targetAspectRatio = targetSize.AspectRatio();

            switch (Stretch)
            {
            default:
            case Stretch.None:
                return(sourceSize);

            case Stretch.Fill:
                return(targetSize);

            case Stretch.Uniform:
                return(targetAspectRatio > sourceAspectRatio
                                                ? new Size(sourceSize.Width * targetSize.Height / sourceSize.Height, targetSize.Height)
                                                : new Size(targetSize.Width, sourceSize.Height * targetSize.Width / sourceSize.Width));

            case Stretch.UniformToFill:
                return(targetAspectRatio < sourceAspectRatio
                                                ? new Size(sourceSize.Width * targetSize.Height / sourceSize.Height, targetSize.Height)
                                                : new Size(targetSize.Width, sourceSize.Height * targetSize.Width / sourceSize.Width));
            }
        }
コード例 #3
0
        public void CanCalculatePortraitAspectRatio()
        {
            //arrange
            var size = new Size(3, 4);

            //act
            var aspectRatio = size.AspectRatio();

            //assert
            Assert.Equal(0.75, aspectRatio);
        }
コード例 #4
0
        public void CanCalculateLandscapeAspectRatio()
        {
            //arrange
            var size = new Size(3, 2);

            //act
            var aspectRatio = size.AspectRatio();

            //assert
            Assert.Equal(1.5, aspectRatio);
        }
コード例 #5
0
        public void AspectRatioIsCorrect()
        {
            //arrange
            var size = new Size(3, 2);

            //act
            var aspectRatio = size.AspectRatio();

            //assert
            Assert.Equal(1.5, aspectRatio);
        }
コード例 #6
0
        /// <summary>
        /// Resize the render resources
        /// </summary>
        /// <param name="newSize">The <see cref="Size"/> indicating the size to resize to</param>
        public void Resize(Size newSize)
        {
            if (newSize == Resolution)
            {
                return;
            }

            Resolution  = newSize;
            AspectRatio = Resolution.AspectRatio();

            InternalResize(newSize);
        }
コード例 #7
0
            protected override Size MeasureOverride(Size availableSize)
            {
                Size sourceSize = ImageControl.SourceImageSize;

                var hasKnownWidth  = ImageControl.HasKnownWidth(availableSize.Width);
                var hasKnownHeight = ImageControl.HasKnownHeight(availableSize.Height);

                ImageControl._hasFiniteBounds = hasKnownWidth && hasKnownHeight;

                // If one dimension is known and the other isn't, we need to consider uniformity based on the dimension we know.
                // Example: Horizontal=Stretch, Vertical=Top, Stretch=Uniform, SourceWidth=200, SourceHeight=100 (AspectRatio=2)
                //			This Image is Inside a StackPanel (infinite height and width=300).
                //			When being measured, the height can be calculated using the aspect ratio of the source image and the available width.
                //			That means the Measure should return
                //						height = (KnownWidth=300) / (AspectRatio=2) = 150
                //			...and not	height = (SourceHeight=100) = 100
                if (hasKnownWidth ^ hasKnownHeight)
                {
                    var aspectRatio = sourceSize.AspectRatio();
                    var desiredSize = new Size();
                    if (hasKnownWidth)
                    {
                        var knownWidth = ImageControl.GetKnownWidth(availableSize.Width, sourceSize.Width);
                        desiredSize.Width = knownWidth;
                        switch (ImageControl.Stretch)
                        {
                        case Stretch.Uniform:
                            // If sourceSize is empty, aspect ratio is undefined so we return 0.
                            // Since apsect ratio can have a lot of decimal, iOS ceils Image size to 0.5 if it's not a precise size (like 111.111111111)
                            // so the desiredSize will never match the actual size causing an infinite measuring and can freeze the app
                            desiredSize.Height = sourceSize == default(Size) ? 0 : Math.Ceiling((knownWidth / aspectRatio) * 2) / 2;
                            break;

                        case Stretch.None:
                            desiredSize.Height = sourceSize.Height;
                            break;

                        case Stretch.Fill:
                        case Stretch.UniformToFill:
                            desiredSize.Height = double.IsInfinity(availableSize.Height) ? sourceSize.Height : availableSize.Height;
                            break;
                        }

                        if (this.Log().IsEnabled(Microsoft.Extensions.Logging.LogLevel.Debug))
                        {
                            this.Log().Debug(Panel.ToString() + $" measuring with knownWidth={knownWidth} with availableSize={availableSize}, returning desiredSize={desiredSize}");
                        }

                        return(desiredSize);
                    }
                    else if (hasKnownHeight)
                    {
                        var knownHeight = ImageControl.GetKnownHeight(availableSize.Height, sourceSize.Height);
                        desiredSize.Height = knownHeight;
                        switch (ImageControl.Stretch)
                        {
                        case Stretch.Uniform:
                            //If sourceSize is empty, aspect ratio is undefined so we return 0
                            // Since apsect ratio can have a lot of decimal, iOS ceils Image size to 0.5 if it's not a precise size (like 111.111111111)
                            // so the desiredSize will never match the actual size causing an infinite measuring and can freeze the app
                            desiredSize.Width = sourceSize == default(Size) ? 0 : Math.Ceiling(knownHeight * aspectRatio * 2) / 2;
                            break;

                        case Stretch.None:
                            desiredSize.Width = sourceSize.Width;
                            break;

                        case Stretch.Fill:
                        case Stretch.UniformToFill:
                            desiredSize.Width = double.IsInfinity(availableSize.Width) ? sourceSize.Width : availableSize.Width;
                            break;
                        }

                        if (this.Log().IsEnabled(Microsoft.Extensions.Logging.LogLevel.Debug))
                        {
                            this.Log().Debug(Panel.ToString() + $" measuring with knownHeight={knownHeight} with availableSize={availableSize}, returning desiredSize={desiredSize}");
                        }

                        return(desiredSize);
                    }
                }

                if (sourceSize.Width > availableSize.Width || sourceSize.Height > availableSize.Height || (hasKnownWidth && hasKnownHeight))
                {
                    var knownWidth  = ImageControl.GetKnownWidth(availableSize.Width, sourceSize.Width);
                    var knownHeight = ImageControl.GetKnownHeight(availableSize.Height, sourceSize.Height);
                    var knownSize   = new Size(knownWidth, knownHeight);

                    switch (ImageControl.Stretch)
                    {
                    case Stretch.Uniform:
                        var desiredSize = new Size();
                        var aspectRatio = sourceSize.AspectRatio();
                        // Since apsect ratio can have a lot of decimal, iOS ceils Image size to 0.5 if it's not a precise size (like 111.111111111)
                        // so the desiredSize will never match the actual size causing an infinite measuring and can freeze the app
                        desiredSize.Width  = Math.Min(knownWidth, Math.Ceiling(knownHeight * aspectRatio * 2) / 2);
                        desiredSize.Height = Math.Min(knownHeight, Math.Ceiling(knownWidth / aspectRatio * 2) / 2);

                        if (this.Log().IsEnabled(Microsoft.Extensions.Logging.LogLevel.Debug))
                        {
                            this.Log().Debug(Panel.ToString() + $" measuring with knownSize={knownSize} and Stretch.Uniform with availableSize={availableSize}, returning desiredSize={desiredSize}");
                        }

                        return(desiredSize);
                        //TODO: #11103 - For futur needs, for an Image with no specified size, need to implement Stretching: UniformToFill, Fill, None
                        //case Stretch.UniformToFill:
                        //case Stretch.Fill:
                        //case Stretch.None:
                        //return sourceSize;
                    }
                }

                if (this.Log().IsEnabled(Microsoft.Extensions.Logging.LogLevel.Debug))
                {
                    this.Log().Debug(Panel.ToString() + $" measuring with availableSize={availableSize}, returning sourceSize={sourceSize}");
                }

                return(sourceSize);
            }
コード例 #8
0
 public static Size Resize(this Size size, ushort width)
 => new Size(width, (ushort)(width / size.AspectRatio()));