简体   繁体   中英

How to convert a byte array to an image?

I tried to convert a byte array to an image but nothing and no error.

byte[] data = user.Properties["thumbnailPhoto"].Value as byte[];
if (data != null)
{
    using (MemoryStream s = new MemoryStream(data))
    {
          Bitmap bmp = new Bitmap(s);
          imbThumbnail.ImageUrl = bmp.ToString();
    }
}

You don't need Bitmap class. All you need is base64 encoded data as below

imbThumbnail.ImageUrl = "data:image/jpeg;base64,"+ Convert.ToBase64String(data);

Try this.

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

Source : C# Image to Byte Array and Byte Array to Image Converter Class

Try this code:

var stream = new MemoryStream(bytes);
var image = Image.FromStream(stream);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM