Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

asp.net display image from byte array

I have a byte array and trying to display image from that.

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.Linq;

namespace RealPortableTerminal
{
public partial class resim : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        PortableTerminalDbEntities entity = new PortableTerminalDbEntities();
        byte[] arr = (from b in entity.Sicil where b.Id == 31 select b.Fotograf).First();

        Image rImage = null;
        using (MemoryStream ms = new MemoryStream(arr))
        {
            rImage = Image.FromStream(ms);
        }
    }
}
}

It underlines FromStream and says 'System.Web.UI.WebControls.Image' does not contain a definition for 'FromStream'. It seems adding System.Data.Linq reference did not changed anything. Am I missing something ? Btw I am pretty sure I take byte array from database correctly.

like image 376
Koray Durudogan Avatar asked Sep 05 '16 22:09

Koray Durudogan


People also ask

How do I display an image in a byte array?

//ImageConverter Class convert Image object to Byte Array. byte[] bytes = (byte[])(new ImageConverter()). ConvertTo(img, typeof(byte[])); //Convert Byte Array to Image and display in PictureBox.

How is an image represented in bytes?

whole bytes (24 bits) are used to represent the colour of each pixel. This is split up into one byte for each of the primary colours of light, red, green and blue. This is because computer displays uses these primary colours of light to display all the different colours we see on the screen.


1 Answers

<img id="img" runat="server" alt=""/>

In code behind

string base64String = Convert.ToBase64String(arr, 0, arr.Length);
img.Src = "data:image/jpg;base64," + base64String;

You wouldn't need MemoryStream.

like image 147
Sami Avatar answered Sep 24 '22 19:09

Sami