Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Display bytes as images on an .aspx page

I'm using a database to store clients' images as bytes. How can I render these images on an .aspx page?

like image 638
BreakHead Avatar asked Aug 07 '10 16:08

BreakHead


People also ask

How do I display an image in ASPX?

To upload the image, click on Choose File and then browse to the image which you want to upload. Once the image is selected then the name of the image will be displayed next to the Choose File button as shown in the following screenshot. As you can see the that images.

How do I display an image in a byte array?

To convert a byte array to an image. Create a ByteArrayInputStream object by passing the byte array (that is to be converted) to its constructor. Read the image using the read() method of the ImageIO class (by passing the ByteArrayInputStream objects to it as a parameter).

How to assign byte array to image control in ASP net?

Another way to do it is to convert your byte array into a base 64 string and assign that to the ImageUrl property of rImage , like so: rImage. ImageUrl = "data:image;base64," + Convert. ToBase64String(arr);


2 Answers

Two solutions.

  1. Build a handler page. That takes an ImageID/RowID as GET parameter and returns data with mimetype image/jpeg or image/png.

  2. Use DATA uri scheme as explained on wikipedia.

    <img src="data:image/png;base64, iVBORw0KGgoAAAANSUhEUgAAAAoAAAAKCAYAAACNMs+9AAAABGdBTUEAALGP C/xhBQAAAAlwSFlzAAALEwAACxMBAJqcGAAAAAd0SU1FB9YGARc5KB0XV+IA AAAddEVYdENvbW1lbnQAQ3JlYXRlZCB3aXRoIFRoZSBHSU1Q72QlbgAAAF1J REFUGNO9zL0NglAAxPEfdLTs4BZM4DIO4C7OwQg2JoQ9LE1exdlYvBBeZ7jq ch9//q1uH4TLzw4d6+ErXMMcXuHWxId3KOETnnXXV6MJpcq2MLaI97CER3N0 vr4MkhoXe0rZigAAAABJRU5ErkJggg==" alt="Red dot" />

like image 148
ankitjaininfo Avatar answered Oct 10 '22 01:10

ankitjaininfo


This can be done easily by converting the Byte Array to a Base64 image.

public string GetImageAsBase64String(byte[] bin)
{
    if (bin != null)
    {
        return "<img src=\"data:image/jpeg;base64," + Convert.ToBase64String(bin) + "\">";
    }
    else
    {
       return null;
    }
}

//usage, for demo purposes an uploaded image from a FileUpload Control
Label1.Text = GetImageAsBase64String(FileUpload1.FileBytes);
like image 26
VDWWD Avatar answered Oct 10 '22 01:10

VDWWD