views:

927

answers:

4

Question

How can I dynamically resize an image in ASP.NET MVC?

Background

I'm trying to automatically create thumbnails from images that are already on the server. In ASP.NET Webforms, I created a HTTPHandler to do this and added verbs in the web.config for all image extensions to pass through the handler. The handler was nice in that if you wanted the original image, you'd use a typical image tag:

<img src="pic.jpg"/>

But if you wanted a resized image, you'd use:

<img src="pic.jpg?width=320&height=240"/>

Is there a way to duplicate the same behavior in ASP.NET MVC?

+1  A: 

Can't you use the same handler in your ASP.NET MVC code? MVC is built on top of ASP.NET and goes through the same motions.

David Andres
A: 

You can do the same in mvc. You can either use a httphandler as you did before or you create an action that streams the resized image.

If it were me I would create a controller with a resize method.

Chuck Conway
Can you elaborate on what you said about creating an action that streams the resized image?
Daniel T.
Wait, I think I get it. You mean to use RedirectToAction("Thumbnail") that returns a thumbnail, right?
Daniel T.
I'm not at my computer, but there is a "Content" method that streams data. I believe it has a signature that takes a stream or byte array and then has a paramenter for content type. For the content type you would pass "image/jpg." Then all you need is to reference the action in an HTML image element.
Chuck Conway
The above assumes you already have the resized the image in memory as a stream oy byte[]
Chuck Conway
I'd very much prefer the view to resize the image. My opinion is that the controller should only give the view a 'table' of data to display, in this case, a list of all the images that match a criteria, and it's the view's job to handle the image resizing, if necessary.
Daniel T.
+2  A: 

You can definitely reuse the same IHttpHandler. You just need a new IRouteHandler to map the incoming request to the correct handler:

public class ImageRouteHandler : IRouteHandler
{
    public IHttpHandler GetHttpHandler(RequestContext requestContext)
    {
        return new YourImageHttphandler();
    }
}

In your routes, add:

routes.Add("Images", new Route("images/{*file}", new ImageRouteHandler()));

Now any request in /images (e.g. /images/pic.jpg?width=320&height=240) will be handled by your existing handler. Obviously you can change the route pattern to match any path that makes sense, just like a typical MVC route.

Rex M
Will this code work for subdirectories under the images folder?
Daniel T.
@DanielT yes! See the tiny edit to my answer - adding the asterisk to `{*file}` will have that route catch everything under /images, including subdirectories.
Rex M
Hmm, slight problem: this routing only works if I manually type the URL into the address bar, like so: `http://localhost:4692/Content/images/IMG_0146.JPG`. However, I need to embed the image inside a View, and it doesn't go through the routing handler since it's already been routed to the controller.
Daniel T.
@DanielT hmm, not sure what you mean. Each resource on the page, such as `<img src...`, is sent to the server by the browser as a distinct request, and each will be handled separately. Can you describe what you're seeing in more detail?
Rex M
+1  A: 

check the following article. u can find useful

http://jaganeee.wordpress.com/2010/02/10/image-resize-using-asp-net-mvc/

Jagan