Hello, Everyone,

I need your help with my code, I am trying to display the image from database to picturebox but I can't figure it out. Below is my code:

Private Sub LoadImage(ByVal ind As Integer)
        Dim cn As New SQLiteConnection("Data Source=C:\db\QC_ASD_2013_08_09.qdb")
        Dim cmd As New SQLiteCommand(String.Concat("SELECT beeld_data FROM image WHERE port_item_id=", ind), cn)
        Dim da As New SQLiteDataAdapter(cmd)
        Dim ds As New DataSet()
        da.Fill(ds, "image")
        Dim c As Integer = ds.Tables("image").Rows.Count
        If c > 0 Then
            Dim bytBLOBData() As Byte = _
                System.Text.Encoding.UTF8.GetBytes(ds.Tables("image").Rows(c - 1)("beeld_data"))
            Dim stmBLOBData As New MemoryStream(bytBLOBData)
            PictureBox1.Image = Image.FromStream(stmBLOBData)
        End If
End Sub

It always says parameter is invalid. Please see below:

4483729635c65466a478be8cfec53f18

Here's the SQlite DB that I am using. ---> http://www.sendspace.com/file/cw8rlm

Edited 3 Years Ago by renzlo

thanks for the link Begginnerdev, I hope other coders can help me.

Edited 3 Years Ago by renzlo

It shouldn't really matter, but the function is calling for a stream.

See if this works for you:

     Dim stmBLOBData As New MemoryStream(bytBLOBData)
     Using strm As Stream
         stmBLOBData.CopyTo(strm)
         PictureBox1.Image = Image.FromStream(strm)
     End Using

One that that does catch my eye is the:

System.Text.Encoding.UTF8.GetBytes

This is grasping for straws: but I am not sure an image should be encoded in UTF8 text format.

You can try something like a direct cast:

  PictureBox1.Image = Image.FromStream(New MemoryStream(DirectCast(ds.Tables("image").Rows(c - 1)("beeld_data"), Byte)))  

To see if that solves your problem as well.

I hope it helps!

This article has been dead for over six months. Start a new discussion instead.