1

我想製作一個應用程序,它可以使用相機拍攝圖像並將其存儲在手機的獨立存儲器中。如何在每次運行中存儲7張圖像(即每次模擬器被激活),對於我捕獲和保存的八張圖像,我得到一個內存不足的異常。如果我必須在隔離存儲中存儲更多圖像,我必須停止調試並重新啓動調試。我是wp7開發新手。我是使用debugging.Please模擬器幫助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

您保存到「doc_list」,不只是URL,因此這款手機將有每個圖像,你在內存中捕獲位圖。您可能應該使用常規圖像控件和「isostore://」URL來尋找UI中引用圖像的解決方案。

編輯:

在下面我使用用於存儲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 2012-01-18 07:03:10

+0

類文檔有兩個成員,即名稱和照片,都是字符串。而且照片只有圖像文件的字符串路徑 – user1155478 2012-01-18 07:23:25

+0

我試圖包含一個例子abobe。它使答案相當長;對於那個很抱歉! – faester 2012-01-23 11:16:33