11

How do you get the file stream of an uploaded image (IFormFile) and resize it?

public ActionResult Upload(IFormFile file)
{
    using (var reader = new StreamReader(file.OpenReadStream()))
    {
        var fileContent = reader.ReadToEnd();
        var parsedContentDisposition = ContentDispositionHeaderValue.Parse(file.ContentDisposition);

        //scale image here?
    }
}
Nick De Beer
  • 5,232
  • 6
  • 35
  • 50

3 Answers3

13

You can use IFormFile.OpenReadStream() to get the stream, and then just insert the stream into an Image. For this instance I scaled it to 1024x768.

Image image = Image.FromStream(file.OpenReadStream(), true, true);
var newImage = new Bitmap(1024, 768);
using (var g = Graphics.FromImage(newImage))
{
    g.DrawImage(image , 0, 0, 1024, 768); 
}

You can then use newImage to save or do whatever you want with.

Nick De Beer
  • 5,232
  • 6
  • 35
  • 50
  • 2
    This will work if it targets the full framework and not just the core. I could be wrong but I don't think there's a way to manipulate images in the core yet. http://www.hanselman.com/blog/RFCServersideImageAndGraphicsProcessingWithNETCoreAndASPNET5.aspx (There are enough things missing for me that I targeted 4.6 libraries for reasons like this). – b.pell Jan 27 '16 at 03:40
  • asp.net-core and asp.net 5 are temporary synonyms on stackoverflow for the time being; However, I will update the tag to include asp.net for clarity. I was talking about the full framework (4.6 and core) fyi. Thanks! – Nick De Beer Jan 27 '16 at 03:51
  • 3
    You should take a look at this project (https://github.com/JimBobSquarePants/ImageProcessor) which is currently being updates for ASP.NET Core support. It's got pretty good traction and I believe it to be the only solution to the problem right now. We use it across multiple .NET sites. – Markus Jul 02 '16 at 03:20
  • It's nothing new: https://msdn.microsoft.com/en-us/library/system.drawing.bitmap(v=vs.110).aspx – JoshYates1980 Jan 30 '17 at 04:51
6

Install from nuget : Install-Package SixLabors.ImageSharp

[HttpPost]
public IActionResult Upload(IFormFile file)
{
    //Better use extension method
    string fileName;
    string customRoot = "wwwroot\\Upload\\";
    Directory.CreateDirectory(customRoot);
    var path = Path.Combine(Directory.GetCurrentDirectory(), customRoot,fileName);
    using var image = Image.Load(file.OpenReadStream());
    //100: height
    //100: width
    image.Mutate(x => x.Resize(100, 100));
    image.Save(path); 
    return Ok();
}

for more information : https://blog.elmah.io/upload-and-resize-an-image-with-asp-net-core-and-imagesharp/

Arad Alvand
  • 8,607
  • 10
  • 51
  • 71
Omid Soleiman
  • 246
  • 3
  • 8
0

Very helpful. Thank you so much. This aspect saved me from out of memory exception.I want to make a contribution, if you are using IFormFile interface, then "file" refers to your IFormFile in the code.

This is IFormFile property of my Entity

[NotMapped]
public IFormFile MyImage1 { set; get; }

And this is how i use it

Image image = Image.FromStream(model.MyImage1.OpenReadStream(), true, true);