2016-02-19 55 views
0

我在資源文件中有一個圖像對象,我試圖在我的網頁上顯示它無濟於事。我得到的替代文字。爲什麼我的圖像沒有出現

enter image description here

+4

當您檢查瀏覽器中的元素時,標記看起來像什麼? – terbubbs

+3

請不要使用屏幕截圖來顯示代碼,而是實際上將代碼複製到問題中 – NineBerry

回答

2

爲了在使用圖像形式的數據url,僅使用src屬性中的base64編碼圖像數據是不夠的,您還需要使用前綴來告訴瀏覽器數據url正在跟隨。

用途:

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

注意,內容類型(這裏圖像/ PNG)必須適合圖像圖形格式。

4

由於SRC具有開始與 「數據:{圖像/格式}; BASE64,」

嘗試這種情況:

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); 
    } 
    } 
} 
相關問題