我希望能够自己解决这个问题,但是经过几个小时的谷歌搜索,我有无数的例子,我无法开始工作。这是场景。希望有人可以提供一个简单的解决方案。
我有一个VB WPF应用程序。它与特定的Web服务通信以获取图像的Base64字符串。我可以使用以下代码将此字符串转换为 System.Drawing.Image 对象:
Public Function Base64ToImage(ByVal base64str As String) As System.Drawing.Image
'Setup image and get data stream together
Dim img As System.Drawing.Image
Dim MS As System.IO.MemoryStream = New System.IO.MemoryStream
Dim b64 As String = base64str.Replace(" ", "+")
Dim b() As Byte
'Converts the base64 encoded msg to image data
b = Convert.FromBase64String(b64)
MS = New System.IO.MemoryStream(b)
'creates image
img = System.Drawing.Image.FromStream(MS)
Return img
End Function
然后我打开一个弹出窗口,我想要做的就是在弹出窗口中显示这个图像。我发现的例子依赖于使用 PaintEventArgs ,但我不确定它是如何工作的,在这种情况下看起来并不相关。我能做的最好的事情是让图像显示在屏幕上,但它实际上并没有附加到弹出窗口。我使用以下代码完成了这个操作,这是弹出窗口类中的一个方法:
Dim img as System.Drawing.Image = Base64ToImage(base64string)
Dim gr As System.Drawing.Graphics = System.Drawing.Graphics.FromHwnd(New Interop.WindowInteropHelper(Me).Handle)
gr.DrawImage(img, 10, 10, 500, 800)
gr.Dispose()
显示图像,但它似乎出现在屏幕的10,10位,而不是弹出窗口。
答案 0 :(得分:0)
System.Drawing是一个WinForms-Namespace,因此它不适用于WPF。您可以使用此转换器将base64字符串转换为WPF可以使用的bitmapsource:
<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编码的格式返回图像。