字符串 C# 中的 HTML 标记

本文关键字:标记 HTML 中的 字符串 | 更新日期: 2023-09-27 18:31:38

需要来自字符串的 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;

字符串 C# 中的 HTML 标记

尝试直接在 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; 
}