Exemple #1
0
 public ResizeOperation(int?width, int?height, AnchorPositionMode anchor, ResizeMode mode)
 {
     _width  = width;
     _height = height;
     _mode   = mode;
     _anchor = anchor;
 }
Exemple #2
0
 /// <summary>
 /// Resizes the image to a certain width and height. No resizing will be performed if
 /// both width and height are set to <c>null</c>.
 /// </summary>
 /// <param name="width">The desired width. If set to <c>null</c> or <c>0</c>, the image will maintain it's original aspect ratio.</param>
 /// <param name="height">The desired height. If set to <c>null</c> or <c>0</c>, the image will maintain it's original aspect ratio.</param>
 /// <param name="anchor">The anchor position to use (if necessary).</param>
 /// <param name="mode">The resize mode to use.</param>
 /// <returns>The current module instance.</returns>
 public MutateImage Resize(
     int?width,
     int?height,
     AnchorPositionMode anchor = AnchorPositionMode.Center,
     ResizeMode mode           = ResizeMode.BoxPad)
 {
     _operations.Peek().Enqueue(new ResizeOperation(width, height, anchor, mode));
     return(this);
 }
        /// <summary>
        /// Draws a given image as an overlay on another image.
        /// </summary>
        /// <param name="baseImageFilename">Path to the base image.</param>
        /// <param name="watermarkImageFilename">Path to the image to be drawn on the base image.</param>
        /// <param name="anchorPos">Position to draw the watermark image at.</param>
        /// <param name="paddingScale">How much padding to use in positioning.</param>
        /// <param name="watermarkScale">How much to scale the watermark when drawn.</param>
        /// <returns>The given base image with the watermark drawn on it.</returns>
        public static Image WatermarkImage(string baseImageFilename,
                                           string watermarkImageFilename,
                                           AnchorPositionMode anchorPos       = AnchorPositionMode.Bottom,
                                           double paddingPercentageHorizontal = 0.10,
                                           double paddingPercentageVertical   = 0.1,
                                           double watermarkScale = 0.2,
                                           float opacity         = 0.8f)
        {
            var baseImage = Image.Load(baseImageFilename);

            using (var watermarkImage = Image.Load(watermarkImageFilename))
            {
                //resize the source image if it's too small to draw the watermark on
                int resizeWidth  = baseImage.Width;
                int resizeHeight = baseImage.Height;
                while (resizeWidth < watermarkImage.Width || resizeHeight < watermarkImage.Height)
                {
                    resizeWidth  *= 2;
                    resizeHeight *= 2;
                }
                baseImage.Mutate(x => x.Resize(resizeWidth, resizeHeight));

                //scale the watermark so it's proportional in size to the source image
                double scaleFactor = baseImage.Width / (double)watermarkImage.Width * watermarkScale;
                watermarkImage.ResizeProportional(scaleFactor);

                //compute padding
                int paddingHorizontal = (int)(baseImage.Width * paddingPercentageHorizontal);
                int paddingVertical   = (int)(baseImage.Height * paddingPercentageVertical);

                //compute the position to draw the watermark at (based on its top-left corner)
                Point position;
                switch (anchorPos)
                {
                case AnchorPositionMode.BottomRight:
                    position = new Point(baseImage.Width - watermarkImage.Width - paddingHorizontal, baseImage.Height - watermarkImage.Height - paddingVertical);
                    break;

                case AnchorPositionMode.Bottom:
                    position = new Point(baseImage.Width / 2 - watermarkImage.Width / 2, baseImage.Height - watermarkImage.Height - paddingVertical);
                    break;

                case AnchorPositionMode.BottomLeft:
                    position = new Point(paddingHorizontal, baseImage.Height - watermarkImage.Height - paddingVertical);
                    break;

                default:
                    position = new Point(0, 0);
                    break;
                }

                //draw the watermark on the base image with 80% opacity (make this an argument?)
                baseImage.Mutate(x => x.DrawImage(watermarkImage, position, opacity));

                return(baseImage);
            }
        }
        private static AnchorPositionMode GetAnchor(
            ushort orientation,
            CommandCollection commands,
            CommandParser parser,
            CultureInfo culture)
        {
            AnchorPositionMode anchor = parser.ParseValue <AnchorPositionMode>(commands.GetValueOrDefault(Anchor), culture);

            return(ExifOrientationUtilities.Transform(anchor, orientation));
        }
