2

I am having problem when displaying the binary image in MVC view. I stored images in database as binary format and then assigned to ViewBag in Controller. How do I assign viewbag data to image in View?

Controller

            ItemBO mibo = new ItemBO();
            ViewBag.Picture1 = Convert.ToByte(mibo.GetImage(1));

View

           var elem = document.createElement("img");     
           elem.setAttribute("src", @ViewBag.Picture1);

           elem.setAttribute("style", "height:100%");
           elem.setAttribute("alt", "Image");

           document.getElementById("mydiv").appendChild(elem);

Above code doesn't work as I expected. Any help would be appreciated.

1 Answer 1

3

Try following,

string imageBase64 = Convert.ToBase64String(ViewBag.Picture1);
     string imageSrc = string.Format("data:image/gif;base64,{0}", imageBase64);
     <img src="@imageSrc" width="100" height="100" />     
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.