0

i am trying to retrieve an image from a .mdb database and show it in an image control.My code is:

 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
            Dim recentbtn As New Rctsctt.UserControl1
            Dim data As Byte() = CType(table.Rows(0)(1), Byte())
            Dim strm As MemoryStream = New MemoryStream()
            strm.Write(data, 0, data.Length)
            strm.Position = 0
            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()
            recentbtn.Image.ImageSource = bi
    Next
    End if

I am getting a `parameter not valid exception in the line :

 Dim img As System.Drawing.Image = System.Drawing.Image.FromStream(strm)

Why is this happening ?

Update 1

I opened my .mdb database and saw that the image/picture was saved as a Long binary data'..So, i thought if i could convert the binary data to anImageSource`,it might work.So i tried this from here

        For Each row In table.Rows
            Dim recentbtn As New Rctsctt.UserControl1
            Dim picsource() As Byte = CType(row("Pic"), Byte())
            Dim bmp As BitmapImage = New BitmapImage
            Dim strm As MemoryStream = New MemoryStream
            Dim offset As Integer = 1
            strm.Write(picsource, offset, picsource.Length - offset)
            bmp.BeginInit()
            bmp.StreamSource = strm
            bmp.EndInit()
            recentbtn.Image.ImageSource = bmp
         Next

But now i get an error : No imaging component suitable to complete this operation was found.

  • That indicates that the `Byte` array you are getting from the database doesn't represent a valid `Bitmap` object. – jmcilhinney Feb 24 '18 at 05:27
  • Why are you creating two `MemoryStream` objects? Why write data to one `MemoryStream`, then read it back, then write it to another, then read it back? Just use one `MemoryStream`. Surely you shouldn't need to change formats as the data should be saved in the appropriate format in the first place. – jmcilhinney Feb 24 '18 at 05:30
  • my database has a .bmp image in it ....and as u said to use 1 `memorystream`, how should i do it ? –  Feb 24 '18 at 05:42
  • If `Image.FromStream` says the parameter is not valid then your `MemoryStream` doesn't contain valid data. It's that simple. How did you save it in the first place? – jmcilhinney Feb 24 '18 at 05:59
  • You know how to write the data from a `MemoryStream` and you know how to load a `BitmapImage` from one. Just get rid of the stuff in the middle. – jmcilhinney Feb 24 '18 at 06:00
  • my datatabase is a .mdb database, the 2nd field/cell is an `ole object` cell...I added the image from acces by right clicking on the field/cell > insert object>create new from file...That's how i added the image in the first place –  Feb 24 '18 at 06:03
  • The data that Access saves when you do that is not the same as if you save an `Image` object from .NET code. I'm not sure what the specific differences are, but I would expect that Access wraps the image data in some OLE data. If you want to be able to read the data as you are, you need to have saved it in basically the inverse manner. – jmcilhinney Feb 24 '18 at 06:12
  • so, if i save an image from code behind,hen i'll be able to load it ? –  Feb 24 '18 at 06:14
  • If you get a `Bitmap` from your `BitmapSource` and save that, or just start with a `Bitmap`, then yes, you will be able to load it that way. – jmcilhinney Feb 24 '18 at 06:27
  • i'll try and come back with the result...thnaks for de help –  Feb 24 '18 at 06:30
  • You should not use `System.Drawing.Image` in a WPF application, because it belongs to WinForms. See the duplicate question for the correct method to decode a bitmap frame. If for any reason your byte array does not contain an encoded BMP (or any other format), but a raw pixel buffer, you'd need this method: https://stackoverflow.com/questions/38477499/what-is-the-easiest-way-to-display-an-image-from-byte/38478982#38478982 – Clemens Feb 24 '18 at 08:13
  • @clemens, what should i do ? any sample codes? –  Feb 24 '18 at 12:49
  • @jmcilhinney,error is still there after adding image from code-behind –  Feb 24 '18 at 12:49
  • Then you did it wrong. – jmcilhinney Feb 24 '18 at 12:52
  • @Clemens, i was folowing [this link](http://www.redmondpie.com/how-to-save-and-retrieve-images-in-c-wpf-application-from-sql-server-database/) ...they could successfully retrieve the image but i can't ..why ? –  Feb 24 '18 at 12:59
  • what did i do wrong ? @jmcilhinney –  Feb 24 '18 at 13:16
  • i used [this](https://pastebin.com/mf0iJ4Pj) code to insert the image –  Feb 24 '18 at 13:18

0 Answers0