code_junkie

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

Nov 10th, 2011
128
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.90 KB | None | 0 0
  1. public class HtmlFormatter
  2. {
  3. /// <summary>
  4. /// Indicator that this is a URI referencing the local
  5. /// file path.
  6. /// </summary>
  7. public static readonly string FILE_URL_PREFIX =
  8. "file://";
  9.  
  10. /// <summary>
  11. /// The path separator for HTML paths.
  12. /// </summary>
  13. public const string PATH_SEPARATOR = "/";
  14. }
  15.  
  16.  
  17. // We need to add the proper paths to each image source
  18. // designation that match where they are being placed on disk.
  19. String html = HtmlFormatter.ReplaceImagePath(
  20. myHtml,
  21. HtmlFormatter.FILE_URL_PREFIX + ApplicationPath.FullAppPath +
  22. HtmlFormatter.PATH_SEPARATOR);
  23.  
  24. <img src="file://ApplicationPath/images/myImage.gif">
  25.  
  26. XsltArgumentList lstArgs = new XsltArgumentList();
  27. lstArgs.AddParam("absoluteRoot", string.Empty, Path.GetFullPath("."));
  28.  
  29. <img src="{$absoluteRoot}/Images/SilkIcons/comment_add.gif" align="middle" border="0" />
  30.  
  31. html = HtmlFormatter.ReplaceImagePathAuto(html);
  32.  
  33. public class HtmlFormatter
  34. {
  35.  
  36. public static readonly string FILE_URL_PREFIX = "file://";
  37. public static readonly string PATH_SEPARATOR = "/";
  38. public static String ReplaceImagePath(String html, String path)
  39. {
  40. return html.Replace("file://ApplicationPath/", path);
  41. }
  42. /// <summary>
  43. /// Replaces URLs matching file://ApplicationPath/... with Executable Path
  44. /// </summary>
  45. /// <param name="html"></param>
  46. /// <returns></returns>
  47. public static String ReplaceImagePathAuto(String html)
  48. {
  49. String executableName = System.Windows.Forms.Application.ExecutablePath;
  50. System.IO.FileInfo executableFileInfo = new System.IO.FileInfo(executableName);
  51. String executableDirectoryName = executableFileInfo.DirectoryName;
  52. String replaceWith = HtmlFormatter.FILE_URL_PREFIX
  53. + executableDirectoryName
  54. + HtmlFormatter.PATH_SEPARATOR;
  55.  
  56. return ReplaceImagePath(html, replaceWith);
  57. }
  58. }
Add Comment
Please, Sign In to add comment