BennoDual
BennoDual

Reputation: 6259

WPF WebBrowser and special characters like german "umlaute"

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

Upvotes: 3

Views: 8024

Answers (4)

NekoMisaki
NekoMisaki

Reputation: 99

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.

Upvotes: 2

BennoDual
BennoDual

Reputation: 6259

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

Upvotes: 2

Gavin Jones
Gavin Jones

Reputation: 81

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.

Upvotes: 8

Jon Onstott
Jon Onstott

Reputation: 13727

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.

Upvotes: 0

Related Questions