C#.NET如何在WebBrowser控件上显示图像?

时间:2010-05-31 00:50:55

标签: c# .net webbrowser-control

如何在C#/ .NET中的Web浏览器控件上显示图像?我正在做类似

的事情
webBrowser1.DocumentText = "<html><head></head><body><img src=imagelocationURL.png/></body></html>"

但图像没有出现。我做错了什么?

2 个答案:

答案 0 :(得分:3)

我猜两件事中的一件:要么就像codeka所指出的那样,你错过了imagelocationURL.png周围的引号(单引号或双引号)而且标签没有渲染;否则你需要检查.png文件的位置。当然,请添加引号:

webBrowser1.DocumentText = "<html><head></head><body><img src='imagelocationURL.png'/></body></html>" 

然后,尝试硬编码.png文件的路径,看看是否有效:

webBrowser1.DocumentText = "<html><head></head><body><img src='C:/Temp/imagelocationURL.png'/></body></html>"

如果硬编码路径有效,那么您只需要使用代码来提取相当于硬编码的路径。

答案 1 :(得分:0)

如果您可以使用文件中的内容而不是传递整个html内容,则可以通过以下方式轻松实现:

if (File.Exists(filetoopen))
                this.webBrowser1.Url = new Uri(String.Format(filetoopen));
相关问题