-5

When I attempted to retrieve Binary (image from database) to byte[] so I can display the image on the webpage.

2 Answers2

1

Sorry for not adding as a comment, dont have enough reputation yet.

why not try store the image as a base-64 string instead of binary. Then you can use the string literal in the webpage directly

<img alt="Embedded Image" src="data:image/png;base64,base64-string-from-database-here" />

If you want to convert an image to base-64 use the steps listed here (Convert image to base64 using javascript)

Or in C# you could use something similar to

using (Image image = Image.FromFile(Path))
{                 
    using (MemoryStream m = new MemoryStream())
    {
        image.Save(m, image.RawFormat);
        byte[] imageBytes = m.ToArray();

        // Convert byte[] to Base64 String
        string base64String = Convert.ToBase64String(imageBytes);
        return base64String;
    }                  
}

Courtesy of @nitin-varpe - Convert Image to base64

Community
  • 1
  • 1
Byren Higgin
  • 552
  • 4
  • 13
  • Thanks for your suggestion. The problem is that I am not using javascript. – maliha majidzada Jan 04 '16 at 00:49
  • All I need is a mechanism to display an image from database to the webpage. The image is stored in database in binary format. – maliha majidzada Jan 04 '16 at 00:51
  • If you are adding the images to the databases manually one at a time you can use an online converter, i just provided a link as a potential solution to your problem.I added a code snippet of how you could potentially do it in C# - I'm just attempting a c# binary to base 64 conversion now – Byren Higgin Jan 04 '16 at 00:52
  • I managed to upload an image to the sql database and I can see it exists in binary format. How can I display that image back on the webpage. I am using WCF Framework, Linq, and I have create models for each page. – maliha majidzada Jan 04 '16 at 01:01
1
        Binary binary = roomModel.RoomImage;
         byte[] bytes= binary.ToArray();

        string strBase64= Convert.ToBase64String(bytes);
        Image1.ImageUrl = "data:Image/png;base64," + strBase64;