加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > Windows > 正文

保存和加载图像到IsolatedStorage需要保存两次

发布时间:2020-12-14 02:16:56 所属栏目:Windows 来源:网络整理
导读:我使用codeplex中的 ImageTools来将画布保存为png;但是,当我使用writeableBitmap.SaveJpeg()时,我遇到了同样的问题.因此,问题不在于图像类型,而在于我如何在IsolatedStorage中保存或加载. 当我通过按下保存按钮保存图像时文件存在,但是当我加载图像时,没有任
我使用codeplex中的 ImageTools来将画布保存为png;但是,当我使用writeableBitmap.SaveJpeg()时,我遇到了同样的问题.因此,问题不在于图像类型,而在于我如何在IsolatedStorage中保存或加载.

当我通过按下保存按钮保存图像时文件存在,但是当我加载图像时,没有任何内容出现.如果我将图像保存两次,则图像会加载并正确显示.

以下是我的代码.

保存文件:

ExtendedImage myImage = myCanvas.ToImage();

using (var isoStore = IsolatedStorageFile.GetUserStoreForApplication())
{
    if (isoStore.FileExists("image.png"))
       isoStore.DeleteFile("image.png");

    using (var fileStream = isoStore.CreateFile("image.png"))
    {
        myImage.WriteToStream(fileStream,"image.png");
        fileStream.Close();
    }
}

加载文件

BitmapImage bi = new BitmapImage();

using (var isoStore = IsolatedStorageFile.GetUserStoreForApplication())
{
    if (isoStore.FileExists("image.png"))
    {
        using (var fileStream = isoStore.OpenFile("image.png",FileMode.Open))
        {
            bi.SetSource(fileStream);
            this.img.Height = bi.PixelHeight;
            this.img.Width = bi.PixelWidth;
            this.img.Source = bi;
        }
    }
}

解决方法

尝试此代码从isoStore检索图像.这个对我有用.

using (IsolatedStorageFile iso = IsolatedStorageFile.GetUserStoreForApplication())
{
      if (iso.FileExists(string.Format("image.png")))
         {
            string fileName = "image.png";
            string filePath = iso.GetType().GetField("m_RootDir",System.Reflection.BindingFlags.NonPublic |
            System.Reflection.BindingFlags.Instance).GetValue(iso).ToString() + fileName;
         }
}

您可以将Image的源设置为filePath,访问它时不会有任何问题.

如果这不起作用,那么问题是在保存图像时.您可能必须找到将画布保存为png或jpeg的解决方法.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读