2013-01-20 133 views
5

我使用codeplex的ImageTools来保存画布为PNG;然而,当我使用writeableBitmap.SaveJpeg()时,我遇到了同样的问题。因此,问题不在于图像类型,而在于我如何在IsolatedStorage中进行保存或加载。将图像保存并加载到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; 
     } 
    } 
} 
+1

刚刚尝试了你的代码,似乎工作正常,都在模拟器上(诺基亚Lumia 920)... –

+0

奇数。我已经尝试过仿真器和设备(L920,针对Windows Phone 8.0的Visual Studio 2012),并且这两个错误都是持续存在的。不过,谢谢你让我知道它有效。 –

回答

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; 
     } 
} 

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

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