我如何引用中的WinForms WebBrowser控件生成的HTML本地资源?控件、资源、WinForms、WebBrowser

2023-09-03 03:00:08 作者:余安

我使用的WinForms WebBrowser控件来显示在Windows窗体应用程序的一些内容。我使用DocumentText属性写入生成的HTML。这部分工作壮观。现在我想在标记使用的一些图像。 (我也preFER使用,联CSS和JavaScript,但可以通过周围的只是嵌入它来工作。)

I'm using a winforms webbrowser control to display some content in a windows forms app. I'm using the DocumentText property to write the generated HTML. That part is working spectacularly. Now I want to use some images in the markup. (I also would prefer to use linked CSS and JavaScript, however, that can be worked around by just embedding it.)

我一直在谷歌上搜索了数天的过程中,似乎无法找到答案的标题问题。

I have been googling over the course of several days and can't seem to find an answer to the title question.

我试着使用相对引用:应用程序exe文件是在bin \调试。这些图像住在图像目录,在项目的根目录下。我已经设定被复制到编译输出目录中的图像,使他们最终在斌\调试\图像*。所以,我再使用这样的参考形象......以为它会是相对于exe文件。但是,当我看到在嵌入式浏览器窗口中的图片属性,我看到的图像的URL是有关:blankImages / *。一切都显得相对有关:空白当HTML写入控制。由于缺乏一个位置情况下,我想不出用什么相对文件资源引用。

I tried using a relative reference: the app exe is in the bin\debug. The images live in the "Images" directory at the root of the project. I've set the images to be copied to the output directory on compile, so they end up in bin\debug\Images*. So I then use a reference like this "Images..." thinking it will be relative to the exe. However, when I look at the image properties in the embedded browser window, I see the image URL to be "about:blankImages/*". Everything seems to be relative to "about:blank" when HTML is written to the control. Lacking a location context, I can't figure out what to use for a relative file resource reference.

我周围戳控件的属性,看看是否有一种方法来设置一些东西来解决这个问题。我创建了一个空白的HTML页面,并使用导航()方法,使用完整的本地路径所指向的文件浏览器吧。这工作正常与报告当地的file:/// ...浏览器路径空白页。然后,我又写了一封信给浏览器,这次使用的document.write()。此外,浏览器现在报告有关:空白作为URL

I poked around the properties of the control to see if there is a way to set something to fix this. I created a blank html page, and pointed the browser at it using the "Navigate()" method, using the full local path to the file. This worked fine with the browser reporting the local "file:///..." path to the blank page. Then I again wrote to the browser, this time using Document.Write(). Again, the browser now reports "about:blank" as the URL.

总之,就是有引用文件资源没有别的办法?

Short of writing the dynamic HTML results to a real file, is there no other way to reference a file resource?

我要尝试最后一件事:建设绝​​对文件路径,图像和写那些到HTML。正在使用XSL转换序列化对象的XML,所以我需要一些XSL的参数,这将需要一些额外的时间,因为我没那么熟悉和他们一起玩生成我的HTML。

I am going to try one last thing: constructing absolute file paths to the images and writing those to the HTML. My HTML is being generated using an XSL transform of a serialized object's XML so I'll need to play with some XSL parameters which will take a little extra time as I'm not that familiar with them.

推荐答案

下面是我们做什么,但我要指出,我们使用了一个自定义的Web浏览器来消除这种事情的能力,用鼠标右键单击,看到了美好的旧IE上下文菜单:

Here's what we do, although I should mention that we use a custom web browser to remove such things as the ability to right-click and see the good old IE context menu:

public class HtmlFormatter
{
    /// <summary>
    /// Indicator that this is a URI referencing the local
    /// file path.
    /// </summary>
    public static readonly string FILE_URL_PREFIX = 
        "file://";

    /// <summary>
    /// The path separator for HTML paths.
    /// </summary>
    public const string PATH_SEPARATOR = "/";
}


// We need to add the proper paths to each image source
// designation that match where they are being placed on disk.
String html = HtmlFormatter.ReplaceImagePath(
    myHtml, 
    HtmlFormatter.FILE_URL_PREFIX + ApplicationPath.FullAppPath + 
    HtmlFormatter.PATH_SEPARATOR);

基本上,你需要有一个有一个文件URI,如图像路径

Basically, you need to have an image path that has a file URI, e.g.

<img src="file://ApplicationPath/images/myImage.gif">