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!
From what I can see you are using the
VirtualPath
string member in theThumbnailResult
custom action to identify the last portion of the image url. So for example if your site is located atc:\wwwroot\Galleries\WhereConfusionMeetsConcrete
and the image files are located inside this folder likeimage1.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 theid
parameter. So in order to show a thumbnail forimage1.bmp
in your view you could do this:Of course this assumes that you have a default route like this: