1

I have saved an image in MSSQL server database using the IMAGE format. It shows as bytes from the database, I want to convert that byte stream to image in HTML and display that.

I have followed this tutorial. It shows as Image even though tutorial describes it will display the Image properly it shows Only the "Image" instead of an actual image file.

 <td>

        @{ byte[] photo = item.image;
            string imageSrc = null;
            if (photo != null)
            {
                MemoryStream ms = new MemoryStream();
                ms.Write(photo, 78, photo.Length - 78);
                string imageBase64 = Convert.ToBase64String(ms.ToArray());
                imageSrc = string.Format("data:image/jpeg;base64,{0}", imageBase64);
            }
        }

        <img src="@imageSrc" alt="Image" />

    </td>

Output of the code

enter image description here

Rendered Result Source

enter image description here Please help me, I am a beginner to ASP.net

west user
  • 41
  • 1
  • 7
  • What is the resulting client-side HTML for this? Specifically, what is the resulting `src` value? Is the image a valid JPEG? – David Aug 16 '17 at 15:00
  • @David I have added the resulting image. – west user Aug 16 '17 at 15:03
  • Ok, and how about the HTML? – David Aug 16 '17 at 15:04
  • @David I have mentioned the HTML Code in the code block of the question – west user Aug 16 '17 at 15:53
  • Not the resulting HTML in the browser, you haven't. You've shown code which generates HTML, and you're assuming that it generated what you expect. Don't assume, debug. What is the *actual resulting HTML* that gets generated? – David Aug 16 '17 at 15:54
  • @David Sorry, I have added the rendered HTML code part of screenshot – west user Aug 16 '17 at 16:20
  • Looks like `imageSrc` is empty. Have you confirmed that `photo` isn't `null`? It seems that you have some debugging to do as you have some false assumptions somewhere. – David Aug 16 '17 at 16:25
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/152078/discussion-between-west-user-and-david). – west user Aug 16 '17 at 17:56

1 Answers1

0

Consider creating a separate action that returns the image using the File() helper as mentioned here;

Can an ASP.NET MVC controller return an Image?

Then setting the src to the Url of that new action. This will be more flexible way to do things

Milney
  • 6,253
  • 2
  • 19
  • 33