internal static Size AdjustImageSizeForNewBaseSize(bool allowEnlargement, IResizeDecoratorSettings s, Size newBaseSize, RotateFlipType rotation, ImageDecoratorContext context)
        {
            Size rotatedBaseSize = ImageUtils.IsRotated90(rotation)
                ? new Size(newBaseSize.Height, newBaseSize.Width)
                : newBaseSize;

            if (s.ImageSizeName != ImageSizeName.Custom)
            {
                // If a standard image size is being used, fit to that

                Size sizeBounds = ImageSizeHelper.GetSizeConstraints(s.ImageSizeName);
                if (!allowEnlargement)
                {
                    sizeBounds.Width = Math.Min(rotatedBaseSize.Width, sizeBounds.Width);
                    sizeBounds.Height = Math.Min(rotatedBaseSize.Height, sizeBounds.Height);
                }
                return ImageUtils.GetScaledImageSize(sizeBounds.Width, sizeBounds.Height, rotatedBaseSize);
            }
            else
            {
                // If custom size, but we know the base size, preserve
                // the aspect ratio "skew" (difference in x and y DPI)
                // and pixel area

                Size imageSize = s.ImageSize;
                // Need to get the image size to the non-rotated angle,
                // because s.BaseSize dimensions are always pre-rotation.
                // Although ImageSize has not been fully updated for this
                // decorator yet (that's what we're trying to do here),
                // the width/height gets flipped immediately when a
                // rotation is applied, so rotation is already taken
                // into account.
                if (ImageUtils.IsRotated90(rotation))
                    imageSize = new Size(imageSize.Height, imageSize.Width);

                // If the base size has not been set yet, we have to guess.
                // This basically means the image was inserted using an older
                // build of Writer that did not have the crop feature. Ideally
                // we would use the original image size, but this is expensive
                // to get from here. It just so happens that newBaseSize works
                // for now because the crop dialog defaults to the same aspect
                // ratio as the original image, but if that ever changes this
                // will break.
#if DEBUG
                if (s.BaseSize == null)
                {
                    using (Bitmap bitmap = (Bitmap)Bitmap.FromFile(context.SourceImageUri.LocalPath))
                    {
                        Size size = new Size(Math.Max(1, bitmap.Width / 2),
                                             Math.Max(1, bitmap.Height / 2));
                        Debug.Assert(size.Equals(newBaseSize) || bitmap.Size.Equals(newBaseSize), "Check base size assumptions. Can't use 's.BaseSize ?? newBaseSize', instead must calculate original image size (context.SourceImageUri.LocalPath).");
                    }
                }
#endif
                Size baseSize = s.BaseSize ?? newBaseSize;

                double xFactor = imageSize.Width / (double)baseSize.Width;
                double yFactor = imageSize.Height / (double)baseSize.Height;
                newBaseSize = new Size(
                    (int)Math.Round(xFactor * newBaseSize.Width),
                    (int)Math.Round(yFactor * newBaseSize.Height)
                    );

                // Need to re-apply the rotation if necessary.
                if (ImageUtils.IsRotated90(rotation))
                    newBaseSize = new Size(newBaseSize.Height, newBaseSize.Width);

                // At this point, newBaseSize has the right aspect ratio; we now
                // need to scale it so it uses about the same number of pixels
                // as it did before.

                double factor = (imageSize.Width * imageSize.Height) / (double)(newBaseSize.Width * newBaseSize.Height);
                factor = Math.Sqrt(factor);
                newBaseSize.Width = (int)Math.Round(newBaseSize.Width * factor);
                newBaseSize.Height = (int)Math.Round(newBaseSize.Height * factor);

                if (!allowEnlargement)
                {
                    if (newBaseSize.Width > rotatedBaseSize.Width || newBaseSize.Height > rotatedBaseSize.Height)
                        newBaseSize = ImageUtils.GetScaledImageSize(rotatedBaseSize.Width, rotatedBaseSize.Height, newBaseSize);
                }

                return newBaseSize;
            }
        }
