【问题标题】:How do I resolve images in a HTML file loaded by WebBrowser control如何解析 WebBrowser 控件加载的 HTML 文件中的图像
【发布时间】:2011-02-01 18:36:47
【问题描述】:

我正在使用 Microsoft Visual Studio 2005,我可以使用

加载网页
string exePath = Application.ExecutablePath ;
int n =  exePath.LastIndexOf("\\");
// the web page & image is in the same directory as the executable
string filename = exePath.Substring(0, n)+ "\\switchboard.htm"; 
webBrowser1.DocumentStream = new FileStream(filename, FileMode.Open);

网页的文本加载正常,但是这个

<img src="cardpack1.jpg" height="200" alt="card">

导致图像损坏。

图像文件与 html 文件位于同一目录中。 我什至不确定要谷歌什么。我发现的所有问题似乎都在做 更有趣的事情,例如从资源包中解析图像并尝试动态处理 显示/不显示图像。

所以,任何建议都将不胜感激(即使它只是 ... google for x, y, z)

【问题讨论】:

    标签: c# .net winforms webbrowser-control


    【解决方案1】:

    为什么不直接使用WebBrowser.Navigate 方法。 URI 可以是本地文件。使用 Path 方法创建字符串也是一个好主意。

         string exePath = Application.ExecutablePath;
         string htmPath = Path.Combine( Path.GetDirectoryName(exePath) , "switchboard.htm");
    
         webBrowser1.Navigate(htmPath);
    

    【讨论】:

    • 非常感谢您花时间回答这个问题。您的解决方案几乎是正确的,并且绝对帮助我克服了困难。我需要做的更改是将协议添加到 htmPath。字符串 exePath = Application.ExecutablePath ;字符串 url = "file://" + Path.Combine(Path.GetDirectoryName(exePath), "switchboard.htm"); webBrowser1.Navigate(url);并且使用 Path.Combine 和 Path.GetDirectoryName 比我所做的要好得多。
    猜你喜欢
    • 2011-05-22
    • 1970-01-01
    • 2012-12-29
    • 1970-01-01
    • 2015-03-30
    • 1970-01-01
    • 1970-01-01
    • 2011-10-03
    • 1970-01-01
    相关资源
    最近更新 更多