Click here to Skip to main content
15,891,607 members
Please Sign up or sign in to vote.
5.00/5 (2 votes)
See more:
i need help here a little bit
i have been able to save several pictures in my access database and i also want to navigate through my database
i can navigate with text but for pictures it's kinda hard coz i have never used pictures before
here is my code for navigation
VB
private Sub NavigateRecords()
    Dim da As New OleDb.OleDbDataAdapter("SELECT*FROM nax", con)
    Dim dt As New DataTable

    da.Fill(dt)
    If ds.Tables("tree").Rows.Count > 0 Then
        txtAgentNumber.Text = ds.Tables("tree").Rows(inc).Item(1)
        txtFirstName.Text = ds.Tables("tree").Rows(inc).Item(2)
        txtMiddleName.Text = ds.Tables("tree").Rows(inc).Item(3)
        txtSurName.Text = ds.Tables("tree").Rows(inc).Item(4)
        cboGender.Text = ds.Tables("tree").Rows(inc).Item(5)
        txtAddress.Text = ds.Tables("tree").Rows(inc).Item(6)
        txtPhone.Text = ds.Tables("tree").Rows(inc).Item(7)
        txtEmail.Text = ds.Tables("tree").Rows(inc).Item(8)
        txtNotes.Text = ds.Tables("tree").Rows(inc).Item(9)
        picPhoto.Image = ds.Tables("tree").Rows(inc).Item(10
        ds.AcceptChanges()
    End If
End Sub
the error keeps on appearing
error"Unable to cast object of type 'System.Byte[]' to type 'System.Drawing.Image" at
picPhoto.Image = ds.Tables("tree").Rows(inc).Item(10
i would graetly appreciate your help
thanks all in advance
Posted
Updated 28-Nov-13 1:53am
v3
Comments
Richard MacCutchan 28-Nov-13 7:29am    
You cannot use a cast, you need to recreate the image from the byte array. Just reverse the process that you used when saving the imge content. http://stackoverflow.com/questions/3440366/how-can-i-convert-system-byte-to-image-c-window-forms should help.
kef ngunjiri 28-Nov-13 8:44am    
iam new to vb can u please elaborate how is should add this statement
byte[] data = (byte[]) dt.Rows[0]["IMAGE"];
MemoryStream ms = new MemoryStream(data);
pictureBox1.Image = Image.FromStream(ms);
which i got from this link http://stackoverflow.com/questions/3440366/how-can-i-convert-system-byte-to-image-c-window-forms
i don't know how to put it in the code
Richard MacCutchan 28-Nov-13 8:52am    
Put it instead of the line that gives the error.
kef ngunjiri 28-Nov-13 9:26am    
iam trying to put the code, but a syntax error at
byte[] data = (byte[]) dt.Rows[0]["IMAGE"];
it says
'Byte' is a type that cannot be used as an expression
Richard MacCutchan 28-Nov-13 9:51am    
That is C# code; I don't think the VB.NET compiler will be able to understand it. You need to convert it to the equivalent VB.NET statements.

1 solution

You can't cast an array of bytes to an Image at all - they are not even close to the same size in most cases!

Instead, you need to convert the byte array to a Memory stream, then construct the Image from that:
VB
Dim bytes As [Byte]() = ds.Tables("tree").Rows(inc).Item(10)
Dim ms As New MemoryStream(bytes)
picPhoto.Image = Image.FromStream(ms)
 
Share this answer
 
Comments
kef ngunjiri 28-Nov-13 9:57am    
wow it's that simple thanks both of u
OriginalGriff 28-Nov-13 10:55am    
You're welcome!
Member 13514099 8-Dec-17 7:50am    
hi have prob solve it pls, how to change RDP control tell me

Dim bf As New BinaryFormatter
While client.Connected = True
ns = client.GetStream
PictureBox1.Image = bf.Deserialize(ns)


End While
OriginalGriff 8-Dec-17 8:17am    
THis is unrelated to the original query, and needs to be posted as a new question - preferably with rather more detail than "have prob solve it pls"...

https://www.codeproject.com/Questions/ask.aspx

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900