In Windows Phone 7, how can I save BitmapImage for local storage?

In Windows Phone 7, how can I save BitmapImage to local storage? I need to save the image for caching and reloading if it is requested again in the next few days.

+3
source share
2 answers

If you save the file in IsolStorage, you can set the relative path for viewing it.

Here is a quick example of saving a file that was included in XAP (as a resource) in isolated storage.

using (IsolatedStorageFile isoStore = IsolatedStorageFile.GetUserStoreForApplication())
{
  if (!isoStore.FileExists(fileName)
  {
    var sr = Application.GetResourceStream(new Uri(fileName, UriKind.Relative));

    using (var br = new BinaryReader(sr.Stream))
    {
      byte[] data = br.ReadBytes((int)sr.Stream.Length);
      string strBaseDir = string.Empty;
      const string DelimStr = "/";
      char[] delimiter = DelimStr.ToCharArray();
      string[] dirsPath = fileName.Split(delimiter);

      // Recreate the directory structure
      for (int i = 0; i < dirsPath.Length - 1; i++)
      {
          strBaseDir = Path.Combine(strBaseDir, dirsPath[i]);
          isoStore.CreateDirectory(strBaseDir);
      }

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

, Ben Gracewood . .

+5

, , - , xap . .

        using (var isoStore = IsolatedStorageFile.GetUserStoreForApplication()) {

            var bi = new BitmapImage();
            bi.SetSource(picStreamFromXap);
            var wb = new WriteableBitmap(bi);

            using (var isoFileStream = isoStore.CreateFile("pic.jpg")) 
                Extensions.SaveJpeg(wb, isoFileStream, wb.PixelWidth, wb.PixelHeight, 0, 100);

        }
0

Source: https://habr.com/ru/post/1762984/


All Articles