2013-05-17 30 views
0

我希望能自己弄清楚這一點,但經過幾個小時的谷歌搜索,我有無數的例子,我無法工作。這是場景。希望有人能提供一個簡單的解決方案。在VB中使用DrawImage的簡單例子WPF

我有一個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

WPF不關心'System.Drawing'。從項目中刪除所有對此的引用。 –

回答

0

System.Drawing是一個WinForms命名空間,因此它不適用於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字符串作爲一個對象的屬性,綁定源圖像控制屬性。

編輯:這裏是如何使用轉換器的例子:

<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編碼。

+0

謝謝你的這個轉換器。你能否提供一個我將如何使用它的例子? – BrianVPS