We're a community of 1077K IT Pros here for help, advice, solutions, professional growth and fun. Join us!
1,076,166 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Start New Discussion Reply to this Discussion

Problem in displaying html pages with images in webbrowser control

I have an webbrowser control on my form. I am able display html files in that control. But my page contains some images if i give absolute path to it then images are displayed. But if i give relative path then images are not shown in the pages.
I have HtmlPages folder located at bin folder.
And i am assigning

FileStream source = new FileStream(@"..\HtmlPages\supportHtml.html", FileMode.Open, FileAccess.Read);
    webBrowser.DocumentStream = source;

If i assign D:\myapp\bin\HtmlPages\file.png then there is no problem.
My images are stored in same folder. If i open html files with webbrowser then images are displayed.
What is the correct path to set ??

4
Contributors
4
Replies
1 Year
Discussion Span
1 Year Ago
Last Updated
5
Views
Royson
Newbie Poster
2 posts since Apr 2010
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0

Hi,

I think that when you use DocumentStream the webBrowser does not know what the root folder is thus only fully qualified paths will work. Try this:

webBrowser.Url = new Uri(@"..\HtmlPages\supportHtml.html");

Hope this helps.

slider212
Newbie Poster
15 posts since Apr 2010
Reputation Points: 10
Solved Threads: 7
Skill Endorsements: 0

I dont have any problem with html files. My control is able to display it. but there are some images in it which are not shown if i give relative path.
I also made an installer for my app with absolute path for image but image are not displayed. :(
I have to set src attribute of img tag within html files. So cant use application.startup to get root path.

Royson
Newbie Poster
2 posts since Apr 2010
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0

Have you set the image proerty "Copy to output Directory" to "Copy always"

ArunKrr
Newbie Poster
1 post since Jan 2011
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0

I understand that this is an old thread and probably the original poster no longer needs the solution. However, I post my solution here in case if someone else has the same problem.

  1. When you add your html files and images into the Visual Studio, open their properties, be sure to select your build action to "None" and select always copy to output folder or copy if newer.
  2. Then in your code, to make your browser navigate to the html file,
    //replace yourfile and yourfolder with your file and folder's names
    Uri uri = new Uri("pack://siteoforigin:,,,/yourfile.html"); //or pack://siteoforigin:,,,/yourfolder/yourfile.html
    webBrowser.Navigate(uri);

In this case, you are putting your html files and stuffs outside your assembly. The good thing about this design is that you do not need to rebuild your assembly every time you change your html file, and any images or css files used by your html files will still be rendered correctly.

hankas
Newbie Poster
2 posts since Nov 2011
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0

This article has been dead for over three months: Start a new discussion instead

Post: Markdown Syntax: Formatting Help
 
You
View similar articles that have also been tagged:
 
© 2013 DaniWeb® LLC
Page rendered in 0.1013 seconds using 2.67MB