Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/css/36.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
.net 如何使用值转换器将字节数组绑定到WPF中的图像?_.net_Wpf_Data Binding_Xaml_Image - Fatal编程技术网

.net 如何使用值转换器将字节数组绑定到WPF中的图像?

.net 如何使用值转换器将字节数组绑定到WPF中的图像?,.net,wpf,data-binding,xaml,image,.net,Wpf,Data Binding,Xaml,Image,我正在尝试将数据库中的字节数组绑定到WPF映像 我的XAML: <Window.Resources> <local:BinaryImageConverter x:Key="imgConverter" /> </Window.Resources> ... <Image Source="{Binding Path=ImageData, Converter={StaticResource imgConverter}}" /> BinaryIma

我正在尝试将数据库中的字节数组绑定到WPF映像

我的XAML:

<Window.Resources>
    <local:BinaryImageConverter x:Key="imgConverter" />
</Window.Resources>
...
<Image Source="{Binding Path=ImageData, Converter={StaticResource imgConverter}}" />
BinaryImageConverter的Convert()函数的i
mage.EndInit()
行抛出此NotSupportedException

“没有适合的成像组件 找到“完成此操作。”

InnerException:“来自 HRESULT:0x88982F50“

我不明白我做错了什么。我怎样才能让它工作


更新 似乎问题出在数据库中的字节。我放它们的方式一定有问题


请参阅下面我的工作代码。

我认为这实际上是一个安全许可问题。尝试以管理员权限运行,看看是否有效,然后继续

编辑:我不同意否决票和评论。请查看此链接:


此用户有完全相同的错误,这是由安全设置引起的。因此,我坚持我的答案(这可能不是原因,但肯定值得一试)

我猜字节不是合法的图像格式。我认为错误代码对应于
WINCODEC\u ERR\u COMPONENTNOTFOUND
,这与无效字节一致

字节数组应该采用什么格式?你能把它保存到磁盘上,然后用另一个成像程序打开它吗?

试试这个

Dim imageSource as ImageSource
Dim bitmapDecoder = new PngBitmapDecoder(stream, BitmapCreateOptions.PreservePixelFormat, BitmapCacheOption.OnLoad);
imageSource = bitmapDecoder.Frames[0];
imageSource.Freeze();
Return imageSource

谢谢你的帮助。我现在让它工作了。我仍然不确定到底是什么问题

这就是我将图像放入数据库的方式

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}}" />

...

您可以将字节[]绑定到图像

以下是一个示例:

Xaml:


这会在“Dim bitmapDecoder=”行上引发相同的异常。如果将图像直接保存到文件流,它是否成功打开?此外,在更新中,您正在从文件流读取图像,然后将该图像对象的数据放入数据库。为什么不直接将文件OpenStream加载到数据库中呢?bendewey,我将ButtonUpload_Click()方法更改为“使用br作为新的二进制读取器(FileOpenStream)”,并使用该方法将图像重新添加到数据库中。我仍然收到相同的异常。直接从数据库保存到磁盘怎么样?这会创建一个24425字节的文件,Windows图片和传真查看器无法读取该文件。@Zack:那意味着我的回答是正确的,您的图像文件格式无效(要么是这样,要么是您获取/处理字节的方式不正确并导致其损坏)。我尝试了您的解决方案。但我遇到了一个错误,如
无法将System.Byte[]类型转换为System.Windows.Media.ImageSource
,可能是因为我正在为Windows phone开发。不确定2013年的情况,但在.NET 4.5绑定
Byte[]
直接发送到
图像
效果很好
<Window xmlns:local="clr-namespace:MyProjectName" ... >
    <Window.Resources>
        <local:BinaryImageConverter x:Key="imgConverter" />
    </Window.Resources>
...
<Image Source="{Binding Path=ImageData, Converter={StaticResource imgConverter}}" />
<Image Source="{Binding UserImage}"/>
private byte[] userImage;

public byte[] UserImage
   {
       get { return userImage; }
       set
       {
           if (value != userImage)
           {
               userImage = value;
               OnPropertyChanged("UserImage");
           }
       }
   }