5

I am using ImageTools from codeplex in order to save a canvas as a png; however, I had the same problem when I was using a writeableBitmap.SaveJpeg(). Therefore, the problem is not with the image type, but rather with how I am saving or loading in IsolatedStorage.

When I save the image by pressing a save button the file exists, but when I load the image nothing appears. If I save the image twice then the image loads and is displayed correctly.

Below is my code.

Saving File:

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

Loading file

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

1 回答 1

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

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

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

于 2013-11-04T08:01:31.737 回答