views:

192

answers:

2

I have a View that displays a list of images and i am now trying to get it to display the images as thumbnails. Well, i'm pretty sure i got most of it right using VirtualPath's from a custom ActionResult although i can't seem to figure out what it is making the VirtualPath url?? BTW, i'm using XML to store the data from the images instead of SQL. Here is my code:

Code from my custom ActionResult:

public class ThumbnailResult : ActionResult
{
    public ThumbnailResult(string virtualPath)
    {
        this.VirtualPath = virtualPath;
    }

    public string VirtualPath { get; set; }

    public override void ExecuteResult(ControllerContext context)
    {
        context.HttpContext.Response.ContentType = "image/bmp";

        string fullFileName =
            context.HttpContext.Server.MapPath("~/Galleries/WhereConfusionMeetsConcrete/" + VirtualPath);
        using (System.Drawing.Image photoImg =
            System.Drawing.Image.FromFile(fullFileName))
        {
            using (System.Drawing.Image thumbPhoto =
                photoImg.GetThumbnailImage(100, 100, null, new System.IntPtr()))
            {
                using (System.IO.MemoryStream ms = new System.IO.MemoryStream())
                {
                    thumbPhoto.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);
                    context.HttpContext.Response.BinaryWrite(ms.ToArray());
                    context.HttpContext.Response.End();
                }
            }
        }
    }
}

Code for my Controller:

public ActionResult Thumbnail(string id)
{
    return new ThumbnailResult(id);
}

Code for my View:

<% foreach (var image in ViewData.Model) { %>

<a href="../Galleries/TestGallery1/<%= image.Path %>"><img src="../Galleries/TestGallery1/thumbnail/<%= image.Path %>" alt="<%= image.Caption %>" /></a>
<br /><br /><%= image.Caption %><br /><br /><br />

<% } %>

Any help would be greatly appreciated!! Let me know of any questions you have as well. :) Thanks!

A: 

From what I can see you are using the VirtualPath string member in the ThumbnailResult custom action to identify the last portion of the image url. So for example if your site is located at c:\wwwroot\Galleries\WhereConfusionMeetsConcrete and the image files are located inside this folder like image1.bmp, image2.bmp, ... you could only pass the image filename to the custom action result constructor which is called in the controller action and passed the id parameter. So in order to show a thumbnail for image1.bmp in your view you could do this:

<img src="<%= Url.RouteUrl(new { controller = "home", action = "Thumbnail", id = "image1.bmp" }) %>" alt="" />

Of course this assumes that you have a default route like this:

routes.MapRoute(
    "Default",
    "{controller}/{action}/{id}",
    new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
Darin Dimitrov
A: 

Thanks for the quick response. In the image link, since i'm looping the results to display every image in the xml file, is there a way make the (id = "image1.bmp") pull from the xml instead of physically declaring it? Would i use image.Path?

Ryan Pitts
btw, i am very new to asp.net mvc and am still learning. The code i am using i got from http://www.joshholmes.com/blog/2009/01/27/returningthumbnailswiththemvcframework/I am using his tutorials on the Simple Photo Gallery.
Ryan Pitts