Search code examples
imagerazorwebmatrix-3

Display Binary Image In Webmatrix


Could someone please point me to a tutorial/page that will explain how to view a binary image from a database (or if its simple please tell me)?

I'm using cshtml and can query the database but the image part has got me stumped.

Thank you


Solution

  • I found a way and hope this helps anyone with the same question.

    I created a querySingle to return my image binary data in 'var=file'. I then created the following string variables:

    string imageBase64 = Convert.ToBase64String(file.FileContent);
    string imageSrc = string.Format("data:image/png;base64,{0}", imageBase64);
    

    *FileContent being my binary data.

    And finally created the img:

    <img src="@imageSrc" alt="@file.FileName" />
    

    Please up-mark if you find this helpful.