我有一個顯示圖像列表的視圖,我現在試圖讓它以縮略圖形式顯示圖像。那麼,我敢肯定,我從自定義ActionResult使用VirtualPath的大部分權利,雖然我似乎無法弄清楚它是什麼使VirtualPath的網址?順便說一句,我使用XML來存儲來自圖像而不是SQL的數據。這裏是我的代碼:ASP.NET MVC:找不出VirtualPath是什麼?
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();
}
}
}
}
}
代碼爲我的控制器:
public ActionResult Thumbnail(string id)
{
return new ThumbnailResult(id);
}
代碼爲我的觀點:
<% 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 />
<% } %>
任何幫助將是從我的自定義的ActionResult
代碼不勝感激!!讓我知道你有什麼問題。 :) 謝謝!