1

カメラを使用して画像をキャプチャし、電話機の隔離されたストレージに保存するアプリケーションを作成したいと思います。これまでのところ、それぞれの実行で7つの画像を保存できますエミュレータが起動されています)、8枚の画像をキャプチャして保存するとメモリ不足になります。隔離されたストレージにさらに画像を保存する必要がある場合は、デバッグをやめ、デバッグを再開する必要があります。 .Iはdebugging.Please用エミュレータを使用していますあなただけのURL、「doc_list」にビットマップを保存していないされているので、携帯電話は、あなたがメモリに取り込む各イメージを持っていますwp7のメモリ不足の例外

Stream doc_photo; 
    List<document> doc_list = new List<document>(); 
    document newDoc = new document(); 
    public Page2() 
    { 
     InitializeComponent(); 
    } 


    private void captureDocumentImage(object sender, RoutedEventArgs e) 
    { 
     ShowCameraCaptureTask(); 
    } 

    private void ShowCameraCaptureTask() 
    { 
     CameraCaptureTask photoCameraCapture = new CameraCaptureTask(); 
     photoCameraCapture = new CameraCaptureTask(); 
     photoCameraCapture.Completed += new EventHandler<PhotoResult>photoCameraCapture_Completed); 
     photoCameraCapture.Show(); 
    } 

    void photoCameraCapture_Completed(object sender, PhotoResult e) 
    { 
     if (e.TaskResult == TaskResult.OK) 
     { 
      capturedImage.Source = PictureDecoder.DecodeJpeg(e.ChosenPhoto); 
      doc_photo = e.ChosenPhoto; 

     } 
    } 

    private void SaveToIsolatedStorage(Stream imageStream, string fileName) 
    { 
     using (IsolatedStorageFile myIsolatedStorage = IsolatedStorageFile.GetUserStoreForApplication()) 
     { 
      if (myIsolatedStorage.FileExists(fileName)) 
      { 
       myIsolatedStorage.DeleteFile(fileName); 
      } 

      IsolatedStorageFileStream fileStream = myIsolatedStorage.CreateFile(fileName); 
      BitmapImage bitmap = new BitmapImage(); 
      bitmap.SetSource(imageStream); 
      try 
      { 
       WriteableBitmap wb = new WriteableBitmap(bitmap); 
       wb.SaveJpeg(fileStream, wb.PixelWidth, wb.PixelHeight, 0, 85); 
       fileStream.Close(); 
      } 
      catch (OutOfMemoryException e1) 
      { 
       MessageBox.Show("memory exceeded"); 
      } 

     } 
    } 

    private void save_buttonclicked(object sender, RoutedEventArgs e) 
    { 

     if (namebox.Text != "" && doc_photo!=null) 
     { 

      newDoc.doc_name = namebox.Text; 

      IsolatedStorageFile myIsolatedStorage = IsolatedStorageFile.GetUserStoreForApplication(); 
      if(!myIsolatedStorage.DirectoryExists(App.current_panorama_page)) 
      { 
       myIsolatedStorage.CreateDirectory(App.current_panorama_page); 
      } 
      newDoc.photo = App.current_panorama_page + "/" + namebox.Text + ".jpg";// 
      SaveToIsolatedStorage(doc_photo, newDoc.photo); 

      doc_list.Add(newDoc); 
      NavigationService.Navigate(new Uri("/MainPage.xaml", UriKind.Relative)); 

     } 
     else 
     { 
      if (namebox.Text == "") 
      { 
       MessageBox.Show("Enter the name"); 
      } 
      else if (doc_photo == null) 
      { 
       MessageBox.Show("Capture an Image"); 
      } 

     } 
    } 

答えて

3

を助けます。おそらく、通常の画像コントロールと 'isostore://' URLを使用してUIで画像が参照されるような解決策を検討する必要があります。

EDIT:私はIsoImageWrappersを格納するためのObservableCollectionを使用以下の例で

。後者のクラスは、コンストラクタで指定されたURIを持つ独立したファイルストリームをインスタンス化することによって、分離されたストレージへの接続を処理します。

ObservableCollectionは、新しい画像が追加されたときにWP7フレームワークに通知します。イメージの保存はオリジナルの提案とほぼ同じです。結合

リストボックスには、次のとおりです。

<ListBox Grid.Row="0" Height="495" Margin="0" Name="listBox1" Width="460" > 
    <ListBox.ItemTemplate> 
     <DataTemplate> 
      <Image Source="{Binding Source}" Width="Auto" /> 
     </DataTemplate> 
    </ListBox.ItemTemplate> 
</ListBox> 

とヘルパークラスなどの醜いインラインでコード:

using System; 
using System.Collections.ObjectModel; 
using System.IO; 
using System.IO.IsolatedStorage; 
using System.Windows; 
using System.Windows.Media; 
using System.Windows.Media.Imaging; 
using Microsoft.Phone.Controls; 
using Microsoft.Phone.Tasks; 

namespace WP7Photo 
{ 
    public partial class MainPage : PhoneApplicationPage 
    { 
     public class IsoImageWrapper 
     { 
      public string Uri { get; set; } 

      public ImageSource Source 
      { 
       get 
       { 
        IsolatedStorageFile isostore = IsolatedStorageFile.GetUserStoreForApplication(); 
        var bmi = new BitmapImage(); 
        bmi.SetSource(isostore.OpenFile(Uri, FileMode.Open, FileAccess.Read)); 
        return bmi; 
       } 
      } 
     } 

     public ObservableCollection<IsoImageWrapper> Images { get; set; } 

     // Constructor 
     public MainPage() 
     { 
      InitializeComponent(); 
      Images = new ObservableCollection<IsoImageWrapper>(); 
      listBox1.ItemsSource = Images; 
     } 

     private void Button1Click(object sender, RoutedEventArgs e) 
     { 
      var cameraTask = new CameraCaptureTask(); 
      cameraTask.Completed += new EventHandler<PhotoResult>(cameraTask_Completed); 
      cameraTask.Show(); 
     } 

     void cameraTask_Completed(object sender, PhotoResult e) 
     { 
      if (e.TaskResult != TaskResult.OK) 
      { 
       return; 
      } 

      StorePhoto(e); 
     } 

     private void StorePhoto(PhotoResult photo) 
     { 
      IsolatedStorageFile isostore = IsolatedStorageFile.GetUserStoreForApplication(); 
      if (!isostore.DirectoryExists("photos")) 
      { 
       isostore.CreateDirectory("photos"); 
      } 

      var filename = "photos/" + System.IO.Path.GetFileName(photo.OriginalFileName); 

      if (isostore.FileExists(filename)) { isostore.DeleteFile(filename);} 

      using (var isoStream = isostore.CreateFile(filename)) 
      { 
       photo.ChosenPhoto.CopyTo(isoStream); 
      } 
      Images.Add(new IsoImageWrapper {Uri = filename}); 
     } 
    } 
} 
+0

あなたは私の上記のコードを変更してくださいすることができますか? – user1155478

+0

クラス文書には名前と写真の2つのメンバーがあり、どちらもstringです。写真には画像ファイルの文字列パスのみがあります – user1155478

+0

私はabobeの例を含めるようにしました。答えはかなり長くなります。それについて申し訳ありません! – faester