2011-02-01 72 views
2

加载我使用Microsoft Visual Studio 2005中,我能够加载使用如何解决图像由WebBrowser控件

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); 

网页加载好了的文本网页的HTML文件,但是这个

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

导致一个破碎的图像。

图像文件与html文件位于同一目录中。 我什至不知道谷歌。我发现的所有问题似乎都是在做 这样的事情,比如解决资源束中的图像并试图动态地显示/不显示图像 。

因此,任何建议,将不胜感激(即使只是......谷歌X,Y,Z)

回答

2

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

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

    webBrowser1.Navigate(htmPath); 
+0

非常感谢您花时间回答这个问题。你的解决方案几乎是正确的,绝对帮助我克服过去的困难。我需要做的更改是将协议添加到htmPath。 string exePath = Application.ExecutablePath; string url =“file://”+ Path.Combine(Path.GetDirectoryName(exePath),“switchboard.htm”); webBrowser1.Navigate(url); 并使用Path.Combine和Path.GetDirectoryName比我所做的更好。 – 2011-02-03 21:19:53