2010-11-23 14 views
0

私はWindows Phone 7アプリケーション開発の新人です。 PhotoChooserTaskクラスを使用してピクチャライブラリにアクセスしています。画像ライブラリから画像を選択した後、その画像(.jpgファイル)を画像ライブラリからアプリケーションの画像フォルダに追加します。これを行う方法 ?私は次のコードを使用しています画像ライブラリから選択した画像をアプリケーションの画像フォルダに動的にコピーする方法はありますか?

public partial class MainPage : PhoneApplicationPage 
    { 

     PhotoChooserTask photoChooserTask; 
     // Constructor 
     public MainPage() 
     { 
      InitializeComponent(); 
      photoChooserTask = new PhotoChooserTask(); 
      photoChooserTask.Completed += new EventHandler<PhotoResult>(photoChooserTask_Completed); 
     } 

     private void button1_Click(object sender, RoutedEventArgs e) 
     { 
      photoChooserTask.Show();    
     } 

     void photoChooserTask_Completed(object sender, PhotoResult e) 
     { 
      if (e.TaskResult == TaskResult.OK) 
      { 
       BitmapImage bmp = new BitmapImage(); 
       bmp.SetSource(e.ChosenPhoto); 

      } 
     } 
    } 

私は選択した画像を自分のアプリケーションの画像フォルダに動的に追加したいと思います。これを行う方法?上記の問題を解決するためのコードやリンクを教えてください。

答えて

4

はここでIsolatedStorageに選択した画像を保存し、ページに表示し、それを読み出す例です:FYI

void photoChooserTask_Completed(object sender, PhotoResult e) 
{ 
    if (e.TaskResult == TaskResult.OK) 
    { 
     var contents = new byte[1024]; 

     using (var store = IsolatedStorageFile.GetUserStoreForApplication()) 
     { 
      using (var local = new IsolatedStorageFileStream("image.jpg", FileMode.Create, store)) 
      { 
       int bytes; 
       while ((bytes = e.ChosenPhoto.Read(contents, 0, contents.Length)) > 0) 
       { 
        local.Write(contents, 0, bytes); 
       } 
      } 

      // Read the saved image back out 
      var fileStream = store.OpenFile("image.jpg", FileMode.Open, FileAccess.Read); 
      var imageAsBitmap = PictureDecoder.DecodeJpeg(fileStream); 

      // Display the read image in a control on the page called 'MyImage' 
      MyImage.Source = imageAsBitmap; 
     } 
    } 
} 
0

あなたはストリームを取得したら、あなたはバイトに変換し、ローカルに保存することができます。ここでは、あなたのTask_Completedイベントハンドラを持っているべきものである。

using (MemoryStream stream = new MemoryStream()) 
{ 
    byte[] contents = new byte[1024]; 
    int bytes; 

    while ((bytes = e.ChosenPhoto.Read(contents, 0, contents.Length)) > 0) 
    { 
     stream.Write(contents, 0, bytes); 
    } 

    using (var local = new IsolatedStorageFileStream("myImage.jpg", FileMode.Create, IsolatedStorageFile.GetUserStoreForApplication())) 
    { 
     local.Write(stream.GetBuffer(), 0, stream.GetBuffer().Length); 
    } 
} 
+0

。これを行うには、別のMemoryStreamを作成する必要はありません。さらに、コードはGetUserStoreForApplication()によって返されたIsolatedStorageFileを破棄しません。 –

関連する問題