Best way to render System.Drawing.Image in ASP.NET MVC 3 View

ⅰ亾dé卋堺 提交于 2019-12-01 01:43:22

问题


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.


回答1:


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"/>


来源:https://stackoverflow.com/questions/11546865/best-way-to-render-system-drawing-image-in-asp-net-mvc-3-view

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!