How can I reference a local resource in the generated HTML in a WinForms WebBrowser control?

I am using the winforms web browser control to display some content in a windows forms application. I use the DocumentText property to write the generated HTML. This part works spectacularly. Now I want to use some images in the markup. (I would also prefer to use related CSS and JavaScript, however, this could be handled simply by nesting it.)

I have been working for several days and can not find the answer to the title question.

I tried using a relative link: the exe application is in bin \ debug. Images are in the "Images" directory at the root of the project. I found that the images will be copied to the output directory during compilation, so they get into bin \ debug \ Images *. Therefore, I then use a link similar to this "Samples ...", thinking that it will refer to exe. However, when I look at the image properties in the built-in browser window, I see the image URL "about: blankImages / *". Everything seems to be like "about: blank" when HTML is written to the control. Lack of location context, I cannot figure out what to use relative resource file for linking.

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

Without writing dynamic HTML results to a real file, is there no other way to link to a file resource?

I am going to try the latter: build absolute paths to images and write them in HTML. My HTML is generated using the XSL transform of a serialized XML object, so I will need to play with some XSL parameters, which will require a bit of extra time, as I am not familiar with them.

+3
winforms webbrowser-control
source share
5 answers

Here's what we do, although I should mention that we use a custom web browser to remove things like the ability to right-click and view the old old IE context menu:

public class HtmlFormatter { /// <summary> /// Indicator that this is a URI referencing the local /// file path. /// </summary> public static readonly string FILE_URL_PREFIX = "file://"; /// <summary> /// The path separator for HTML paths. /// </summary> public const string PATH_SEPARATOR = "/"; } // We need to add the proper paths to each image source // designation that match where they are being placed on disk. String html = HtmlFormatter.ReplaceImagePath( myHtml, HtmlFormatter.FILE_URL_PREFIX + ApplicationPath.FullAppPath + HtmlFormatter.PATH_SEPARATOR); 

Basically, you need to have an image path having a file URI, for example.

 <img src="file://ApplicationPath/images/myImage.gif"> 
+4
source share

I understood.

I just pass the full resolved exe directory url to an XSL transform that contains HTML output with image tags:

 XsltArgumentList lstArgs = new XsltArgumentList(); lstArgs.AddParam("absoluteRoot", string.Empty, Path.GetFullPath(".")); 

Then I just prefix all the images with the parameter value:

 <img src="{$absoluteRoot}/Images/SilkIcons/comment_add.gif" align="middle" border="0" /> 
+1
source share

In the end, I used something that basically was the same as Ken. However, instead of manually adding the file prefix, I used the UriBuilder class to create a complete URI with the file protocol.

This also solved the following problem when we tested the application in a more realistic place, Program Files. Spaces were encoded, but the OS could not deal with encoded characters when the file was referenced using the standard system path (ie "C: \ Program% 20Files ..."). The true value of the URI is used (file: /// C: / Program Files / ...).

0
source share

As an alternative, keep the relative normal-style links, clear the HTML conversion code, and instead insert a C # web server like this into your exe, then point your WebControl to your internal url like localhost: 8199 / myapp /

0
source share

Ken's code lacked a few things that needed to work. I reviewed it and created a new method that should simplify the process a bit.

Just call the static method like this:

 html = HtmlFormatter.ReplaceImagePathAuto(html); 

and all the links in html that correspond to the file: // ApplicationPath / will be replaced by the current working directory. If you want to specify an alternative location, the original static method is enabled (plus the bit that it was missing).

 public class HtmlFormatter { public static readonly string FILE_URL_PREFIX = "file://"; public static readonly string PATH_SEPARATOR = "/"; public static String ReplaceImagePath(String html, String path) { return html.Replace("file://ApplicationPath/", path); } /// <summary> /// Replaces URLs matching file://ApplicationPath/... with Executable Path /// </summary> /// <param name="html"></param> /// <returns></returns> public static String ReplaceImagePathAuto(String html) { String executableName = System.Windows.Forms.Application.ExecutablePath; System.IO.FileInfo executableFileInfo = new System.IO.FileInfo(executableName); String executableDirectoryName = executableFileInfo.DirectoryName; String replaceWith = HtmlFormatter.FILE_URL_PREFIX + executableDirectoryName + HtmlFormatter.PATH_SEPARATOR; return ReplaceImagePath(html, replaceWith); } } 
0
source share

All Articles