简体   繁体   中英

why is my image not appearing

I have an image object inside a resource file and I'm trying to display it on my web page to no avail. I'm getting the alt text instead.

在此处输入图片说明

Because the src has to start with "data:{image/format};base64,"

Try this:

public string FlagPic
{
  get
  {
    using (System.IO.MemoryStream m = new System.IO.MemoryStream())
    {
      ClassLibrary1.Resource1.flag.Save(m, ClassLibrary1.Resource1.flag.RawFormat);

      byte[] imageBytes = m.ToArray();    
      base64Image = System.Convert.ToBase64String(imageBytes);

      return String.Format("data:{1};base64,{0}", base64Image, ClassLibrary1.Resource1.flag.RawFormat);
    }
  }
}

In order to use an image in the form of a data url, it is not enough to use the base64 encoded image data in the src attribute, you also need to use a prefix to tell the browser that a data url is following.

Use:

<img src="data:image/png;base64,<%= FlagPic %>" alt="Bla"/>

Note that the content type (here image/png) must fit the image graphics format.

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