2014-03-07 93 views
0

我試圖將從URL下載的BitmapImage保存到應用程序StorageFolder中。 我試着做一個功能,爲我節省圖像。這是我走到這一步:如何在C#中將BitmapImage保存到StorageFolder中Windows Phone 8

public async Task<string> savePhotoLocal(BitmapImage photo, string photoName) 
    { 
     var profilePictures = await storageRoot.CreateFolderAsync("profilePictures", CreationCollisionOption.OpenIfExists); 
     var profilePicture = await profilePictures.CreateFileAsync(photoName+".jpg", CreationCollisionOption.ReplaceExisting); 

     byte[] byteArray = new byte[0]; 
     using (MemoryStream stream = new MemoryStream()) 
     { 
      using (Stream outputStream = await profilePicture.OpenStreamForWriteAsync()) 
      { 
       await stream.CopyToAsync(outputStream); 
      } 
     } 

     return profilePicture.Path; 
    } 

但這不是工作,我沒有得到任何錯誤回來,所以我真的不知道怎麼回事錯在這裏。任何幫助或代碼示例都將是非常棒的。

回答

0
public async Task<string> savePhotoLocal(BitmapImage photo, string photoName) 
    { 
     string folderName ="profilePictures"; 
     var imageName =photoName+".jpg";   
     Stream outputStream = await profilePictures.OpenStreamForWriteAsync(); 
     if(outputStream!=null) 
      { 
      this.SaveImages(outputStream,folderName,imageName); 
      } 
     return imageName ; 
    } 





private void SaveImages(Stream data, string directoryName, string imageName) 
      { 
IsolatedStorageFile StoreForApplication =IsolatedStorageFile.GetUserStoreForApplication(); 
       try 
       { 
        using (MemoryStream memoryStream = new MemoryStream()) 
        { 
         data.CopyTo(memoryStream); 
         memoryStream.Position = 0; 
         byte[] buffer = null; 
         if (memoryStream != null && memoryStream.Length > 0) 
         { 
          BinaryReader binaryReader = new BinaryReader(memoryStream); 
          buffer = binaryReader.ReadBytes((int)memoryStream.Length); 
          Stream stream = new MemoryStream(); 
          stream.Write(buffer, 0, buffer.Length); 
          stream.Seek(0, SeekOrigin.Begin); 
          string FilePath = System.IO.Path.Combine(directoryName, imageName); 
          IsolatedStorageFileStream isoFileStream = new IsolatedStorageFileStream(FilePath, FileMode.Create, StoreForApplication); 
          Deployment.Current.Dispatcher.BeginInvoke(() => 
           { 
            BitmapImage bitmapImage = new BitmapImage { CreateOptions = BitmapCreateOptions.None }; 
            bitmapImage.SetSource(stream); 
            WriteableBitmap writeableBitmap = new WriteableBitmap(bitmapImage); 
            writeableBitmap.SaveJpeg(isoFileStream, writeableBitmap.PixelWidth, writeableBitmap.PixelHeight, 0, 100); 
           }); 
         } 
        } 

       } 
       catch (Exception ex) 
       { 
        //ExceptionHelper.WriteLog(ex); 
       } 
      } 
+0

感謝您的快速響應,但您試圖爲流打開的profilePicture是不可能的,因爲它是一個字符串。 – apero

+1

您能否解釋我StoreForApplication?我得到這個錯誤。它要求一個IsolatedStorageFile isf,但我不知道這是什麼 – apero

0

試試這個:

通過HttpWebRequest的下載圖像:

linkRequest = (HttpWebRequest)WebRequest.Create(uri); 
linkRequest.Method = "GET"; 
WebRequestState webRequestState = new WebRequestState(linkRequest, additionalDataObject); 
linkRequest.BeginGetResponse(client_DownloadImageCompleted, webRequestState); 

然後:

private void client_DownloadImageCompleted(IAsyncResult asynchronousResult) 
     { 
      Deployment.Current.Dispatcher.BeginInvoke(() => 
      { 
        WebRequestState webRequestState = asynchronousResult.AsyncState as WebRequestState; 

        AdditionalDataObject file = webRequestState._object; 

        using (HttpWebResponse response = (HttpWebResponse)webRequestState.Request.EndGetResponse(asynchronousResult)) 
        { 
         using (IsolatedStorageFile isoStore = IsolatedStorageFile.GetUserStoreForApplication()) 
         { 
            using (Stream stream = response.GetResponseStream()) 
            { 

             BitmapImage b = new BitmapImage(); 

             b.SetSource(stream); 
             WriteableBitmap wb = new WriteableBitmap(b); 
             using (var isoFileStream = isoStore.CreateFile(yourImageFolder + file.Name)) 
             { 
              var width = wb.PixelWidth; 
              var height = wb.PixelHeight; 
              System.Windows.Media.Imaging.Extensions.SaveJpeg(wb, isoFileStream, width, height, 0, 100); 
             } 
            } 
         } 
       } 
       }); 
     } 

而且WebRequestState類是:

public class WebRequestState 
    { 
     public HttpWebRequest Request { get; set; } 
     public object _object { get; set; } 



     public WebRequestState(HttpWebRequest webRequest, object obj) 
     { 
      Request = webRequest; 
      _object = obj; 
     } 
    } 
+0

感謝您的回覆,但我仍然沒有得到代碼工作。我是新來的。你能解釋我附加的數據對象是什麼,我應該把它放在那裏?我現在得到一個錯誤 – apero

+0

啊,是的。這只是我的舊代碼。您可以傳輸包含圖像名稱而不是additionalDataObject的字符串對象。然後你可以從webRequestState中獲取它。 (string filename =(string)webRequestState._object;) – Olter

相關問題