public static IImageProcessingContext <TPixel> Resize <TPixel>(this IImageProcessingContext <TPixel> source, Size target) where TPixel : struct, IPixel <TPixel>
        {
            if (target.Width == 0 && target.Height == 0)
            {
                return(source);
            }

            var origin = source.GetCurrentSize();

            if (target.Width == 0)
            {
                var rate = (float)origin.Height / target.Height;
                if (rate <= 1)
                {
                    return(source);
                }
                return(source.Resize <TPixel>((int)(origin.Width / rate), target.Height, KnownResamplers.Bicubic, false));
            }

            if (target.Height == 0)
            {
                var rate = (float)origin.Width / target.Width;
                if (rate <= 1)
                {
                    return(source);
                }
                return(source.Resize <TPixel>(target.Width, (int)(origin.Height / rate), KnownResamplers.Bicubic, false));
            }

            var size = new Size();
            var r    = Math.Max(((float)origin.Width / target.Width), ((float)origin.Height) / target.Height);

            if (r <= 1)
            {
                return(source);
            }
            size.Width  = (int)Math.Round(origin.Width / r);
            size.Height = (int)Math.Round(origin.Height / r);
            if (size.Width == 0)
            {
                size.Width = 1;
            }
            if (size.Height == 0)
            {
                size.Height = 1;
            }
            return(source.Resize <TPixel>(size, KnownResamplers.Bicubic, false));
        }
Exemple #2
0
 private static IImageProcessingContext <Rgba32> ConvertToAvatar(this IImageProcessingContext <Rgba32> processingContext, Size size, float cornerRadius)
 {
     return(processingContext.Resize(new ResizeOptions {
         Size = size,
         Mode = ResizeMode.Crop
     }).Apply(i => ApplyRoundedCorners(i, cornerRadius)));
 }
Exemple #3
0
        private Matrix <float> ApplyFitWithLetterbox(IImageProcessingContext <Rgba32> context, Image <Rgba32> image)
        {
            var    imageAspectRatio = image.Width / (double)image.Height;
            var    boxAspectRatio   = MaxWidth / (double)MaxHeight;
            double scaledWidth      = image.Width;
            double scaledHeight     = image.Height;
            var    drawingPoint     = new Point(0, 0);

            if (imageAspectRatio < boxAspectRatio)
            {
                // box is wider than image
                scaledWidth  = image.Height * boxAspectRatio;
                scaledHeight = image.Height;
                drawingPoint = new Point((int)((scaledWidth - image.Width) / 2), 0);
            }
            else if (boxAspectRatio < imageAspectRatio)
            {
                // image is wider than box
                scaledHeight = image.Width / boxAspectRatio;
                scaledWidth  = image.Width;
                drawingPoint = new Point(0, (int)((scaledHeight - image.Height) / 2));
            }
            context.Resize((int)scaledWidth, (int)scaledHeight);
            context.DrawImage(GraphicsOptions, image, drawingPoint);
            return(GetProjectiveTransformationMatrix((float)scaledWidth, (float)scaledHeight));
        }
 public static IImageProcessingContext ConvertToRounded(this IImageProcessingContext processingContext, Size size, float cornerRadius)
 {
     return(processingContext.Resize(new ResizeOptions
     {
         Size = size,
         Mode = ResizeMode.Crop
     }).ApplyRoundedCorners(cornerRadius));
 }
 private static IImageProcessingContext Crop(this IImageProcessingContext processingContext, Size size)
 {
     return(processingContext.Resize(new ResizeOptions
     {
         Size = size,
         Mode = ResizeMode.Crop
     }));
 }
Exemple #6
0
 internal static void ConvertToAvatar(this IImageProcessingContext processingContext, Size size,
                                      float cornerRadius)
 {
     processingContext.Resize(new ResizeOptions
     {
         Size = size,
         Mode = ResizeMode.Crop
     }).ApplyRoundedCorners(cornerRadius);
 }
