ASP.NET MVC image from byte array

前端 未结 2 1643
一整个雨季
一整个雨季 2020-12-14 02:31

currently I have a byte array representing my Image in my ViewModel. I display it with the following code:



        
2条回答
  •  夕颜
    夕颜 (楼主)
    2020-12-14 03:12

    One way is to add this to a new c# class or HtmlExtensionsclass

    public static class HtmlExtensions
    {
        public static MvcHtmlString Image(this HtmlHelper html, byte[] image)
        {
            var img = String.Format("data:image/jpg;base64,{0}", Convert.ToBase64String(image));
            return new MvcHtmlString("");
        }
    }
    

    then you can do this in any view

    @Html.Image(Model.MyImageBytes)
    

提交回复
热议问题