There are many ways to display image from byte array in MVC as well as normal ASP.Net projects.In this post I'll explain couple of them. 1) Using BASE64 string of the image through ViewBag The first way i s by sending a Base64 string as dataURL through ViewBag. As shown in b elow action method under consideration generates such a Base64 string of the image (often called Data URL) and then pass it to the view via a ViewBag property in MVC . public ActionResult Image() { string path = Server.MapPath("~/images/computer.png"); byte[] imageByteData = System.IO.File.ReadAllBytes(path); string imageBase64Data=Convert.ToBase64String(imageByteData); string imageDataURL= string.Format("data:image/png;base64,{0}", imageBase64Data); ViewBag.ImageData = imageDataURL; return View(); } The above code is a action method of Controller. In this action method it uses physical path of image and read all bytes of that image using ReadAllByte
Comments
Post a Comment