找不到适合完成操作的成像组件 WPF vb.net

No imaging component suitable to complete the operation was found WPF vb.net

我使用我的 WPF 应用程序中的代码将图像插入到 .mdb 数据库中:

Dim con As New OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" & GetCurrentDirectory() & "\Data\rctts.mdb;Jet OLEDB:Database Password=arn33423342;")
    con.Open()
    Dim cmd As New OleDbCommand("Insert into recents(Uname,Pic,Email)values(@uname,@pic,@email)", con)
    image1.Source = New BitmapImage(New Uri("D:\logo.png"))
    Dim buffer As Byte()
    Dim bitmap = TryCast(image1.Source, BitmapSource)
    Dim encoder = New PngBitmapEncoder()
    encoder.Frames.Add(BitmapFrame.Create(bitmap))
    Using stream = New MemoryStream()
    encoder.Save(stream)
    buffer = stream.ToArray()
    End Using
    cmd.Parameters.AddWithValue("@pic", buffer)

Pic 列或单元格的数据类型是 OLE 对象...无论如何,在插入数据后,我打开我的数据库,我看到添加了一条新记录但是 [=13 的值=] 列是 Long binary data。无论如何,然后我继续在我的 wpf app.I 中检索图像使用此代码?

 Dim cmd As New OleDbCommand("Select * from recents", con)
    Dim table As New DataTable
    Dim adap As New OleDbDataAdapter(cmd)
    adap.Fill(table)
    If table.Rows.Count <= 0 Then
    Else
   For Each row In table.Rows
   recentbtn.Image.ImageSource = BytesToImage(CType(row(1), Byte()))
            recentbtn.Names.Text = table.Rows(0)(0).ToString
            AddHandler recentbtn.MouseDown, AddressOf recentbtn_mousedow
            RecentsList.stp.Children.Add(recentbtn)
        Next
   End If
    loadingrecents.Visibility = Visibility.Hidden
  End Sub
  Private Shared Function BytesToImage(ByVal bytes As Byte()) As BitmapImage
    Dim bm = New BitmapImage()
    Using stream As MemoryStream = New MemoryStream(bytes)
        stream.Position = 0
        stream.Seek(0, SeekOrigin.Begin)
        bm.BeginInit()
        bm.StreamSource = stream
        bm.CreateOptions = BitmapCreateOptions.PreservePixelFormat
        bm.CacheOption = BitmapCacheOption.OnLoad
        bm.EndInit()
    End Using
    Return bm
End Function

但返回错误:找不到适合完成操作的成像组件

已修复...对于将来遇到此错误的任何人:

  1. 确保您以正确的方式插入数据(有时数据库中损坏的数据会导致此类错误)

2。您无需进行大量编码即可将图像转换为字节!

最后,让我们来编码:

   Public Sub read()
    con.Open()
    Dim cmd As New OleDbCommand("Select * from recents", con)
    Dim _dr As OleDbDataReader
    _dr = cmd.ExecuteReader
    Dim _photo As Byte()
    While _dr.Read()
            Try
            _photo = CType(_dr(1), Byte())
            Dim strm As MemoryStream = New MemoryStream(_photo)
                Dim img As System.Drawing.Image = System.Drawing.Image.FromStream(strm)
                Dim bi As BitmapImage = New BitmapImage()
                bi.BeginInit()
                Dim ms As MemoryStream = New MemoryStream()
                img.Save(ms, System.Drawing.Imaging.ImageFormat.Bmp)
                ms.Seek(0, SeekOrigin.Begin)
                bi.StreamSource = ms
                bi.EndInit()
            image1.Source = bi
        Catch ex As Exception
                MessageBox.Show(ex.Message)
            End Try
    End While