我想要在XAML中綁定保存爲數據庫中varbinary類型的圖像。如何才能做到這一點?如何將圖像控件綁定到以VarBinary類型保存在數據庫中的圖像
例如Northwind DataBase中的圖片字段。
感謝
編輯:) 1
我寫(在Northwind數據庫分類表圖片字段)這個代碼轉換圖片領域,但每一次我得到異常:
class ImageConverter : IValueConverter
{
object IValueConverter.Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
if (value == null) { return null; }
var image = (System.Drawing.Image)value;
var bitmap = new System.Windows.Media.Imaging.BitmapImage();
bitmap.BeginInit();
MemoryStream memoryStream = new MemoryStream();
image.Save(memoryStream, ImageFormat.Bmp);
memoryStream.Seek(0, System.IO.SeekOrigin.Begin);
bitmap.StreamSource = memoryStream;
bitmap.EndInit();
return bitmap;
}
object IValueConverter.ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
throw new Exception("The method or operation is not implemented.");
}
}
And:
class ImageConverter : IValueConverter
{
object IValueConverter.Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
if (value != null && value is byte[])
{
byte[] bytes = value as byte[];
MemoryStream stream = new MemoryStream(bytes);
BitmapImage image = new BitmapImage();
image.BeginInit();
image.StreamSource = stream;
image.EndInit();
return image;
}
return null;
}
object IValueConverter.ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
throw new Exception("The method or operation is not implemented.");
}
}
和例外:
Provide value on 'System.Windows.Markup.StaticResourceHolder' threw an exception.
如果我使用價值轉換器什麼類型我轉換Linq.Binary? – Arian
僅當一個(源或目標)屬性是依賴項屬性時,綁定才起作用。如果您有Linq.Binary類型的源屬性,則需要將其轉換爲目標類型。 – kubal5003