System.Drawing 是一个 WinForms 命名空间,因此它不能很好地与 WPF 一起使用。您可以使用此转换器将 base64 字符串转换为 WPF 可以使用的位图源:
<ValueConversion(GetType(String), GetType(BitmapSource))> _
Public Class Base64ToImageConverter
Implements IValueConverter
Public Function Convert(ByVal value As Object, ByVal targetType As System.Type, ByVal parameter As Object, ByVal culture As System.Globalization.CultureInfo) As Object Implements System.Windows.Data.IValueConverter.Convert
Try
Return Base64ToImage(value)
Catch ex As Exception
If TypeOf parameter Is BitmapSource Then
Return parameter
End If
Return Binding.DoNothing
End Try
End Function
Public Function ConvertBack(ByVal value As Object, ByVal targetType As System.Type, ByVal parameter As Object, ByVal culture As System.Globalization.CultureInfo) As Object Implements System.Windows.Data.IValueConverter.ConvertBack
Try
Return ImageToBase64(value)
Catch ex As Exception
Return Binding.DoNothing
End Try
End Function
Public Shared Function Base64ToImage(ByVal imageString As String) As BitmapSource
Dim buffer() As Byte = System.Convert.FromBase64String(imageString)
Dim stream As New System.IO.MemoryStream(buffer)
Dim result As New BitmapImage()
With result
.BeginInit()
.StreamSource = stream
.EndInit()
End With
Return result
End Function
Public Shared Function ImageToBase64(ByVal image As BitmapSource) As String
Dim encoder As New PngBitmapEncoder
encoder.Frames.Add(BitmapFrame.Create(image))
Dim stream As New System.IO.MemoryStream
encoder.Save(stream)
stream.Seek(0, IO.SeekOrigin.Begin)
Dim buffer() As Byte = New System.IO.BinaryReader(stream).ReadBytes(stream.Length)
stream.Close()
Dim result As String = System.Convert.ToBase64String(buffer)
Return result
End Function
End Class
使用此转换器,您可以将 base64 字符串公开为对象的属性,并将图像控件的 Source 属性绑定到它。
编辑:这是如何使用转换器的示例:
<Window.Resources>
<s:String x:Key="TestImageString">iVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAMAAAAoLQ9TAAAABGdBTUEAAK/INwWK6QAAABl0RVh0U29mdHdhcmUAQWRvYmUgSW1hZ2VSZWFkeXHJZTwAAABjUExURXK45////6fT8PX6/bTZ8onE643F7Pf7/pDH7PP5/dns+b7e9MPh9Xq86NHo947G7Hm76NTp+PL4/bHY8ojD67rc85bK7b3e9MTh9dLo97vd8/D3/Hy96Xe76Nfr+H+/6f///1bvXooAAAAhdFJOU///////////////////////////////////////////AJ/B0CEAAACHSURBVHjaXI/ZFoMgEEMzLCqg1q37Yv//KxvAlh7zMuQeyAS8d8I2z8PT/AMDShWQfCYJHL0FmlcXSQTGi7NNLSMwR2BQaXE1IfAguPFx5UQmeqwEHSfviz7w0BIMyU86khBDZ8DLfWHOGPJahe66MKe/fIupXKst1VXxW/VgT/3utz99BBgA4P0So6hyl+QAAAAASUVORK5CYIII</s:String>
<t:Base64ToImageConverter x:Key="converter"/>
<t:ImageToBase64Converter x:Key="backConverter"/>
<BitmapImage x:Key="defaultImage" UriSource="/delete_24.png"/>
</Window.Resources>
<StackPanel>
<Image x:Name="Image" Source="{Binding Source={StaticResource TestImageString}, Converter={StaticResource converter}, ConverterParameter={StaticResource defaultImage}}" Stretch="None"/>
<TextBlock x:Name="ConvertedImage" Text="{Binding ElementName=Image, Path=Source, Converter={StaticResource backConverter}, ConverterParameter={StaticResource defaultImage}}"/>
<Image x:Name="CheckImage" Source="{Binding ElementName=ConvertedImage, Path=Text, Converter={StaticResource converter}, ConverterParameter={StaticResource defaultImage}}" Stretch="None"/>
</StackPanel>
您可以使用绑定对象上的任何属性来代替静态字符串资源,该属性以 WPF 可识别的格式返回图像并已进行 base64 编码。