2012-03-28 10 views
1

隔離されたストレージに保存されたイメージをバインドして、カメラと同じ画面に表示しようとしていますが、イメージを表示できないようです。私は電話のカメラロールに保存していないので、私はそれを知っていませんが、私は複数の写真を撮ってカメラのビューファインダーの下部にフィルムストリップのように表示するので、それらを保存していません。誰も私を助けることができますか?隔離されたストレージからのイメージ

私はここで、このチュートリアルHere

public partial class Page1 : PhoneApplicationPage 
{ 
private static ObservableCollection<PhotoImage> photoList = new ObservableCollection<PhotoImage>();//For the class and list 
private int savedCounter = 0; 



public Page1() 
{ 
     InitializeComponent(); 
} 


private void ShutterButton_Click(object sender, RoutedEventArgs e) 
    { 
     if (cam != null) 
     { 
      try 
      { 
       // Start image capture. 
       cam.CaptureImage(); 

      } 
      catch (Exception ex) 
      { 
       this.Dispatcher.BeginInvoke(delegate() 
       { 
        txtDebug.Text = ex.Message; 
       }); 
      } 
     } 
    } 

    void cam_CaptureCompleted(object sender, CameraOperationCompletedEventArgs e) 
    { 
     // Increments the savedCounter variable used for generating JPEG file names. 
     savedCounter++; 
    } 

    void cam_CaptureImageAvailable(object sender, Microsoft.Devices.ContentReadyEventArgs e) 
    { 
     string fileName = "MyImage" + savedCounter + ".jpg"; 

     try 
     { 

      // Save picture to the library camera roll. 
      //library.SavePictureToCameraRoll(fileName, e.ImageStream);//dont want to save it to the camera roll 


      // Set the position of the stream back to start 
      e.ImageStream.Seek(0, SeekOrigin.Begin); 

      // Save picture as JPEG to isolated storage. 
      using (IsolatedStorageFile isStore = IsolatedStorageFile.GetUserStoreForApplication()) 
      { 
       using (IsolatedStorageFileStream targetStream = isStore.OpenFile(fileName, FileMode.Create, FileAccess.Write)) 
       { 
        // Initialize the buffer for 4KB disk pages. 
        byte[] readBuffer = new byte[4096]; 
        int bytesRead = -1; 

        // Copy the image to isolated storage. 
        while ((bytesRead = e.ImageStream.Read(readBuffer, 0, readBuffer.Length)) > 0) 
        { 
         targetStream.Write(readBuffer, 0, bytesRead); 
        } 

       } 

      } 

      Deployment.Current.Dispatcher.BeginInvoke(delegate() 
      { 

       photoList.Add(new PhotoImage(fileName));//here is where I set with the file name 
       listBoxSearch.ItemsSource = photoList; //here is the binding 
      }); 


     } 
     finally 
     { 
      // Close image stream 
      e.ImageStream.Close(); 
     } 

    } 
    public class PhotoImage 
    { 

     public string PhotoItem { get; set; } 

     public PhotoImage(string pItem) 
     { 
      this.PhotoItem = pItem; 

     } 
    } 

を使用しています私のXAMLコードはオーケー

<!--LayoutRoot is the root grid where all page content is placed--> 
<Grid x:Name="LayoutRoot" Background="Transparent"> 
    <Grid.ColumnDefinitions> 
     <ColumnDefinition Width="640" /> 
     <ColumnDefinition Width="160" /> 
    </Grid.ColumnDefinitions> 

    <Canvas x:Name="viewfinderCanvas" Width="640" HorizontalAlignment="Left" Margin="0,0,0,143"> 

     <!--Camera viewfinder --> 
     <Canvas.Background> 
      <VideoBrush x:Name="viewfinderBrush" /> 
     </Canvas.Background> 
     <TextBlock Height="40" Name="txtDebug" Width="626" FontSize="24" FontWeight="ExtraBold" Canvas.Left="14" Canvas.Top="297" /> 
    </Canvas> 

    <!--Button StackPanel to the right of viewfinder>--> 
    <StackPanel Grid.Column="1" > 
     <Button x:Name="ShutterButton" Content="SH" Click="ShutterButton_Click" FontSize="26" FontWeight="ExtraBold" Height="75" /> 
    </StackPanel> 
    <Grid> 
     <ListBox Foreground="RoyalBlue" Height="131" Name="listBoxSearch" Width="438" TabIndex="10" Margin="96,343,106,6"> 
      <ListBox.ItemTemplate> 
       <DataTemplate> 
        <StackPanel Orientation="Horizontal" Height="Auto" > 
         <Image Height="73" Width="73" VerticalAlignment="Top" Margin="0,10,8,0" Source="{Binding PhotoItem }" /> 
        </StackPanel> 
       </DataTemplate> 
      </ListBox.ItemTemplate> 
     </ListBox> 

    </Grid> 

</Grid> 
+0

閉じるようにしてください! :)あなたはPhotoItemの定義を表示できますか(それはおそらくあなたのために壊れているものです) –

+0

こんにちはShahar、PhotoItemは、クラス –

+0

BlehのC#コードの一番下にあります。 PhotoItemは文字列です - 申し訳ありません。今すぐお答えください。 –

答えて

4

ある - 問題は、それがIsoStorage URIを取る方法を知っていると暗黙のコンバータを持っていないですそれをロードする。

一つの簡単な解決策は、あなたの光画像クラスに別のプロパティを追加し、代わりにそれと結合することである - ここでは、迅速かつ汚いです:これは偉大な解決策ではないことを

public ImageSource SourceItem 
{ 
    get 
    { 
    BitmapImage image = new BitmapImage(); 
    image.SetSource(isStore.OpenFile(PhotoItem, FileMode.Open)); 
    return image; 
    } 
} 

注 - 私はあなたを示しています一般的なアイデア。

  1. ストリームは破棄されません。 imageに設定するときは、Streamをaで囲みます。最後に
  2. あなたがやろうとしている内容に応じて、アプリの応答性にするためにimage.CreateOptionsを使用することもできます(ただし、あなたが開か維持することが必要とストリームを処理する方法を理解する必要がありますが)
  3. 、画像ロードされるものは完全な解像度になります。代わりにPictureDecoder.DecodeJpeg()を調べて、画像のサムネイルを読み込むことができます(またはカメラオブジェクトが提供するサムネイルをご覧ください)。
+0

助けてくれてありがとう、私のクラスを 'public class PhotoImageに変更することを意味する。 { public ImageSource PhotoItem {get;セット; } public PhotoImage(ImageSource pItem) { this.PhotoItem = pItem; } } ' ImageSourceをどこに追加しますか? –

+0

いいえ - そのままPhotoImageクラスを残してください - 私が書いたプロパティを追加してください(fileNameの代わりにPhotoItemを入れてください - 返信で変更します) –

+0

私はこのプロパティをどこに置くべきか見当たりません。 'public string PhotoItem {get;}のようなプロパティ。セット; } 'を持ち、' ​​public ImageSource SourceItem {get;セット; } 'と、上記の部分をどこに置くのですか? –

関連する問題