3

I use the WPF WebBrowser Control in my app. I have a file (mht) which contains german umlaute (ä ö ü). Now, I load this this file with .Navigate(path) but the Problem is, that this charactes are not shown correct. How can I solve this?

Best Regards, Thomas

BennoDual
  • 5,865
  • 15
  • 67
  • 153

4 Answers4

8

This is very quirky.

  1. My solution was to put an explicit meta tag in my HTML file - "My Page.html"

    <meta http-equiv='Content-Type' content='text/html;charset=UTF-8'>
    
  2. Then using the standard Web Browser .NET control I then created a URI object first.

    webBrowser1.Url = new Uri("My Page.html");
    
  3. Then draw the page using the refresh method.

    webBrowser1.Refresh();
    

Note if you use the Navigate method directly it fails to pick up the utf-8 directive, but the URI and refresh approach does.

Quirky, but it works.

j0k
  • 22,600
  • 28
  • 79
  • 90
Gavin Jones
  • 81
  • 1
  • 1
  • 1
    Welcome to SO Gavin. Instead of trying to have a nice render for `<` & `>` you should add 4 spaces to highlight the code or put them between `. – j0k Sep 05 '12 at 15:55
  • Item 2. solves my issue (being having special chars in the url path, my url is a local file), was passing a string to Navigate, passing a url instead works. – Harps Apr 27 '21 at 19:07
2

I was faced with this problem this morning and it annoyed me a lot until I found this solution:

Stream stream = new MemoryStream(System.Text.Encoding.Default.GetBytes(Content_Of_HTML_File_In_String)));
webBrowser.NavigateToStream(stream);

Compared to the solution above, you wont expect any "COMException" or something of this sort.

NekoMisaki
  • 101
  • 1
  • 6
2

I have solved it with the following:

    static void webBrowser_LoadCompleted(object sender, System.Windows.Navigation.NavigationEventArgs e) {
        var webBrowser = sender as WebBrowser;
        if(webBrowser == null) {
            return;
        }
        var doc = (IHTMLDocument2)webBrowser.Document;           

        doc.charset = "utf-8";
        webBrowser.Refresh();
    }
Tim Sylvester
  • 22,897
  • 2
  • 80
  • 94
BennoDual
  • 5,865
  • 15
  • 67
  • 153
0

The WebBrowser control uses Internet Explorer internally, whichever version you have on your local PC. If you can fix the problem in IE, it should be fixed in the WebBrowser control.

Jon Onstott
  • 13,499
  • 16
  • 80
  • 133
  • Ok. I have seen, that I can change the Encoding in IE. When I load my file and change the Encoding to UTF-8, all is showing perfect. But I cannot find the place, where I can change the Encoding in the WebBrowser control. Can you help me about that? – BennoDual May 05 '11 at 22:03
  • 1
    In your page you can try setting encoding (charset) in this format: `` – Jon Onstott May 05 '11 at 23:13