Is there an easy and dynamic way to create thumbnails and resize images in MVC3/Razor? A helper, libary, anything?
It would be nice, if I somehow could manage the si
Using WebImage class that comes in System.Web.Helpers.WebImage you can achieve this.
You can use this great kid to output resized images on the fly.
Sample code:
public void GetPhotoThumbnail(int realtyId, int width, int height)
{
// Loading photos’ info from database for specific Realty...
var photos = DocumentSession.Query().Where(f => f.RealtyId == realtyId);
if (photos.Any())
{
var photo = photos.First();
new WebImage(photo.Path)
.Resize(width, height, false, true) // Resizing the image to 100x100 px on the fly...
.Crop(1, 1) // Cropping it to remove 1px border at top and left sides (bug in WebImage)
.Write();
}
// Loading a default photo for realties that don't have a Photo
new WebImage(HostingEnvironment.MapPath(@"~/Content/images/no-photo100x100.png")).Write();
}
In a view you'd have something like this:
More about it here: Resize image on the fly with ASP.NET MVC
I just found this nice tutorial about WebImage at the ASP.NET site:
Working with Images in an ASP.NET Web Pages (Razor) Site.