Display bytes as images on an .aspx page

后端 未结 3 2029
生来不讨喜
生来不讨喜 2020-12-08 17:23

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

相关标签:
3条回答
  • 2020-12-08 18:01

    Instructions can be found here: http://www.dotnetcurry.com/ShowArticle.aspx?ID=129&AspxAutoDetectCookieSupport=1

    in step 4, but the whole article is worth a read.

    0 讨论(0)
  • 2020-12-08 18:13

    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" />

    0 讨论(0)
  • 2020-12-08 18:18

    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);
    
    0 讨论(0)
提交回复
热议问题