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

PRN*_*ios 5 image isolatedstorage windows-phone-8

我正在使用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();
    }
}
Run Code Online (Sandbox Code Playgroud)

加载文件

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;
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

Raj*_*ati 0

尝试使用此代码从 检索图像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;
         }
}
Run Code Online (Sandbox Code Playgroud)

您可以将图像的来源设置为 filePath,访问它不会有任何问题。

如果这不起作用,则问题出在您保存图像时。您可能需要找到一种解决方法,将画布保存为 png 或 jpeg。