Web浏览器控制问题(编码和XHTML页面)

发布于 2024-10-12 22:15:43 字数 1171 浏览 4 评论 0原文

我真的开始讨厌这个控件了,似乎有些小任务几乎是不可能完成的。我研究过替代方案,但没有发现任何有用的东西。

我正在开发一个用于编辑电子书的小型应用程序。我正在使用 WebBrowser 控件预览 html 文件。

首先,我非常努力地尝试没有临时文件,所以我在 Memory with Streams 中完成了这一切,修改 html 以包含以 Base64 编码的图像,并将 CSS 直接包含在 Html 中。这样做的问题是大图像无法正确显示,并且它迫使用户至少使用 IE8。

我寻找了其他库,例如 Chrome 或 Firefox 引擎,但两者都太大而无法使用(当我的应用程序小 500k 时,11 Meg 仅用于预览)。

然后我就放弃了只使用临时文件,这样我就能更好地兼容 CSS 和图像并减少问题。

我遇到的问题是某些文件(在某些计算机上)检测到错误的编码(windows-1252),这会导致某些非英语字符错误显示。我希望他们强制使用 UTF-8。如果我获取文件并使用 StreamReader 将文本编码为 UTF8 并将它们提供给 DocumentText 属性,我会丢失所有图像和格式。

    public frmPreview(string filename, string Chapter)
    {
        InitializeComponent();
        string Path = Variables.TempFolder + "\\" + Utils.GetFilePathInsideZip(filename);
        webBrowser1.Navigate(Path);
        webBrowser1.Document.Encoding = "utf-8";


        this.Text = Chapter;
    }

1 - 无论计算机设置如何,如何强制页面编码?

请考虑图像和 CSS 很重要,我看不到一种方法来同时拥有图像和正确的编码,因为一旦加载文本(使用导航),就无法更改。但如果我不使用 Navigate,它就无法看到或查找 CSS 和图像。

此外,当导航到 XHTML 文件时,它们会尝试在应用程序外部(在默认 Windows 浏览器内)打开。起初,当我使用我唯一的 Streams 时,查看文件没有任何问题。

2 - 如何强制在控件内打开 XHTML 文件?

I am really starting to hate this control, it seems that some small task are almost impossible to do. I have looked into alternative but did not find anything useful.

I am working on a small application to edit eBooks. I am using the WebBrowser Control to preview the html files.

At First I tried really hard to have no Temp files, so I did it all in Memory with Streams, Modifying the html to include the images Encoded in Base64 and including the CSS directly in the Html. The problem with this is that large image are not displayed correctly, and it forces the user to have IE8 at a minimum.

I looked for other libraries, like a Chrome or Firefox Engine, but both are too big to be of use (11 Meg only for a preview when my app is 500k small).

I then resigned myself to just use temp files, so I would have better compatibility and less problems, with CSS and Images.

The Problem I have is that some file (on some Computer) have the wrong encoding detected (windows-1252), which causes some non-English character to be wrongly displayed. I want them to be forced to UTF-8. If I take the file and encode the text with StreamReader to UTF8 and feed them to DocumentText Property, I loose all the images and formating.

    public frmPreview(string filename, string Chapter)
    {
        InitializeComponent();
        string Path = Variables.TempFolder + "\\" + Utils.GetFilePathInsideZip(filename);
        webBrowser1.Navigate(Path);
        webBrowser1.Document.Encoding = "utf-8";


        this.Text = Chapter;
    }

1 - How can I force the encoding of the page, Regardless of Computer settings?

Please consider that the Images and CSS are important and I can't see a way To have both my images and the right encoding, because once the text is loaded (with Navigate), it cannot be changed. But if I don't use Navigate it cannot see or Find the CSS and Images.

Also when Navigating to XHTML Files, they are trying to be opened outside of the App (inside the default Windows Browser). At First when using my only Streams I did not have any problems viewing the files.

2 - How can I force the XHTML files to be opened inside the control?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文