2011-09-13 9 views
0

IsolatedStorageにイメージを保存し、Silverlight(XAML)を表示する方法を見つける必要があります 重要:Silverlightはイメージ "自分"を取得する必要があり、イメージを設定できません のコードの前に私は以前に多くのソリューションを試しました。 非常に最後の解決策は、バイト配列に結合し、そして、私は、データベースにbyte[]を保存および取得しようとしたまでWindows Phone 7 SilverlightバインディングイメージをIsolatedStorageから取得

public byte[] ThumbLocal 
     { 
      get; 
      set; 
     } 


public class ByteImageConverter : IValueConverter 
    { 

      public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) 
     { 
      MemoryStream memStream = new MemoryStream((byte[])value); 
      memStream.Seek(0, SeekOrigin.Begin); 
      BitmapImage thumbLocal = new BitmapImage(); 
      thumbLocal.SetSource(memStream); 
      return thumbLocal; 
     } 
    } 

すべての仕事の後ろに画像 XAML

StackPanel Orientation="Horizontal" Margin="0,0,0,20"> 
           <Image Width="110" CacheMode="BitmapCache" Source="{Binding ThumbLocal,Converter={StaticResource imgConverter}}" 
             Margin="12,0,9,0"/> 
           <StackPanel Width="311"> 

コードに変換すること、です。 これで、イメージをファイルとしてIsolatedStorageに保存し、次に回収してbyte[]に変換する唯一のオプションが表示されます。 この「スマートな」ソリューションですか?

答えて

1

まず、このコンバータを作成:このコンバータを使用して

public class BinaryToImageSourceConverter : IValueConverter 
{ 

    public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) 
    { 
     if (value != null && value is byte[]) 
     { 
      var bytes = value as byte[]; 
      var stream = new MemoryStream(bytes); 
      var image = new BitmapImage(); 

      image.SetSource(stream); 
      stream.Close(); 
      return image; 
     } 
     return null; 
    } 

    public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) 
    { 
     throw new NotImplementedException(); 
    } 
} 

第二に、あなたのバイトにバインド[]、あなたはMVVMを使用している、すなわち場合: ビュー:

<Image Source="{Binding IsolatedStorageImage, Converter={StaticResource BinaryToImageSourceConverter}}" x:Name="ScanImage"/> 

をあなたは財産を作ることができますcontrlol(propipp snippet)型byte []を読み込み、イメージをisostorageからバイト配列に読み込み、propertyの値をそれに設定します。 ご不明な点がございましたら、お気軽にお問い合わせください。

関連する問題