Exemple #5
0
 private static void Resize(Image <Rgba32> image, Size size, AnchorPositionMode anchorPositionModel)
 {
     image.Mutate(ctx => ctx.Resize(new ResizeOptions
     {
         Size     = size,
         Sampler  = new Lanczos2Resampler(),
         Mode     = ResizeMode.Crop,
         Position = anchorPositionModel
     }));
 }
Exemple #6
0
        public void Resize([DPR] int width, [DPR] int height, Utils.FilterTypes.ResizeMode mode, Utils.FilterTypes.AnchorPositionMode position)
        {
            ResizeMode mode2 = mode switch
            {
                Utils.FilterTypes.ResizeMode.Max => ResizeMode.Max,
                Utils.FilterTypes.ResizeMode.Min => ResizeMode.Min,
                Utils.FilterTypes.ResizeMode.Stretch => ResizeMode.Stretch,
                Utils.FilterTypes.ResizeMode.Pad => ResizeMode.Pad,
                Utils.FilterTypes.ResizeMode.Crop => ResizeMode.Crop,
                _ => throw new Exception(),
            };

            AnchorPositionMode anchorPositionMode = position switch
            {
                Utils.FilterTypes.AnchorPositionMode.Bottom => AnchorPositionMode.Bottom,
                Utils.FilterTypes.AnchorPositionMode.BottomLeft => AnchorPositionMode.BottomLeft,
                Utils.FilterTypes.AnchorPositionMode.BottomRight => AnchorPositionMode.BottomRight,
                Utils.FilterTypes.AnchorPositionMode.Center => AnchorPositionMode.Center,
                Utils.FilterTypes.AnchorPositionMode.Left => AnchorPositionMode.Left,
                Utils.FilterTypes.AnchorPositionMode.Right => AnchorPositionMode.Right,
                Utils.FilterTypes.AnchorPositionMode.Top => AnchorPositionMode.Top,
                Utils.FilterTypes.AnchorPositionMode.TopLeft => AnchorPositionMode.TopLeft,
                Utils.FilterTypes.AnchorPositionMode.TopRight => AnchorPositionMode.TopRight,
                _ => throw new Exception(),
            };

            //prevent upscaling
            if (width > Context.Image.Width)
            {
                width = Context.Image.Width;
            }

            if (height > Context.Image.Height)
            {
                height = Context.Image.Height;
            }

            Context.Image.Mutate(m => m.Resize(new ResizeOptions()
            {
                Position = anchorPositionMode,
                Mode     = mode2,
                Size     = new Size(width, height)
            }));
        }
Exemple #7
0
        /// <summary>
        /// Transforms the specified anchor value depending on the specified <paramref name="orientation" />.
        /// </summary>
        /// <param name="anchor">The input anchor value.</param>
        /// <param name="orientation">The EXIF orientation.</param>
        /// <returns>
        /// The transformed anchor.
        /// </returns>
        public static AnchorPositionMode Transform(AnchorPositionMode anchor, ushort orientation)

        /*
         *    New anchor position is determined by calculating the direction of the anchor relative to the source image.
         *    In the following example, the TopRight anchor becomes BottomRight
         *                T                              L
         +-----------------------+           +--------------+
         |                      *|           |              |
         |                       |           |              |
         |  L |           TL          | R         |              |
         |                       |           |              |
         |                       |         B |      LB      | T
         |                       |           |              |
         +-----------------------+           |              |
         |                B                       |              |
         |              |
         |             *|
         +--------------+
         |                                                R
         */
        => orientation switch
        {
Exemple #8
0
        public static IImageFilters Resize(this IImageFilters imageUrlBuilder, int width, int height, ResizeMode mode, AnchorPositionMode anchorPosition)
        {
            imageUrlBuilder.Filter($"resize({width},{height},{mode.ToUrlString()},{anchorPosition.ToUrlString()})");

            return(imageUrlBuilder);
        }