0

I created a webView2 object and called a CoreWebView2.NavigateToString(string htmlpage) to render an HTML file (stored in memory as a string). The method renders all the HTML page except that the background image and the button images are not. No issues with the html page; i can see the images with both Edge and chrome browsers.

This issue did not happen with the old WebBrowser object. Is that a bug, or is there any other extra step to make the NavigateToString render images?

double-beep
  • 5,031
  • 17
  • 33
  • 41
Zimo
  • 312
  • 5
  • 21
  • 1
    Where do the images come from? Show the html/css that fetches them. Remember that `NavigateToString` causes the url to be `about:blank`, so you can't use relative links. – Poul Bak Mar 29 '21 at 20:29
  • An alternative approach, which should work for you is to load html from a file. Check my answer here: https://stackoverflow.com/questions/62975196/webview2-control-not-loading-html-string - that will load images, css etc from the same folder with relative links. – Poul Bak Mar 29 '21 at 22:33
  • The images are stored in the C drive. I set the background image in the body this way `background-image: url("C:/ProgramData/MyFolder/image.PNG");`. The images for the buttons are set as fallows : `img src="file://C:\ProgramData\MyFolder\button.ico" ` Again, the HTML/CSS is fine and loads correctly with Edge or Chrome. Something is wrong with the `NavigateToString` method since only the images are missing. Thanks again for your help – Zimo Mar 30 '21 at 00:56
  • I looked at you post and i don't think it is a good solution for me. The application i am working on pulls the screen definitions from the database in order to build the html pages (as strings). I don't see the need to store them on the hard drive then load them back again to the application. I would have also to create a cache folder by user that will store html pages for the entire application. – Zimo Mar 30 '21 at 01:10
  • Ok, try to press 'F12' to get the developer tools open. That might give you some hint, what is wrong. – Poul Bak Mar 30 '21 at 01:24
  • That's an awesome tool. I can see two errors related to the images: "Not allowed to load local resource". It looks like it is a permission issue. Not sure yet why ... – Zimo Mar 30 '21 at 13:13
  • That explains it. It's only allowed to load local resources when the protocol for the html is `file://` The old WebBrowser was based on IE7, which had terrible security. The only solution I can come up with is to move scripts, css and images to a public webserver and load them from there. – Poul Bak Mar 30 '21 at 14:50
  • apparently, this is a known issue that was resolved with the `SetVirtualHostNameToFolderMapping` method. This allows you to assign a local folder to a domain name. I am trying to learn how to set it up ... – Zimo Mar 30 '21 at 15:15
  • 2
    It worked using `SetVirtualHostNameToFolderMapping`. I was able to stick an image on a button. The thing is that the fie:// needs to be replaced with https:// [link](https://laptrinhx.com/taking-the-new-chromium-webview2-control-for-a-spin-in-net-part-1-535259649/) – Zimo Mar 30 '21 at 15:32
  • 2
    I'm glad you found a solution to your problem. However, an actual answer/solution should **not** be edited into your question. In general, you should [edit] the question to *clarify* it, but not to include an answer within it. You should create your own answer with the code/solution you used to solve your problem, and then accept it (the system may require a 48-hour delay before doing so). When you've solved the problem yourself, [answering your own question is encouraged](/help/self-answer). – double-beep Mar 30 '21 at 16:04

0 Answers0