【问题标题】:HTML img tag does not render source image in WebKit.NET browser in C#HTML img 标签不会在 C# 中的 WebKit.NET 浏览器中呈现源图像
【发布时间】:2015-05-01 02:30:16
【问题描述】:

我正在尝试将我的 WebKitBrowser DocumentText 设置为包含本地 SVG 文件路径作为图像源的 HTML 字符串。 实际上我想在网络浏览器中显示 SVG 文件。 这是我的代码:

        string SVGPath = "file:///D:/MySVGFiles 1/SVGSample01.svg";

        StringWriter stringWriter = new StringWriter();

        using (HtmlTextWriter writer = new HtmlTextWriter(stringWriter))
        {
            writer.AddAttribute(HtmlTextWriterAttribute.Src, SVGPath);
            writer.AddAttribute(HtmlTextWriterAttribute.Width, "50%");
            writer.AddAttribute(HtmlTextWriterAttribute.Height, "50%");

            writer.RenderBeginTag(HtmlTextWriterTag.Img); 
            writer.RenderEndTag(); 

        }

        string content = stringWriter.ToString();

        this.webKitBrowser1.DocumentText = content;

当我运行代码时,浏览器只显示图像画布,不渲染 SVG 文件。我也用 JPG 图像尝试过,得到了相同的结果。

谁能告诉我这段代码有什么问题?

【问题讨论】:

    标签: c# html image webkit htmltextwriter


    【解决方案1】:

    我终于发现出了什么问题。 WebKitBrowser 的 DocumentText 属性是一个字符串,在其 set 方法中,将 HTML 文本传递给 loadHTMLString 方法。

    webView.mainFrame().loadHTMLString(value, null);
    

    在未指定 URL 时使用 DocumentText 属性。但是在这里我想从指定的地址加载图像。因此,如果使用诸如设置 DocumentText 属性之类的标签将无效。我必须调用 loadHTMLString,当要使用其地址将图像添加到 HTML 字符串时,URL 必须是图像文件的目录。 根据我在https://groups.google.com/forum/#!topic/uni_webview/idiRRNIRnCU找到的,我改了代码,问题就解决了! 这是有效的代码:

     string fileName = "SVGSample01.svg";
     string URL = "file:///D:/MySVGFiles 1/";
    
     StringWriter stringWriter = new StringWriter();
    
        using (HtmlTextWriter writer = new HtmlTextWriter(stringWriter))
        {
            writer.AddAttribute(HtmlTextWriterAttribute.Src, fileName);
            writer.AddAttribute(HtmlTextWriterAttribute.Width, "50%");
            writer.AddAttribute(HtmlTextWriterAttribute.Height, "50%");
    
            writer.RenderBeginTag(HtmlTextWriterTag.Img); 
            writer.RenderEndTag(); 
    
        }
    
        string content = stringWriter.ToString();
    
     (this.webKitBrowser1.GetWebView() as IWebView).mainFrame().loadHTMLString(content,URL);
    

    只需确保 URL 字符串包含“file:///”和最后一个“/”即可。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-10-02
      • 2019-02-09
      • 1970-01-01
      • 2018-02-28
      • 1970-01-01
      • 1970-01-01
      • 2011-03-01
      相关资源
      最近更新 更多