В ValueConverter
я пытался преобразовать System.Data.Linq.Binary
(образ SQL CE) в BitmapImage
. Этот метод работает (изображение отображается правильно в форме):
public object Convert(object value, Type targetType, object parameter,
CultureInfo culture) {
Binary binary = value as Binary;
if (binary != null) {
BitmapImage bitmap = new BitmapImage();
bitmap.BeginInit();
bitmap.StreamSource = new MemoryStream(binary.ToArray());
bitmap.EndInit();
return bitmap;
}
return null;
}
Этот метод работает НЕ (но, как ни странно, исключение не выдается):
public object Convert(object value, Type targetType, object parameter,
CultureInfo culture) {
Binary binary = value as Binary;
if (binary != null) {
using (var stream = new MemoryStream(binary.ToArray())) {
BitmapImage bitmap = new BitmapImage();
bitmap.BeginInit();
bitmap.StreamSource = stream;
bitmap.EndInit();
return bitmap;
}
}
return null;
}
Хорошая практика программирования гласит, что вы должны распоряжаться любыми создаваемыми вами потоками ... поэтому я запутался, почему второй метод не работает, а первый работает. Есть идеи?