Image resizing on the fly in asp.net

前端 未结 4 1551
谎友^
谎友^ 2021-01-14 17:40

For simplicity lets say that I have a web page that needs to display thumbnails of images. The images locations are stored in a database(the images are stored on Amazon S3).

4条回答
  •  清酒与你
    2021-01-14 18:26

    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();
    }
    

    More about it here: Resize image on the fly with ASP.NET MVC


    Here's a great tutorial that shows how to work with WebImage directly from the ASP.NET site:

    Working with Images in an ASP.NET Web Pages (Razor) Site

提交回复
热议问题