题
如何在ASP.NET MVC中动态调整图像大小? 
背景
我正在尝试从服务器上已有的图像自动创建缩略图。在ASP.NET Webforms中,我创建了一个 HTTPHandler 这样做并添加了动词 web.config 所有图像扩展都通过处理程序。处理程序很好,如果你想要原始图像,你会使用一个典型的图像标记:
<img src="pic.jpg"/>
但如果你想要一个调整大小的图像,你可以使用:
<img src="pic.jpg?width=320&height=240"/>
有没有办法在ASP.NET MVC中复制相同的行为?
             
            
            
            
你绝对可以重复使用它 IHttpHandler。你只需要一个新的 IRouteHandler 将传入的请求映射到正确的处理程序:
public class ImageRouteHandler : IRouteHandler
{
    public IHttpHandler GetHttpHandler(RequestContext requestContext)
    {
        return new YourImageHttphandler();
    }
}
在您的路线中,添加:
routes.Add("Images", new Route("images/{*file}", new ImageRouteHandler()));
现在任何请求 /images (例如。 /images/pic.jpg?width=320&height=240)将由您现有的处理程序处理。显然,您可以更改路由模式以匹配任何有意义的路径,就像典型的MVC路由一样。
 
                
运用 WebImage 进来的课程 System.Web.Helpers.WebImage 你可以做到这一点。
您可以使用这个好孩子动态输出调整大小的图像。
示例代码:
public void GetPhotoThumbnail(int realtyId, int width, int height)
{
    // Loading photos’ info from database for specific Realty...
    var photos = DocumentSession.Query<File>().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();
}
在视图中你有这样的东西:
<img src="@Url.Action("GetPhotoThumbnail",
     new { realtyId = item.Id, width = 100, height = 100 })" />
更多相关信息: 使用ASP.NET MVC动态调整图像大小
ASP.NET网站上还有一个很棒的教程: 使用ASP.NET网页(Razor)站点中的图像。
 
                
您可以在mvc中执行相同的操作。您可以像以前一样使用httphandler,也可以创建一个流式调整大小的图像的操作。 
如果是我,我会用resize方法创建一个控制器。