Beispiel #2
0
        internal static Size AdjustImageSizeForNewBaseSize(bool allowEnlargement, IResizeDecoratorSettings s, Size newBaseSize, RotateFlipType rotation, ImageDecoratorContext context)
        {
            Size rotatedBaseSize = ImageUtils.IsRotated90(rotation)
                ? new Size(newBaseSize.Height, newBaseSize.Width)
                : newBaseSize;

            if (s.ImageSizeName != ImageSizeName.Custom)
            {
                // If a standard image size is being used, fit to that

                Size sizeBounds = ImageSizeHelper.GetSizeConstraints(s.ImageSizeName);
                if (!allowEnlargement)
                {
                    sizeBounds.Width  = Math.Min(rotatedBaseSize.Width, sizeBounds.Width);
                    sizeBounds.Height = Math.Min(rotatedBaseSize.Height, sizeBounds.Height);
                }
                return(ImageUtils.GetScaledImageSize(sizeBounds.Width, sizeBounds.Height, rotatedBaseSize));
            }
            else
            {
                // If custom size, but we know the base size, preserve
                // the aspect ratio "skew" (difference in x and y DPI)
                // and pixel area

                Size imageSize = s.ImageSize;
                // Need to get the image size to the non-rotated angle,
                // because s.BaseSize dimensions are always pre-rotation.
                // Although ImageSize has not been fully updated for this
                // decorator yet (that's what we're trying to do here),
                // the width/height gets flipped immediately when a
                // rotation is applied, so rotation is already taken
                // into account.
                if (ImageUtils.IsRotated90(rotation))
                {
                    imageSize = new Size(imageSize.Height, imageSize.Width);
                }

                // If the base size has not been set yet, we have to guess.
                // This basically means the image was inserted using an older
                // build of Writer that did not have the crop feature. Ideally
                // we would use the original image size, but this is expensive
                // to get from here. It just so happens that newBaseSize works
                // for now because the crop dialog defaults to the same aspect
                // ratio as the original image, but if that ever changes this
                // will break.
#if DEBUG
                if (s.BaseSize == null)
                {
                    using (Bitmap bitmap = (Bitmap)Bitmap.FromFile(context.SourceImageUri.LocalPath))
                    {
                        Size size = new Size(Math.Max(1, bitmap.Width / 2),
                                             Math.Max(1, bitmap.Height / 2));
                        Debug.Assert(size.Equals(newBaseSize) || bitmap.Size.Equals(newBaseSize), "Check base size assumptions. Can't use 's.BaseSize ?? newBaseSize', instead must calculate original image size (context.SourceImageUri.LocalPath).");
                    }
                }
#endif
                Size baseSize = s.BaseSize ?? newBaseSize;

                double xFactor = imageSize.Width / (double)baseSize.Width;
                double yFactor = imageSize.Height / (double)baseSize.Height;
                newBaseSize = new Size(
                    (int)Math.Round(xFactor * newBaseSize.Width),
                    (int)Math.Round(yFactor * newBaseSize.Height)
                    );

                // Need to re-apply the rotation if necessary.
                if (ImageUtils.IsRotated90(rotation))
                {
                    newBaseSize = new Size(newBaseSize.Height, newBaseSize.Width);
                }

                // At this point, newBaseSize has the right aspect ratio; we now
                // need to scale it so it uses about the same number of pixels
                // as it did before.

                double factor = (imageSize.Width * imageSize.Height) / (double)(newBaseSize.Width * newBaseSize.Height);
                factor             = Math.Sqrt(factor);
                newBaseSize.Width  = (int)Math.Round(newBaseSize.Width * factor);
                newBaseSize.Height = (int)Math.Round(newBaseSize.Height * factor);

                if (!allowEnlargement)
                {
                    if (newBaseSize.Width > rotatedBaseSize.Width || newBaseSize.Height > rotatedBaseSize.Height)
                    {
                        newBaseSize = ImageUtils.GetScaledImageSize(rotatedBaseSize.Width, rotatedBaseSize.Height, newBaseSize);
                    }
                }

                return(newBaseSize);
            }
        }