Image resize and crop RRS feed

  • Question

  • User477335199 posted


    How I can crop and resize images in .net core?

    Its any chance use Webimage from asp.net mvc ? 

    Wednesday, July 10, 2019 9:19 AM


All replies

  • User753101303 posted


    Good question. Not sure if still current but try perhaps https://devblogs.microsoft.com/dotnet/net-core-image-processing/

    I noticed also once https://www.nuget.org/packages/System.Drawing.Common but I'm not sure if truly cross Platform (my understanding is that this is an additonal package rather directed at making porting easier and that includes both cross Platform or when targeting Windows only, the idea would be to make porting easier but swich later to a "true" Platform agnostic API)

    Make sure to hide this behind your own interface so that you can easily replace this if neede by something else.

    Wednesday, July 10, 2019 9:32 AM
  • User1289604957 posted


    You can use ImageSharp .NET Core library.


    Best regards,


    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, July 10, 2019 5:14 PM
  • User711641945 posted

    Hi PopSeka,


    How I can crop and resize images in .net core?

    You could write your own middleware by using SkiaSharp to crop and resize images.

    1. Install SkiaSharp package.

    2. Download some images for test and put them in the folder(wwwroot/images).

    3.Program ImageResizerMiddleware.cs. In this middleware you need two main action to resize and crop images.

    You could resize the images by using url like: https://localhost:port/images/ImageName?w=500&h=200 ,

    You could crop the default images('w 'and 'h' is used to calculate the aspect ratio) by using url like: https://localhost:port/images/ImageName?w=500&h=200&mode=crop .

    //more logic…
    private ResizeParams GetResizeParams(PathString path, IQueryCollection query)
                ResizeParams resizeParams = new ResizeParams();
                // before we extract, do a quick check for resize params
                resizeParams.hasParams =
                    .GetFields().Where(f => f.Name != "hasParams")
                    .Any(f => query.ContainsKey(f.Name));
                // if no params present, bug out
                if (!resizeParams.hasParams)
                    return resizeParams;
                // extract resize params
                if (query.ContainsKey("format"))
                    resizeParams.format = query["format"];
                    resizeParams.format = path.Value.Substring(path.Value.LastIndexOf('.') + 1);
                if (query.ContainsKey("autorotate"))
                    bool.TryParse(query["autorotate"], out resizeParams.autorotate);
                int quality = 100;
                if (query.ContainsKey("quality"))
                    int.TryParse(query["quality"], out quality);
                resizeParams.quality = quality;
                int w = 0;
                if (query.ContainsKey("w"))
                    int.TryParse(query["w"], out w);
                resizeParams.w = w;
                int h = 0;
                if (query.ContainsKey("h"))
                    int.TryParse(query["h"], out h);
                resizeParams.h = h;
                resizeParams.mode = "max";
                // only apply mode if it's a valid mode and both w and h are specified
                if (h != 0 && w != 0 && query.ContainsKey("mode") && ResizeParams.modes.Any(m => query["mode"] == m))
                    resizeParams.mode = query["mode"];
                return resizeParams;
    private SKBitmap Crop(SKBitmap original, ResizeParams resizeParams)
                var cropSides = 0;
                var cropTopBottom = 0;
                // calculate amount of pixels to remove from sides and top/bottom
                if ((float)resizeParams.w / original.Width < resizeParams.h / original.Height) // crop sides
                    cropSides = original.Width - (int)Math.Round((float)original.Height / resizeParams.h * resizeParams.w);
                    cropTopBottom = original.Height - (int)Math.Round((float)original.Width / resizeParams.w * resizeParams.h);
                // setup crop rect
                var cropRect = new SKRectI
                    Left = cropSides / 2,
                    Top = cropTopBottom / 2,
                    Right = original.Width - cropSides + cropSides / 2,
                    Bottom = original.Height - cropTopBottom + cropTopBottom / 2
                // crop
                SKBitmap bitmap = new SKBitmap(cropRect.Width, cropRect.Height);
                original.ExtractSubset(bitmap, cropRect);
                return bitmap;
    //more logic…

    4.Build Extensions so that we can easily add this middleware to our pipeline:

    public static class ImageResizerMiddlewareExtensions
            public static IServiceCollection AddImageResizer(this IServiceCollection services)
                return services.AddMemoryCache();
            public static IApplicationBuilder UseImageResizer(this IApplicationBuilder builder)
                return builder.UseMiddleware<ImageResizerMiddleware>();


    public void ConfigureServices(IServiceCollection services)
    public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
                //…more logic
             //…more logic

    To work this project, you need any other action(besides Pad() and RotateAndFlip()).More detailed logic you could refer to the source code :


    And the tutorial below may help you better understand the code :



    Its any chance use Webimage from asp.net mvc ? 

    Currently .net core does not support WebImage.

    Reference: https://docs.microsoft.com/en-us/dotnet/api/system.web.helpers.webimage?view=aspnet-webpages-3.2

    Best Regards,


    Friday, July 12, 2019 9:44 AM