📎 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
WebImagehelper from ASP.NET MVC is not available in ASP.NET Core. - A basic upload form can post a file with
multipart/form-datato/home/upload. - The upload action can accept the file through the
IFormFileinterface fromMicrosoft.AspNetCore.Http. - ImageSharp is presented as the replacement for
WebImage. - The image can be loaded from
file.OpenReadStream()withImage.Load(...). - The
Mutatemethod can resize the image to a fixed size, such as256 x 256. - The resized image can then be saved to a stream or to a file.
- The full
Uploadmethod combines file upload, image loading, resizing, and saving in a short action method.
