↓ Ir para o conteúdo principal

← todas as notas

📎 Webclip

Upload and resize an image with ASP.NET Core and ImageSharp

The post shows a simple way to handle user photo uploads in ASP.NET Core after moving away from ASP.NET MVC. Since WebImage is not available in Core, it uses IFormFile for uploads and ImageSharp for loading, resizing, and saving the image.

Reading notes
#

  • The author needed a new way to let users upload and resize a profile photo during a migration to ASP.NET Core.
  • The old WebImage helper from ASP.NET MVC is not available in ASP.NET Core.
  • A basic upload form can post a file with multipart/form-data to /home/upload.
  • The upload action can accept the file through the IFormFile interface from Microsoft.AspNetCore.Http.
  • ImageSharp is presented as the replacement for WebImage.
  • The image can be loaded from file.OpenReadStream() with Image.Load(...).
  • The Mutate method can resize the image to a fixed size, such as 256 x 256.
  • The resized image can then be saved to a stream or to a file.
  • The full Upload method combines file upload, image loading, resizing, and saving in a short action method.