Exemple #7
0
        /// <summary>
        /// Evenly pads an image to fit the new dimensions.
        /// </summary>
        /// <param name="source">The source image to pad.</param>
        /// <param name="width">The new width.</param>
        /// <param name="height">The new height.</param>
        /// <returns>The <see cref="IImageProcessingContext"/> to allow chaining of operations.</returns>
        public static IImageProcessingContext Pad(this IImageProcessingContext source, int width, int height)
        {
            var options = new ResizeOptions
            {
                Size    = new Size(width, height),
                Mode    = ResizeMode.BoxPad,
                Sampler = KnownResamplers.NearestNeighbor,
            };

            return(source.Resize(options));
        }
Exemple #8
0
        /// <summary>
        /// Evenly pads an image to fit the new dimensions.
        /// </summary>
        /// <typeparam name="TPixel">The pixel format.</typeparam>
        /// <param name="source">The source image to pad.</param>
        /// <param name="width">The new width.</param>
        /// <param name="height">The new height.</param>
        /// <returns>The <see cref="Image{TPixel}"/>.</returns>
        public static IImageProcessingContext <TPixel> Pad <TPixel>(this IImageProcessingContext <TPixel> source, int width, int height)
            where TPixel : struct, IPixel <TPixel>
        {
            var options = new ResizeOptions
            {
                Size    = new Size(width, height),
                Mode    = ResizeMode.BoxPad,
                Sampler = ResampleMode.NearestNeighbor
            };

            return(source.Resize(options));
        }
Exemple #9
0
        public IImageProcessingContext <Rgba32> Apply(IImageProcessingContext <Rgba32> image)
        {
            Size?size = GetSize();

            if (size == null)
            {
                return(image);
            }
            return(image.Resize(new ResizeOptions
            {
                Size = size.Value,
                Position = _anchor,
                Mode = _mode
            }));
        }
Exemple #10
0
        private static IImageProcessingContext AddCaption(this IImageProcessingContext processingContext, string text, Color color, Color backgroundColor)
        {
            Size imgSize = processingContext.GetCurrentSize();

            float defaultFontSize   = 12;
            float defaultResolution = 645;
            float defaultPadding    = 10;

            float fontSize     = imgSize.Width * defaultFontSize / defaultResolution;
            float padding      = imgSize.Width * defaultPadding / defaultResolution;
            float captionWidth = imgSize.Width - (2 * padding);


            FontCollection collection = new FontCollection();
            FontFamily     family     = collection.Install("Roboto/Roboto-Regular.ttf");
            Font           font       = family.CreateFont(fontSize, FontStyle.Regular);

            // measure the text size
            FontRectangle fontRectangle = TextMeasurer.Measure(text, new RendererOptions(font)
            {
                WrappingWidth = captionWidth
            });

            var location           = new PointF(padding, imgSize.Height + padding);
            var textGraphicOptions = new TextGraphicsOptions()
            {
                TextOptions = { WrapTextWidth = captionWidth }
            };

            var resizeOptions = new ResizeOptions()
            {
                // increse image height to include caption height
                Size     = new Size(imgSize.Width, imgSize.Height + (int)fontRectangle.Height + (int)(2 * padding)),
                Mode     = ResizeMode.BoxPad,
                Position = AnchorPositionMode.Top
            };

            return(processingContext
                   .Resize(resizeOptions)
                   .BackgroundColor(backgroundColor)
                   .DrawText(textGraphicOptions, text, font, color, location));
        }
Exemple #11
0
 protected override void ExecuteResizeOperation(IImageProcessingContext <Rgba32> ctx)
 {
     ctx.Resize(this.DestSize, this.DestSize, KnownResamplers.Bicubic);
 }
Exemple #12
0
 private Matrix <float> ApplyStretchFit(IImageProcessingContext <Rgba32> context, Image <Rgba32> image)
 {
     context.Resize(image.Width, image.Height);
     context.DrawImage(GraphicsOptions, image, new Point(0, 0));
     return(GetProjectiveTransformationMatrix(image.Width, image.Height));
 }