MVC3/Razor thumbnail/resize image ideas?

后端 未结 4 1089
我在风中等你
我在风中等你 2020-12-13 02:39

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

4条回答
  •  难免孤独
    2020-12-13 03:19

    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.

提交回复
热议问题