How to save stream of image data to the root of th

2019-09-02 11:43发布

问题:

I am trying to save the stream of image data to a file. I was able to save it to Pictures library though. But I want to save it to a file in the root of my application/ project. I was trying the below but it doesn't work.

         using (MediaLibrary mediaLibrary = new MediaLibrary())
         mediaLibrary.SavePicture(@"\DefaultScreen.jpg", stream);

回答1:

In this case you should use LocalStorage. Here is a simple solution to do this:

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

Here you can find all info about data in Windows Phone:

http://msdn.microsoft.com/en-us/library/windowsphone/develop/ff402541(v=vs.105).aspx