【问题标题】:Use local images in Webbrowser control在 Webbrowser 控件中使用本地图像
【发布时间】:2015-12-15 05:11:37
【问题描述】:

我在我的 Wp7 应用程序中使用 Webbrowser Control,但我似乎无法将 App 目录中的图像放入 webbrowser。

我已将一些图像放在与 .cs 和 .xaml 文件位于同一目录的文件夹中。现在我尝试将它们放在 webbrowser 控件中,但我似乎无法让它工作。

<img src="images/photo.jpg"/>
<img src="/images/photo.jpg"/>

上面两个显然不行,我猜应该是这样的:

<img src="/SilverlightApplication;component/photo.jpg"/>

“SilverlightApplication”和“component”应该换成别的东西,但我不知道是什么:(

【问题讨论】:

    标签: c# windows-phone-7 xaml webbrowser-control


    【解决方案1】:

    您需要将图像存储在独立存储中,然后从那里显示图像。我整理了一个示例,您可以从以下位置下载:-

    www.smartmobiledevice.co.uk/projects/webbrowserimagesample.zip

    这是基于 MSDN 文章 How to: Display Static Web Content Using the WebBrowser Control for Windows Phone

    【讨论】:

    • 非常感谢保罗!但是我在 Webbrowser 上做了一个 NavigateToString 方法,并在字符串中包含了整个 html 页面。出于某种原因,如果我这样做,图像将不起作用。 :S
    • 顺便说一句。只有孤立故事中的图像不适用于 NavigateToString。具有绝对 Uri 的图像确实有效。
    • 您还需要将字符串保存到独立存储中的文件中,并将 WebBrowser 控件上的 Base 属性设置为您保存文件的本地目录(html、图像、css、javascript)
    • 嗨@Paul 有任何方法可以使用NavigateToString 加载本地图像,因为我有多个图像需要根据用户选择动态加载图像
    【解决方案2】:

    在 Windows Phone 8 上,一些 WinRT 类可用,可以获取应用的隔离存储的文件系统路径。所以 IsoStorage 中文件的绝对 URL 将是:

    string URL = "file://" +
        Windows.Storage.ApplicationData.Current.LocalFolder.Path +
        "\\folder\\filename.png";
    

    WebBrowser 控件采用NavigateToString()'d HTML 中的此类 URL。或者,您可以将 IsoStorage 指定为基础并在整个过程中使用相对 URL。 isostore: URL 不起作用,我试过了。 ms-appx://local/ 也不行。

    为了完整起见,您可以非常相似地获取应用资源的文件系统路径。那是Windows.ApplicationModel.Package.Current.InstalledLocation.Path

    【讨论】:

    • 很好的答案。与NavigateToString 完美搭配。非常感谢:-)
    • @SevaAlekseyev 重要提示:上述技术不适用于与本地文件夹同级的存储中的其他目录,仅适用于本地文件夹及以下文件夹中的目录。例如,我使用 Path.GetTempPath() 方法为 Local 创建了一个名为 Temp 的同级目录。 WebBrowser 组件无法访问该目录中的文件。将它们复制到本地文件夹解决了问题。
    【解决方案3】:

    你可以通过连接上面给出的 Paul 示例应用程序动态更新数组来使用动态图像,

    string[] files = {
            "readme.htm", "desert.jpg", "sample.jpg"
        };
    

    在写隔离之前你可以删除现有的

           private void SaveFilesToIsoStore()
        {
            //These files must match what is included in the application package,
            //or BinaryStream.Dispose below will throw an exception.
            string[] files = {
            "readme.htm", "desert.jpg", "sample.jpg"
        };
    
            IsolatedStorageFile isoStore = IsolatedStorageFile.GetUserStoreForApplication();
            if(isoStore.FileExists(files[0]))
            {
                isoStore.DeleteFile(files[0]);
            }
    
                foreach (string f in files)
                {
                    StreamResourceInfo sr = Application.GetResourceStream(new Uri(f, UriKind.Relative));
                    using (BinaryReader br = new BinaryReader(sr.Stream))
                    {
                        byte[] data = br.ReadBytes((int)sr.Stream.Length);
                        SaveToIsoStore(f, data);
                    }
                }
            }
    
    
        private void SaveToIsoStore(string fileName, byte[] data)
        {
            string strBaseDir = string.Empty;
            string delimStr = "/";
            char[] delimiter = delimStr.ToCharArray();
            string[] dirsPath = fileName.Split(delimiter);
    
            //Get the IsoStore.
            IsolatedStorageFile isoStore = IsolatedStorageFile.GetUserStoreForApplication();
    
            //Re-create the directory structure.
            for (int i = 0; i < dirsPath.Length - 1; i++)
            {
                strBaseDir = System.IO.Path.Combine(strBaseDir, dirsPath[i]);
                isoStore.CreateDirectory(strBaseDir);
            }
    
            //Remove the existing file.
            if (isoStore.FileExists(fileName))
            {
                isoStore.DeleteFile(fileName);
            }
    
            //Write the file.
            using (BinaryWriter bw = new BinaryWriter(isoStore.CreateFile(fileName)))
            {
                bw.Write(data);
                bw.Close();
            }
        }
    

    【讨论】:

      【解决方案4】:

      这是一个非常古老的线程,但我想出了一个解决方案,因为我们今天刚刚更新了一个 WP7 应用程序。

      秘诀是先将图像转换为base 64表示,所以从这段代码开始:

          private string GetBase64(string f)
          {
              string ret = "";
      
              {
                  StreamResourceInfo sr = Application.GetResourceStream(new Uri(f, UriKind.Relative));
                  using (BinaryReader br = new BinaryReader(sr.Stream))
                  {
                      byte[] data = br.ReadBytes((int)sr.Stream.Length);
                      ret = System.Convert.ToBase64String(data);
                  }
              }
              return ret;
          }
      

      现在,你想在代码中注入图像(我的是 gif)使用这个

      StringBuilder sb = ... // a string builder holding your webpage
      String b64 = GetBase64("assets/images/filename.gif");
      
      sb.AppendFormat(@"<img src='data:image/gif;base64,{0}' /></div>", b64);
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-06-24
        • 1970-01-01
        • 2011-08-19
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多