public static string GetContentHtml(string html, string directory) { var document = new HtmlDocument(); document.LoadHtml(html); var result = document.DocumentNode.SelectNodes("//img"); foreach (var img in result) { var src = img.GetAttributeValue("src", string.Empty); if (!string.IsNullOrEmpty(src) && src.StartsWith("data:image")) { var f = ContentImageFile.Parse(src); f.FileName = Path.Combine(directory, f.FileName); File.WriteAllBytes(f.FileName, f.Content); img.SetAttributeValue("src", f.FileName); } } return(document.DocumentNode.InnerHtml); }
public static ContentImageFile Parse(string img64) { ContentImageFile cmf = new ContentImageFile(); var temp = img64.Split(','); cmf.Content = Convert.FromBase64String(temp[1]); string ext = Base64ImageHelper.GetExtension(temp[0]); if (string.IsNullOrEmpty(ext)) { throw new Exception($"Format {temp[0]} not supported."); } cmf.FileName = Guid.NewGuid() + ext; return(cmf); }