最好的方式呈现在System.Drawing.Image对象ASP.NET MVC 3查看 [英] Best way to render System.Drawing.Image in ASP.NET MVC 3 View
问题描述
我型System.Drawing.Image对象的对象,并想在一个视图中显示此图像。什么是做到这一点的最好方法是什么?
我发现可能适合这种情况的一些自定义HTML辅助方法。还有研究发现,使用,为了拉这样的事情了返回 FileContentResult
新的操作方法的例子。我想知道什么技术是最好的,最容易实现的。
修改
要更具体,我有我要在视图中显示在控制器中为System.Drawing.Image变量的形象。
公众的ActionResult GETIMG()
{
串镜像文件= System.Web.HttpContext.Current。
使用Server.Mappath(〜/内容/ tempimg / unicorn.jpg);
VAR srcImage = Image.FromFile(镜像文件);
使用(VAR连胜=新的MemoryStream())
{
srcImage.Save(连胜,ImageFormat.Png);
返回文件(streak.ToArray(),图像/ PNG);
}
}
现在,你可以把它在这样的观点
< IMG SRC =@ Url.Action(GETIMG,YourControllerName),ALT =替换文本/>
I have an object of type System.Drawing.Image and would like to display this image in a view. What would be the best way to do this?
I have found some custom Html Helper methods that might fit the situation. Also found an example that uses a new action method that returns a FileContentResult
in order to pull something like this off. I would like to know what technique is best and easiest to implement.
EDIT
To be more specific, I have the image in a System.Drawing.Image variable in the Controller that I want to display in the view.
public ActionResult GetImg()
{
string imageFile = System.Web.HttpContext.Current.
Server.MapPath("~/Content/tempimg/unicorn.jpg");
var srcImage = Image.FromFile(imageFile);
using (var streak = new MemoryStream())
{
srcImage.Save(streak, ImageFormat.Png);
return File(streak.ToArray(), "image/png");
}
}
Now you can call it in a view like this
<img src="@Url.Action("GetImg","YourControllerName")" alt="alt text"/>
这篇关于最好的方式呈现在System.Drawing.Image对象ASP.NET MVC 3查看的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!