<p>I'm using a winforms webbrowser control to display some content in a windows forms app.  I'm using the DocumentText property to write the generated HTML.  That part is working spectacularly.  Now I want to use some images in the markup.  (I also would prefer to use linked CSS and JavaScript, however, that can be worked around by just embedding it.)</p>

<p>I have been googling over the course of several days and can't seem to find an answer to the title question.  </p>

<p>I tried using a relative reference: the app exe is in the bin\debug. The images live in the "Images" directory at the root of the project.  I've set the images to be copied to the output directory on compile, so they end up in bin\debug\Images*.  So I then use a reference like this "Images..." thinking it will be relative to the exe.  However, when I look at the image properties in the embedded browser window, I see the image URL to be "about:blankImages/*".  Everything seems to be relative to "about:blank" when HTML is written to the control.  Lacking a location context, I can't figure out what to use for a relative file resource reference.</p>

<p>I poked around the properties of the control to see if there is a way to set something to fix this.  I created a blank html page, and pointed the browser at it using the "Navigate()" method, using the full local path to the file.  This worked fine with the browser reporting the local "file:///..." path to the blank page.  Then I again wrote to the browser, this time using Document.Write().  Again, the browser now reports "about:blank" as the URL.</p>

<p>Short of writing the dynamic HTML results to a real file, is there no other way to reference a file resource?</p>

<p>I am going to try one last thing: constructing absolute file paths to the images and writing those to the HTML.  My HTML is being generated using an XSL transform of a serialized object's XML so I'll need to play with some XSL parameters which will take a little extra time as I'm not that familiar with them.</p>

[{'answer_id': 72339, 'author': 'Ken Wootton', 'author_id': 7357, 'author_profile': 'https://Stackoverflow.com/users/7357', 'pm_score': 4, 'selected': True, 'text': '<p>Here\'s what we do, although I should mention that we use a custom web browser to remove such things as the ability to right-click and see the good old IE context menu:</p>\n\n<pre><code>public class HtmlFormatter\n{\n    /// &lt;summary&gt;\n    /// Indicator that this is a URI referencing the local\n    /// file path.\n    /// &lt;/summary&gt;\n    public static readonly string FILE_URL_PREFIX = \n        "file://";\n\n    /// &lt;summary&gt;\n    /// The path separator for HTML paths.\n    /// &lt;/summary&gt;\n    public const string PATH_SEPARATOR = "/";\n}\n\n\n// We need to add the proper paths to each image source\n// designation that match where they are being placed on disk.\nString html = HtmlFormatter.ReplaceImagePath(\n    myHtml, \n    HtmlFormatter.FILE_URL_PREFIX + ApplicationPath.FullAppPath + \n    HtmlFormatter.PATH_SEPARATOR);\n</code></pre>\n\n<p>Basically, you need to have an image path that has a file URI, e.g. </p>\n\n<pre><code>&lt;img src="file://ApplicationPath/images/myImage.gif"&gt;\n</code></pre>\n'}, {'answer_id': 72657, 'author': 'Peter', 'author_id': 5496, 'author_profile': 'https://Stackoverflow.com/users/5496', 'pm_score': 1, 'selected': False, 'text': '<p>I got it figured out.</p>\n\n<p>I just pass the complete resolved url of the exe directory to the XSL transform that contains the HTML output with image tags:</p>\n\n<pre><code>XsltArgumentList lstArgs = new XsltArgumentList();\nlstArgs.AddParam("absoluteRoot", string.Empty, Path.GetFullPath("."));\n</code></pre>\n\n<p>Then I just prefixed all the images with the parameter value:</p>\n\n<pre><code>&lt;img src="{$absoluteRoot}/Images/SilkIcons/comment_add.gif" align="middle" border="0" /&gt;\n</code></pre>\n'}, {'answer_id': 208280, 'author': 'Peter', 'author_id': 5496, 'author_profile': 'https://Stackoverflow.com/users/5496', 'pm_score': 0, 'selected': False, 'text': '<p>I ended up using something that\'s basically the same as what Ken suggested.  However, instead of manually appending the file prefix, I used the UriBuilder class to build the complete URI with the "file" protocol.</p>\n\n<p>This also solved a subsequent problem when we tested the app in a more realistic location, Program Files.  The spaces was encoded, but the OS couldn\'t deal with the encoded characters when the file was referenced using a standard system path (i.e. "C:\\Program%20Files...").  Using the true URI value (file:///C:/Program Files/...) worked.</p>\n'}, {'answer_id': 273840, 'author': 'Roark Fan', 'author_id': 25362, 'author_profile': 'https://Stackoverflow.com/users/25362', 'pm_score': 0, 'selected': False, 'text': '<p>Alternatively, keep your normal style relative links, drop the HTML transforming code and instead embed a C# web server like <a href="http://www.codeplex.com/webserver" rel="nofollow noreferrer">this</a> in your exe, then point your WebControl at your internal URL, like localhost:8199/myapp/</p>\n'}, {'answer_id': 886989, 'author': 'Community', 'author_id': -1, 'author_profile': 'https://Stackoverflow.com/users/-1', 'pm_score': 0, 'selected': False, 'text': '<p>Ken\'s code was missing a few things that it needed to work.  I\'ve revised it, and created a new method that should automate things a little.</p>\n\n<p>Just call the static method as so:</p>\n\n<pre><code>html = HtmlFormatter.ReplaceImagePathAuto(html);\n</code></pre>\n\n<p>and all links in the html that match file://ApplicationPath/ will be swapped with the current working directory.  If you want to specify an alternate location, the original static method is included (plus the bits it was missing).</p>\n\n<pre><code>public class HtmlFormatter\n{\n\n    public static readonly string FILE_URL_PREFIX = "file://";\n    public static readonly string PATH_SEPARATOR = "/";\n    public static String ReplaceImagePath(String html, String path)\n    {\n        return html.Replace("file://ApplicationPath/", path);\n    }\n    /// &lt;summary&gt;\n    /// Replaces URLs matching file://ApplicationPath/... with Executable Path\n    /// &lt;/summary&gt;\n    /// &lt;param name="html"&gt;&lt;/param&gt;\n    /// &lt;returns&gt;&lt;/returns&gt;\n    public static String ReplaceImagePathAuto(String html)\n    {\n        String executableName = System.Windows.Forms.Application.ExecutablePath;\n        System.IO.FileInfo executableFileInfo = new System.IO.FileInfo(executableName);\n        String executableDirectoryName = executableFileInfo.DirectoryName;\n        String replaceWith = HtmlFormatter.FILE_URL_PREFIX\n            + executableDirectoryName\n            + HtmlFormatter.PATH_SEPARATOR;\n\n        return ReplaceImagePath(html, replaceWith);\n    }\n}\n</code></pre>\n'}]
['https://Stackoverflow.com/questions/72103', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/5496/']