Exemplo n.º 1
0
        // NOTE: This could be changed to return a TransformGroup which would allow a
        //       combination of transforms to be performed on the image
        private Transform GetTransform(BitmapSource source)
        {
            Debug.Assert(source != null);

            var width  = _size.Width;
            var height = _size.Height;

            if (_size.Mode == Mode.Scale)
            {
                // Automatically fill in blank with/height values
                if (width == 0)
                {
                    width = height * source.PixelWidth / source.PixelHeight;
                }
                else if (height == 0)
                {
                    height = width * source.PixelHeight / source.PixelWidth;
                }
                else if (_ignoreRotations)
                {
                    if ((width > height) != (source.PixelWidth > source.PixelHeight))
                    {
                        var temp = width;
                        width  = height;
                        height = temp;
                    }
                }
            }

            var scaleX = UnitHelper.ConvertToScale(width, _size.Unit, source.PixelWidth, source.DpiX);
            var scaleY = UnitHelper.ConvertToScale(height, _size.Unit, source.PixelHeight, source.DpiY);

            if (_size.Mode == Mode.Scale)
            {
                var minScale = Math.Min(scaleX, scaleY);

                scaleX = minScale;
                scaleY = minScale;
            }
            else if (_size.Mode != Mode.Stretch)
            {
                throw new NotSupportedException(String.Format(CultureInfo.InvariantCulture, "The mode '{0}' is not yet supported.", _size.Mode));
            }

            if (_shrinkOnly)
            {
                var maxScale = Math.Max(scaleX, scaleY);

                if (maxScale > 1.0)
                {
                    scaleX = 1.0;
                    scaleY = 1.0;
                }
            }

            return(new ScaleTransform(scaleX, scaleY));
        }