2013-02-07 78 views

回答

0

要下載圖像,您需要獲取URI。 一旦你得到它,這是非常簡單的:

public class ServiceImageDownloader 
{ 
    private readonly BitmapImage _downloadedBmpImage = new BitmapImage(); 

    public ServiceImageDownloader() 
    { 
     _downloadedBmpImage.ImageOpened += DownloadedBmpImageImageOpened; 
     _downloadedBmpImage.ImageFailed += DownloadedBmpImageImageFailed; 
     _downloadedBmpImage.CreateOptions = BitmapCreateOptions.BackgroundCreation; 
    } 

    public void DownloadImage(string imageUri) 
    { 
     _downloadedBmpImage.UriSource = imageUri; 
    } 

    private void DownloadedBmpImageImageOpened(object sender, RoutedEventArgs e) 
    { 
     try 
     { 
      OnImageDownloaded(new WriteableBitmap(_downloadedBmpImage)); 
     } 
     catch 
     { 
      //manage other classic errors 
     } 
    } 

    private void OnImageDownloaded(WriteableBitmap wbImTemp) 
    { 
     //here you get your image as a writeableBmp and you can do whatever you wish, 
     //as save it in your IS in your example 
    } 
} 

現在的IS我會建議你使用您選擇的圖像作爲流,或作爲序列化對象。它可能是這樣的:

public bool WriteImage(string imageName, Stream streamImage) 
    { 
     IsolatedStorageFile store = null; 
     Stream stream = null; 

     try 
     { 

      using (store = IsolatedStorageFile.GetUserStoreForSite()) 
      {      
       // Open/Create the file for writing 
       stream = new IsolatedStorageFileStream(imageName, 
        FileMode.Create, FileAccess.Write, store); 

       streamImage.CopyTo(stream); 

       stream.Close(); 
       streamImage.Close(); 
      } 
      return true; 

     } 
     catch (Exception ie) 
     { 
      //manage error the way you prefer (think especially to quota management) 
     } 
    } 

希望它有幫助。

相關問題