Click here to Skip to main content
15,906,081 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
VB
Imports System.Data.OleDb
Imports System.IO
Imports System.Drawing.Imaging

Public Class Retrieve

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim conn = New OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;" & _
                  "Data Source=" & Application.StartupPath & "\DBASE.accdb;")
        conn.Open()
        Dim Str As String
        Str = "SELECT Signature FROM DB WHERE Username = '" & TextBox1.Text & "'"
        Dim cmmd = New OleDbCommand(Str, conn)
        Dim dr As OleDbDataReader
        dr = cmmd.ExecuteReader()


        If dr.HasRows Then                    'REST OF THE PROGRAM
            Dim RetVal As Long
            Dim FieldLen As Int32

            Dim AccessConnection As New OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;" & _
            "Data Source=" & Application.StartupPath & "\AMADS.accdb")
            Dim AccessCommand As New OleDbCommand("SELECT Signature FROM DB WHERE [UserName] = '" & TextBox1.Text & "'", AccessConnection)
            AccessConnection.Open()
            Dim AccessDataReader As OleDbDataReader = AccessCommand.ExecuteReader(CommandBehavior.SequentialAccess)
            AccessDataReader.Read()
            FieldLen = AccessDataReader.Item("Signature").Length

            Dim BinaryByteArray(FieldLen - 1) As Byte
            Dim StartIndex As Integer = 0
            RetVal = AccessDataReader.GetBytes(1, StartIndex, BinaryByteArray, 0, BinaryByteArray.Length)
            Dim OLEStream As New System.IO.MemoryStream(BinaryByteArray)
            PictureBox1.Image = Image.FromStream(OLEStream)
            AccessDataReader.Close()
            AccessConnection.Close()
        Else
            MsgBox("...")
        End If
    End Sub
End Class


Hi guys, i am trying to retrieve picture from MS access.
but i am getting an error on this part:
PictureBox1.Image = Image.FromStream(OLEStream) --> parameter not valid.

i've tried debugging this for ages.. im getting the same error again and again :p
Help me please... thanks..
Posted
Updated 3-Mar-12 19:48pm
v2

1 solution

You need to look at two things: the data that you have in the DB and how you stored it in there, and the data that you have read out.
The code to convert the bytes to an image looks good, so it has to be a problem with the data.
Try it with a small image (make one specially, say 8 by 8 pixels) and see what you get.


Hi, ive tried to do what you said, here it goes:

VB
Dim bytImage() As Byte

Try
    Dim ms As New System.IO.MemoryStream
    Dim bmpImage As New Bitmap(PictureBox1.Image)

    bmpImage.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg)
    bytImage = ms.ToArray()
    ms.Close()
Catch ex As Exception
    MsgBox(ex.ToString)
End Try


then to save it to access:
SQL
SQL = "INSERT INTO DB VALUES ('" & UserName & "', 'bytImage' )"


my code for retrieving:
the same as above.

im still getting the same error.


You are on the right lines (although you don't need to dim it as a new bitmap - you can use the existing one).
VB
Dim bmpImage As New Bitmap(PictureBox1.Image)

bmpImage.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg)
Becomes
VB
PictureBox1.Image.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg)



All you need to do is change the way you save it to the DB (You can't just pass the bytes as part of a text string - the DB will try to interpret them as a command and fail)

VB
Using con As New SqlConnection(strConnect)
	con.Open()
	Using com As New SqlCommand("INSERT INTO myTable (nameOfImageColumnInDB) VALUES (@IMAGE)", con)
		com.Parameters.AddWithValue("@IMAGE", bytImage)
		com.ExecuteNonQuery()
	End Using
End Using
You may need to use OleDbConnection and OleDbCommand instead of SqlConnection and SqlCommand
 
Share this answer
 
v2
Comments
kimsnap 2-Mar-12 10:00am    
Hi, thank you forr your reply.

the image is from ink, converted to base 64 String, converted to byte array then finally to image. then saved into access into a column with OLEObject type.. Is there a problem with the conversion?

i havent been able to see if im saving the image right because it is outputted in access as "long binary data" and i cant retrieve them.
OriginalGriff 2-Mar-12 10:15am    
Probably, yes.
If you have it as an Image (i.e. a object that you can display in a PictureBox with no further modifications) then how did you save that to Access? You should have had to convert it to a byte array first...
kimsnap 2-Mar-12 10:29am    
uhm what do you mean? i converted it to byte array. It is outputted in a textbox, then to this:
PictureBox1.Image = FromInk.Base64toImage(TextBox1.Text)
Then that is the saved value to database.

the ink converted to "base 64 string" to "byte array".
im lost at what you said. Sorry, can you tell me what's wrong with my procedure?
OriginalGriff 2-Mar-12 10:41am    
If you are saving the TextBox1.Text directly to the DB, then you have to perform exactly the same steps to generate the image when you take it out again.
If you are saving the PictureBox.Image directly to the DB, then the chances are that what you have actually saved is the text "System.Drawing.Image" since that is the default ToString response.

When I save an image to a DB, I convert it to a byte array:
Dim ms As New MemoryStream()
imageIn.Save(ms, System.Drawing.Imaging.ImageFormat.Bmp)
Return ms.ToArray()
and then save that.
I would then use code very like yours to read it back (except I would just cast the DB reader column value to a byte array)
kimsnap 2-Mar-12 10:53am    
okay.. so ill cut off this part:
--> PictureBox1.Image = FromInk.Base64toImage(TextBox1.Text)

or ill append
Dim ms As New MemoryStream()
imageIn.Save(ms, System.Drawing.Imaging.ImageFormat.Bmp)
Return ms.ToArray()
after this:
PictureBox1.Image = FromInk.Base64toImage(TextBox1.Text) ??

and what is imageIn? will i declare that as byte?

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