【发布时间】:2010-10-15 17:55:59
【问题描述】:
我正在尝试将 Byte 数组从我的数据库绑定到 WPF 图像。
我的 XAML:
<Window.Resources>
<local:BinaryImageConverter x:Key="imgConverter" />
</Window.Resources>
...
<Image Source="{Binding Path=ImageData, Converter={StaticResource imgConverter}}" />
我已经修改了Ryan Cromwell 发布的代码,用于值转换器:
Class BinaryImageConverter
Implements IValueConverter
Private Function Convert(ByVal value As Object, ByVal targetType As Type, ByVal parameter As Object, ByVal culture As System.Globalization.CultureInfo) As Object Implements IValueConverter.Convert
If value IsNot Nothing AndAlso TypeOf value Is Byte() Then
Dim bytes As Byte() = TryCast(value, Byte())
Dim stream As New MemoryStream(bytes)
Dim image As New BitmapImage()
image.BeginInit()
image.StreamSource = stream
image.EndInit()
Return image
End If
Return Nothing
End Function
Private Function ConvertBack(ByVal value As Object, ByVal targetType As Type, ByVal parameter As Object, ByVal culture As System.Globalization.CultureInfo) As Object Implements IValueConverter.ConvertBack
Throw New Exception("The method or operation is not implemented.")
End Function
End Class
BinaryImageConverter 的 Convert() 函数的 image.EndInit() 行抛出此 NotSupportedException:
"没有适合的成像组件 完成此操作已找到。”
InnerException: "异常来自 HRESULT: 0x88982F50"
我不明白我做错了什么。我怎样才能让它工作?
更新
问题似乎出在数据库中的字节数上。我放入它们的方式一定有问题。
在下面查看我的工作代码。
【问题讨论】:
标签: .net wpf data-binding xaml image