-1

How can I render an HTML image from byte data loaded from a database?

I am retreiving data from database as a byte array. I need to convert that into png format. Is this possible??

below shown is the byte array.

Byte[] imageArray = new byte[0];
MyData = (Byte[])dt.Tables[0].Rows[3]["img"];
Panagiotis Kanavos
  • 120,703
  • 13
  • 188
  • 236
user3432064
  • 115
  • 1
  • 2
  • 7
  • 2
    How can someone upvote this question. It lacks information and is unclear what the expected end result should be. – Patrick Hofman Mar 18 '14 at 09:51
  • I couldn't find the relationship between `imageArray` and `MyData` lol – SuicideSheep Mar 18 '14 at 09:53
  • If I understand well maybe you can use a library like ImageMaigck. I think with this you can construct an image to whatever format given a byte array. – Spyros Mar 18 '14 at 09:54

4 Answers4

0

You can do it by using the convert function. Refer the code below....

Byte[] imageArray = new byte[0];
MyData = (Byte[])dt.Tables[0].Rows[3]["img"];
if (imageArray!= null && imageArray.Length > 0)
{
   string img = Convert.ToBase64String(imageArray, 0, imageArray.Length);
   pictureBox1.ImageUrl = "data:image/png;base64," + img;
}
Alex R.
  • 4,664
  • 4
  • 30
  • 40
Kailas
  • 439
  • 1
  • 5
  • 14
0

You can try the following function

public static Image ByteArrayToImagebyMemoryStream(byte[] imageByte)
{
    MemoryStream ms = new MemoryStream(imageByte);
    Image image = Image.FromStream(ms);
    return image;
}
SuicideSheep
  • 5,260
  • 19
  • 64
  • 117
0

You can try the following function:

System.Drawing.Image newImage;
string strFileName = GetTempFolderName() + "yourfilename.gif";

if (byteArrayIn != null)
{
    using (MemoryStream stream = new MemoryStream(byteArrayIn))
    {
        newImage = System.Drawing.Image.FromStream(stream);
        newImage.Save(strFileName);
        img.Attributes.Add("src", strFileName);
    }
}
else
{
    Response.Write("No image data found!");
}
BenMorel
  • 34,448
  • 50
  • 182
  • 322
Linh Tuan
  • 440
  • 3
  • 11
0

Hope this answer will Help You

public byte[] imageToByteArray(System.Drawing.Image imageIn)
{
 MemoryStream ms = new MemoryStream();
 imageIn.Save(ms,System.Drawing.Imaging.ImageFormat.Gif);
 return  ms.ToArray();
}

public Image byteArrayToImage(byte[] byteArrayIn)
{
     MemoryStream ms = new MemoryStream(byteArrayIn);
     Image returnImage = Image.FromStream(ms);

return returnImage; }

Shailesh
  • 492
  • 3
  • 9
  • 27