简体   繁体   English

为什么我的图像没有出现

[英]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," 由于src必须以“ 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. 为了使用数据url形式的图像,仅在src属性中使用base64编码的图像数据是不够的,还需要使用前缀来告诉浏览器要跟随数据url。

Use: 采用:

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

Note that the content type (here image/png) must fit the image graphics format. 请注意,内容类型(此处为image / png)必须适合图像图形格式。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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