protected override Size MeasureOverride()

in SamplesCommon/SamplesCommon/CompositionImage.cs [111:182]


        protected override Size MeasureOverride(Size availableSize)
        {
            Size desiredSize = new Size(0, 0);

            // We override measure to implement similar semantics to the normal XAML Image UIElement
            if (_surface != null)
            {
                Size scaling = new Size(1, 1);
                Size imageSize = _surface.Size;

                // If we're not stretching or have infinite space, request the full surface size
                if (!(Double.IsInfinity(availableSize.Width) && Double.IsInfinity(availableSize.Height)) &&
                    _stretchMode != CompositionStretch.None)
                {
                    // Calculate the amount of horizontal and vertical scaling to fit into available space
                    scaling = new Size(availableSize.Width / imageSize.Width, availableSize.Height / imageSize.Height);


                    //
                    // If we've got infinite space in either dimension, scale by the same amount as the constrained
                    // dimension.
                    //

                    if (Double.IsInfinity(availableSize.Width))
                    {
                        scaling.Width = scaling.Height;
                    }
                    else if (Double.IsInfinity(availableSize.Height))
                    {
                        scaling.Height = scaling.Width;
                    }
                    else
                    {
                        //
                        // We're fitting into a space confined by both width and height, do appropriate scaling
                        // based on the stretch mode.
                        //

                        switch (_stretchMode)
                        {
                            case CompositionStretch.Uniform:
                                scaling.Width = scaling.Height = Math.Min(scaling.Width, scaling.Height);
                                break;
                            case CompositionStretch.UniformToFill:
                                scaling.Width = scaling.Height = Math.Max(scaling.Width, scaling.Height);
                                break;
                            case CompositionStretch.Fill:
                            default:
                                break;
                        }
                    }
                }

                // Apply the scale to get the final desired size
                desiredSize.Width = imageSize.Width * scaling.Width;
                desiredSize.Height = imageSize.Height * scaling.Height;
            }
            else
            {
                // We don't have any content, so default to zero unless a specific size was requested
                if (!Double.IsNaN(Width))
                {
                    desiredSize.Width = Width;
                }
                if (!Double.IsNaN(Height))
                {
                    desiredSize.Height = Height;
                }
            }

            return new Size(Math.Min(availableSize.Width, desiredSize.Width), Math.Min(availableSize.Height, desiredSize.Height));
        }