gpt4 book ai didi

.net - 如何使用值转换器将字节数组绑定(bind)到 WPF 中的图像?

转载 作者:行者123 更新时间:2023-12-02 03:48:30 28 4
gpt4 key购买 nike

我正在尝试将数据库中的字节数组绑定(bind)到 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:

"No imaging component suitable to complete this operation was found."

InnerException: "Exception from HRESULT: 0x88982F50"

我不明白我做错了什么。我怎样才能让它工作?

<小时/>

更新

问题似乎是来自数据库的字节。一定是我放入它们的方式有问题。

请参阅下面我的工作代码。

最佳答案

可以将字节[]绑定(bind)到图像。

这里是一个示例:

Xaml:

<Image Source="{Binding UserImage}"/>

代码:

private byte[] userImage;

public byte[] UserImage
{
get { return userImage; }
set
{
if (value != userImage)
{
userImage = value;
OnPropertyChanged("UserImage");
}
}
}

关于.net - 如何使用值转换器将字节数组绑定(bind)到 WPF 中的图像?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/686461/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com