0

我希望自己解决这个问题,但经过数小时的谷歌搜索,我有无数的例子我无法开始工作。这是场景。希望有人可以提供一个简单的解决方案。

我有一个 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 位置,而不是弹出窗口的位置。

4

1 回答 1

0

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 编码。

于 2013-05-17T17:31:24.240 回答