15

I want to resize an image and save this image multiple times with different sizes into a folder. I have tried ImageResizer or CoreCompat.System.Drawing but these libraries not compatible with .Net core 2. I have searched a lot of about this but i can't find any proper solution. like in MVC4 i have used as:

public ActionResult Upload(HttpPostedFileBase file)
{
if (file != null)
{
    var versions = new Dictionary<string, string>();

    var path = Server.MapPath("~/Images/");

    //Define the versions to generate
    versions.Add("_small", "maxwidth=600&maxheight=600&format=jpg";);
    versions.Add("_medium", "maxwidth=900&maxheight=900&format=jpg");
    versions.Add("_large", "maxwidth=1200&maxheight=1200&format=jpg");

    //Generate each version
    foreach (var suffix in versions.Keys)
    {
        file.InputStream.Seek(0, SeekOrigin.Begin);

        //Let the image builder add the correct extension based on the output file type
        ImageBuilder.Current.Build(
            new ImageJob(
                file.InputStream,
                path + file.FileName + suffix,
                new Instructions(versions[suffix]),
                false,
                true));
    }
}

return RedirectToAction("Index");
}

but in Asp.Net core 2.0 i am stuck. i have no idea how can i implement this in .Net core 2. Any one please can help me.

Rana Mujahid
  • 236
  • 1
  • 2
  • 8
  • System.Drawing is a Windows API and as such, not available in Core (which is cross-platform). The best available library at this time is ImageSharp. Make sure you get the prerelease version under SixLabors.ImageSharp from NuGet. – Chris Pratt Apr 06 '18 at 13:06
  • Is ImageSharp is compatible with .Net Core 2.0? – Rana Mujahid Apr 06 '18 at 14:08
  • 1
    [Imageflow.NET](https://github.com/imazen/imageflow-dotnet) is the successor to ImageResizer and works on .NET Core and .NET Standard 2.0. – Lilith River Jul 23 '20 at 20:08

4 Answers4

8

.NET Core 2.0 ships with System.Drawing.Common, which is the official implementation of System.Drawing for .NET Core.

Instead of CoreCompat.System.Drawing, can you try to install System.Drawing.Common and check whether that works?

Frederik Carlier
  • 4,606
  • 1
  • 25
  • 36
6

You could get nuget package SixLabors.ImageSharp (do not forget to tick "Include prereleases" since now they have only beta) and use they library like this. Their GitHub

using SixLabors.ImageSharp;
using SixLabors.ImageSharp.Processing;

// Image.Load(string path) is a shortcut for our default type. 
// Other pixel formats use Image.Load<TPixel>(string path))
using (Image<Rgba32> image = Image.Load("foo.jpg"))
{
    image.Mutate(x => x
         .Resize(image.Width / 2, image.Height / 2)
         .Grayscale());
    image.Save("bar.jpg"); // Automatic encoder selected based on extension.
}
valentasm
  • 2,137
  • 23
  • 24
  • your answer was working great for single pictures but when I use it inside loop it causes issue on line "image.Save("bar.jpg"); ". I think it is cause of memory issue. – Aneeq Azam Khan Nov 06 '19 at 04:30
  • 1
    Hm. I would say try image?.Dispose() in each iteration. If you do a lot of iteration maybe do resizing in memory and save bunch of stream objects to disk. – valentasm Nov 06 '19 at 06:22
  • thanks for your reply but I was leaving an image open not disposing it and that was causing issue. – Aneeq Azam Khan Nov 06 '19 at 06:33
0

Image processing in .NET Core : https://blogs.msdn.microsoft.com/dotnet/2017/01/19/net-core-image-processing/

greyxit
  • 693
  • 3
  • 13
0

Imageflow.NET Server is the .NET Core equivalent to ImageResizer, but is much faster and produces much smaller image files. See https://github.com/imazen/imageflow-dotnet-server

If you are just resizing during upload, or want to write your own middleware, use Imageflow.NET directly. See https://github.com/imazen/imageflow-dotnet

[Disclaimer: I am the author of both ImageResizer and Imageflow]

Lilith River
  • 16,204
  • 2
  • 44
  • 76