【问题标题】:Loading local image from Isolated Storage to html page将本地图像从独立存储加载到 html 页面
【发布时间】:2013-07-05 08:10:53
【问题描述】:

在来自隔离存储的 html 页面中使用“图像路径”时,有些奇怪且与“图像路径”相关。

我想创建一个 html 页面,供应用程序的 webBrowser 对象使用。所以我在 IsolatedStorage 中创建了一个 html 页面,然后将此页面与 webBroswser.Navigate 一起使用。

除了图片,一切都很好。

1) 如果我在 IsolatedStorage 的根目录下创建一个 html 页面和图像,一切正常,代码 <img src="image.png"> 可以工作,我可以在页面页面上看到图像。

2) 但是,在我看来,在根目录下保存页面和图像的方式不是一个好主意,因为我已经有许多目录供应用程序使用,因此,我创建了一个新目录“Html”并保存那里的所有页面。

现在,当我打开此页面时,我看不到我的图像。我尝试了几种不同的 src 链接,但仍然找不到答案。


<img src=..."> 标记中的正确链接是什么,如果层次结构是:

IsolatedStorage-->Html(文件夹)-->index.html(文件)

(1) 隔离存储-->Html(文件夹)-->image.png(文件)

(2) 隔离存储-->Html(文件夹)-->图片(文件夹)-->image.png(文件)


实际上,我以为 (1) 会类似于 <img src="image.png">,但我尝试了几个类似的版本,但都没有奏效。

【问题讨论】:

标签: html image windows-phone-7 isolatedstorage


【解决方案1】:

嗯,这似乎有点奇怪:

此方法会将图片保存到IsolatedStorage,但不允许在html img标签中使用:

            using (IsolatedStorageFile isopicsFile = IsolatedStorageFile.GetUserStoreForApplication())
            {
                if (isopicsFile.FileExists(Constants.HtmlFolderName + "launch.png") == false)
                {
                    Stream yourFilepath = Application.GetResourceStream(new Uri("/someUri/launch.png", UriKind.Relative)).Stream;

                    BitmapImage b = new BitmapImage();
                    b.SetSource(yourFilepath);

                    WriteableBitmap wb = new WriteableBitmap(b);
                    using (var isoFileStream = isopicsFile.CreateFile(Constants.HtmlFolderName + "launch.png"))
                    {
                        var width = wb.PixelWidth;
                        var height = wb.PixelHeight;
                        // Theoretically, there may be the problem, as the file extention is png, not jpg
                        System.Windows.Media.Imaging.Extensions.SaveJpeg(wb, isoFileStream, width, height, 0, 100);
                    }
                }
            }

这个将保存图片并允许将其与 html 标签一起使用:

            string f = "somePath/launch.png";
            StreamResourceInfo sr = Application.GetResourceStream(new Uri(f, UriKind.Relative));
            using (BinaryReader br = new BinaryReader(sr.Stream))
            {
                byte[] data = br.ReadBytes((int)sr.Stream.Length);
                string fileName = "launch.png";
                string filePath = Constants.HtmlFolderName + fileName;

                using (IsolatedStorageFile isoStore = IsolatedStorageFile.GetUserStoreForApplication())
                {
                    if (isoStore.FileExists(filePath))
                    {
                        isoStore.DeleteFile(filePath);
                    }

                    using (BinaryWriter bw = new BinaryWriter(isoStore.CreateFile(filePath)))
                    {
                        bw.Write(data);
                        bw.Close();
                    }
                }
            }

另外,在第二种情况下,图片属性必须设置为 Content + Always Copy。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多