ASP.NET MVC: Can't figure out what VirtualPath is?

Posted by Ryan_Pitts on Stack Overflow See other posts from Stack Overflow or by Ryan_Pitts
Published on 2010-04-20T04:59:20Z Indexed on 2010/04/20 5:03 UTC
Read the original article Hit count: 245

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!

© Stack Overflow or respective owner

Related posts about c#

Related posts about asp.net-mvc