Simple.ImageResizer demo

Upload photo for demo. The image is resized and output as a jpg 90% quality which is a good balance between size and quality. You can also Download the library

700 * 300


500 * 200


100 * 100


How to install

To add this to your site run

            install package simple.imageresizer.mvcextensions
This will add a ImageResult class allowing you to add this controller action to your site
            public class ImagesController : Controller
                [OutputCache(VaryByParam = "*", Duration = 60 * 60 * 24 * 365)]
                public ImageResult Index(string filename, int w = 0, int h = 0)
                    string filepath = Path.Combine(Server.MapPath("~/images2"), filename);
                    return new ImageResult(filepath, w, h);
Notice how you can pass in width and height into the ImageResult constructor. The output cache and some custom code in the ImageResult will agressively cache the image response on the client. The ImageResult class also takes care of caching the resized image to disk in subfolders where the original image is located. Therefore make sure that the user have write access to the image folder. Also you will need to add a custome mvc route to map image requests to your controller action.
                "Image", "images/{filename}",
                new { controller = "Images", action = "Index", filename = "" }
Last you may also need to add this line to your web.config to make sure the mvc pipeline is handling the image request
                <runAllManagedModulesForAllRequests="true" />
Code is hosted at github:
Comments and pull requests are welcome.