External CSS background-image from external CSS stylesheet

Feb 24, 2014 at 2:26 PM
Edited Feb 24, 2014 at 2:26 PM
I have a very simple HTML page I am pulling in through C# with this code:
string data = "";
string urlAddress = "http://127.0.0.1:81";
try
{
    using (WebClient client = new WebClient()) // WebClient class inherits IDisposable
    {
        data = client.DownloadString(urlAddress);
    }
    Image image = HtmlRenderer.HtmlRender.RenderToImage(data);
}
catch (WebException ex)
{
    //do something if load fails
}
The HTML page has two <link> tags referencing stylesheets with a full URI:
<link href="http://127.0.0.1:81/Content/globals.css" rel="stylesheet" media="screen" />
<link href="http://127.0.0.1:81/Content/themes/1/theme.css" rel="stylesheet" media="screen" />
Then, in the theme.css stylesheet, there is a very simple CSS style:
.outer-container { background-image: url('http://myurlhere.com/background-images/126266045.jpg'); background-repeat: repeat-y; }
No matter what, HTML Renderer will not render that image from the stylesheet. BUT, if I copy that CSS style to the HTML page itself (in between <style> tags) OR use that style inline, the background image is loaded.

Why will an external image not load from a CSS stylesheet?
Developer
Feb 27, 2014 at 8:10 AM
Stupid bug, sorry, will be fixed in next version.

If you need a quick fix replace "CorrectRelativeUrls" method in "StylesheetLoadHandler" with:
int idx = 0;
while( idx != -1 && idx < stylesheet.Length )
{
    idx = stylesheet.IndexOf("url", idx, StringComparison.OrdinalIgnoreCase);
    if( idx > 0 )
    {
        int endIdx = stylesheet.IndexOf(")", idx, StringComparison.OrdinalIgnoreCase);
        if( endIdx > 0 )
        {
            var offset1 = 4 + (stylesheet[idx + 4] == '\'' ? 1 : 0);
            var offset2 = (stylesheet[endIdx -1] == '\'' ? 1 : 0);
            var urlStr = stylesheet.Substring(idx + offset1, endIdx - idx - offset1 - offset2);
            Uri url;
            if( Uri.TryCreate(urlStr, UriKind.Relative, out url) )
            {
                url = new Uri(baseUri, url);
                stylesheet = stylesheet.Remove(idx + 4, endIdx - idx - 4);
                stylesheet = stylesheet.Insert(idx + 4, url.AbsoluteUri);
                idx += url.AbsoluteUri.Length + 4;
            }
            else
            {
                idx = endIdx + 1;
            }
        }
    }
}

return stylesheet;
Feb 27, 2014 at 2:14 PM
No problem, it's such an awesome tool, a slight inconvenience is no biggie! :)

I currently have this installed via Nuget, so I'm not aware of an easy way to replace a method. Do you, by chance, know when the Nuget package will be updated with the most recent fixes? Thanks so much!
Developer
Mar 2, 2014 at 8:09 AM
I will release a minor fixes version in the coming week so if you find more bugs the time is now.
Developer
Mar 7, 2014 at 2:03 PM
done: 1.4.13.0
Marked as answer by ArthurTep on 3/7/2014 at 7:03 AM
Mar 7, 2014 at 2:09 PM
Works a charm, thank you so much! Awesome job on this project.