Dot-Net
如何使用值轉換器將字節數組綁定到 WPF 中的圖像?
我正在嘗試將我的數據庫中的字節數組綁定到 WPF 圖像。
我的 XAML:
<Window.Resources> <local:BinaryImageConverter x:Key="imgConverter" /> </Window.Resources> ... <Image Source="{Binding Path=ImageData, Converter={StaticResource imgConverter}}" />我已經修改了Ryan Cromwell為值轉換器發布的程式碼:
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 ClassBinaryImageConverter 的 Convert() 函式的 i
mage.EndInit()行拋出此NotSupportedException:“未找到適合完成此操作的成像組件。”
InnerException:“來自 HRESULT 的異常:0x88982F50”
我不明白我做錯了什麼。我怎樣才能得到這個工作?
更新
似乎問題出在數據庫中的字節數上。我放它們的方式一定有問題。
請參閱下面的我的工作程式碼。
感謝你的幫助。我現在已經開始工作了。我仍然不確定到底是什麼問題。
這就是我將圖像放入數據庫的方式……
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這是我用來將字節數組綁定到圖像的值轉換器……
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……
<Window xmlns:local="clr-namespace:MyProjectName" ... > <Window.Resources> <local:BinaryImageConverter x:Key="imgConverter" /> </Window.Resources> ... <Image Source="{Binding Path=ImageData, Converter={StaticResource imgConverter}}" />
您可以將 byte[] 綁定到圖像。
這是一個範例:
xml:
<Image Source="{Binding UserImage}"/>程式碼:
private byte[] userImage; public byte[] UserImage { get { return userImage; } set { if (value != userImage) { userImage = value; OnPropertyChanged("UserImage"); } } }