Search code examples
wpfvb.netimagebytememorystream

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


I inserted an image into a .mdb database with this code from my WPF app :

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)

The Pic column or cell's data type is OLE Object...Anyway,after inserting the data,i opened my database,i saw that a new record was added but the value of the Pic column was Long binary data.Anyway,then i went on retrieving the image in my wpf app.I used this code ?

 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

But it's returning an error : No imaging component suitable to complete the operation was found


Solution

  • Fixed it...For anyone who faces this error in future :

    1. Make sure you're inserting the data in the proper way(sometimes corrupted data in the db causes such errors)

    2 . You don't need to do some heavy coding to convert the image to byte!

    Finally,let's code :

       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