如何绑定一个Byte数组到图像中的WPF与值转换器? [英] How do I bind a Byte array to an Image in WPF with a value converter?
问题描述
我想从我的DATABSE一个字节数组绑定到一个WPF图像。
I'm trying to bind a Byte array from my databse to a WPF Image.
我的XAML:
<Window.Resources>
<local:BinaryImageConverter x:Key="imgConverter" />
</Window.Resources>
...
<Image Source="{Binding Path=ImageData, Converter={StaticResource imgConverter}}" />
通过<发布的
我修改code href="http://cromwellhaus.com/blogs/ryanc/archive/2007/07/26/binding-to-the-byte-of-an-image-in-wpf.aspx">Ryan克伦威尔的值转换器:
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
在我 mage.EndInit()
的BinaryImageConverter的转换线()函数抛出这个 NotSupportedException异常
The image.EndInit()
line of the BinaryImageConverter's Convert() function throws this NotSupportedException:
没有合适的成像组件 完成此操作被发现。
"No imaging component suitable to complete this operation was found."
的InnerException:从异常 HRESULT:0x88982F50
InnerException: "Exception from HRESULT: 0x88982F50"
我不明白我在做什么错。我怎样才能得到这个工作?
I don't understand what I'm doing wrong. How can I get this working?
看来问题是出来的数据库的字节数。一定有与我把他们的方式有问题。
It seems the problem was the bytes coming out of the database. There must have been a problem with the way I was putting them in.
请参阅我的工作code以下。
See my working code below.
推荐答案
感谢您的帮助。现在我已经得到了它的工作。我仍然不知道到底是什么问题了。
Thanks for all your help. I've now got it working. I'm still not sure exactly what the problem was.
这是我怎么把图片到我的数据库&hellip;
This is how I put images into my database…
Private Sub ButtonUpload_Click(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs)
Dim FileOpenStream As Stream = Nothing
Dim FileBox As New Microsoft.Win32.OpenFileDialog()
FileBox.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.MyPictures)
FileBox.Filter = "Pictures (*.jpg;*.jpeg;*.gif;*.png)|*.jpg;*.jpeg;*.gif;*.png|" & _
"All Files (*.*)|*.*"
FileBox.FilterIndex = 1
FileBox.Multiselect = False
Dim FileSelected As Nullable(Of Boolean) = FileBox.ShowDialog(Me)
If FileSelected IsNot Nothing AndAlso FileSelected.Value = True Then
Try
FileOpenStream = FileBox.OpenFile()
If (FileOpenStream IsNot Nothing) Then
Dim ByteArray As Byte()
Using br As New BinaryReader(FileOpenStream)
ByteArray = br.ReadBytes(FileOpenStream.Length)
End Using
Dim g As New ZackGraphic
g.Id = Guid.NewGuid
g.ImageData = ByteArray
g.FileSize = CInt(ByteArray.Length)
g.FileName = FileBox.FileName.Split("\").Last
g.FileExtension = "." + FileBox.FileName.Split(".").Last.ToLower
g.DateAdded = Now
Dim bmp As New BitmapImage
bmp.BeginInit()
bmp.StreamSource = New MemoryStream(ByteArray)
bmp.EndInit()
bmp.Freeze()
g.PixelWidth = bmp.PixelWidth
g.PixelHeight = bmp.PixelHeight
db.AddToZackGraphic(g)
db.SaveChanges()
End If
Catch Ex As Exception
MessageBox.Show("Cannot read file from disk. " & Ex.Message, "Add a New Image", MessageBoxButton.OK, MessageBoxImage.Error, MessageBoxResult.OK)
Finally
If (FileOpenStream IsNot Nothing) Then
FileOpenStream.Close()
End If
End Try
End If
End Sub
这是用来绑定一个Byte数组到图像&hellip我的价值转换器;
This is my value converter used to bind a Byte array to an Image…
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 ByteArray As Byte() = TryCast(value, Byte())
Dim bmp As New BitmapImage()
bmp.BeginInit()
bmp.StreamSource = New MemoryStream(ByteArray)
bmp.EndInit()
Return bmp
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
这是我的XAML使用转换器显示图像&hellip;
This is my XAML that uses the converter display the image…
<Window xmlns:local="clr-namespace:MyProjectName" ... >
<Window.Resources>
<local:BinaryImageConverter x:Key="imgConverter" />
</Window.Resources>
...
<Image Source="{Binding Path=ImageData, Converter={StaticResource imgConverter}}" />
这篇关于如何绑定一个Byte数组到图像中的WPF与值转换器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!