-2

需要来自字符串的html标签img

我有字符串

string s = "<img src=\"@Url.Content(\"~/Content/skin/Office2010Blue.png\")\" style=\"width: 100px;height: 100px;\" />";
var html = new HtmlDocument();



@html.CreateElement(s) does not work

我没有图片只有 HtmlAgilityPack.HtmlNode;

4

1 回答 1

0

尝试直接在 img 中使用数据而不是对文件进行衬里

这样你就可以

string s = "<img src=\""+getImageFromFile("~/Content/skin/Office2010Blue.png","image/png")+"\" style=\"width: 100px;height: 100px;\" />";
var html = new HtmlDocument();



@html.CreateElement(s)


public string getImageFromFile(String url, String imgType)
{
    using (FileStream fs = new FileStream(Server.MapPath(url), 
                                   FileMode.Open, 
                                   FileAccess.Read)){
        byte[] filebytes = new byte[fs.Length];
        fs.Read(filebytes, 0, Convert.ToInt32(fs.Length));
    }
    string encodedData = Convert.ToBase64String(filebytes);
    return "data:"+imgType+";base64,+"encodedData; 
}
于 2013-09-17T08:48:47.593 回答