public static List <ImageLinkData> FindAllLocalImageLinksInHtml(string html)
        {
            List <ImageLinkData> links = new List <ImageLinkData>();

            HtmlDocument doc = new HtmlDocument();

            doc.LoadHtml(html);
            HtmlNodeCollection imgNodes = doc.DocumentNode.SelectNodes("//img");

            if (imgNodes == null)
            {
                return(links);
            }

            for (int i = 0; i < imgNodes.Count; i++)
            {
                // Skip if web link
                if (imgNodes[i].GetAttributeValue("src", "").StartsWith("http") ||
                    imgNodes[i].GetAttributeValue("src", "").StartsWith("www"))
                {
                    continue;
                }

                string localPath = imgNodes[i].GetAttributeValue("src", null);

                ImageLinkData imageData = new ImageLinkData
                {
                    LocalImagePath = localPath,
                };

                links.Add(imageData);
            }

            return(links);
        }
Пример #2
0
        public static List <ImageLinkData> FindAllImageLinksInHtml(string html, string rootFolder)
        {
            List <ImageLinkData> links = new List <ImageLinkData>();

            HtmlDocument doc = new HtmlDocument();

            doc.LoadHtml(html);
            HtmlNodeCollection imgNodes = doc.DocumentNode.SelectNodes("//img");

            if (imgNodes == null)
            {
                return(links);
            }

            foreach (HtmlNode node in imgNodes)
            {
                // Skip if web link
                if (node.GetAttributeValue("src", "").StartsWith("http") ||
                    node.GetAttributeValue("src", "").StartsWith("www"))
                {
                    continue;
                }

                string localPath = node.GetAttributeValue("src", null);
                string fullPath  = rootFolder + "/" + localPath;

                // Check if file exists
                if (File.Exists(fullPath))
                {
                    ImageLinkData imageData = new ImageLinkData
                    {
                        LocalImagePath = localPath,
                        FullImagePath  = fullPath
                    };

                    links.Add(imageData);
                }
            }

            return(links);
        }