Last Updated: February 25, 2016
·
1.613K
· dwimbley

Resize image using WebImage

A useful helper that will allow you to resize images from the file system. Handy for creating avatars on the fly on a user profile for example.

public class HelpersController 
{
    public void GetThumbnail(string img, int width, int height)
    {
        new WebImage(img)
                .Resize(width, height, true, true)
                .Crop(1, 1)
                .Write();
    }
}

To preserve aspect ration and prevent enlarging of the image set the last two parameters for .Resize to true.

.Resize(width, height, true, true)

Usage

<img src="@Url.Action("GetThumbnail", "Helpers", new { img = "/Images/BigImageToResize.png", width = 35, height= 35 })" />