如何将Image控件绑定到以VarBinary类型保存在数据库中的Image
我想在 XAML 中绑定以 varbinary 类型保存在数据库中的图像。我该怎么做?
例如northwind数据库中的图片字段。
谢谢
编辑 1:)
我编写此代码用于转换图像字段(Northwind 数据库中类别表中的图片字段),但每次我得到 Exception:
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.
I want to bind an image that save in varbinary type in database in XAML.How I can do that?
for example Picture field in northwind DataBase.
thanks
EDIT 1:)
I write this codes for convert Image field (Picture field in Categories table in Northwind DataBase) but every time I get Exception:
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.");
}
}
and the exception:
Provide value on 'System.Windows.Markup.StaticResourceHolder' threw an exception.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果您从数据库获取字节数组,则无需将其转换为图像或位图图像......
您可以将图像的 Source 属性绑定到字节数组。wpf 内部处理字节数组并将字节数组转换为图像...
编辑:
如果您仍然想将字节数组转换为位图图像,这里是 经过测试的方法
我已经使用上述方法创建了一个示例...
Xaml 代码:
代码隐藏
希望这会对您有所帮助...
If you are getting byte array from database then no need to convert that to image or bitmap image...
You can bind Source property of image to byte array.. wpf internally handle the byte array and it converts byte array to image...
Edit:
If you still want to convert byte array to Bitmap image here is the method which was tested
I have crated a sample using above method...
Xaml code:
Code Behind
Hope this will help you...
这只能使用自定义转换器来实现。有关如何实现“图像”部分的详细信息,请参阅此 此处了解有关创建转换器的详细信息。
This can be achieved only using a custom converter. Look at this for details on how to implement the "image" part and here for details about creating a converter.