Storing & Retrieving Images in SQL Mobile

H

Heather B.

I am trying to take a picture from a PDA and then store that picture in SQL
Mobile which I have done, but when I try to retrieve the picture I get an
out of memory error. I'm not sure I'm storing it correctly though, do I
need to do some sort of compression on it first? I'm using VS 2005 and
merge replication. Thanks, below is my code:

'TO STORE THE IMAGE

Dim fs As New FileStream(strDirectory & "\" & strFileName, FileMode.Open,
FileAccess.Read)
Dim img As Byte() = New Byte(fs.Length) {}
Dim prm As New SqlCeParameter
prm.SqlDbType = SqlDbType.Image
prm.Size = img.Length
prm.Value = img

Me._cmd.CommandText = "Update Employee Set EmployeePic=? Where
EmployeeId=1194"
Me._cmd.Parameters.Clear()
Me._cmd.Parameters.Add(prm)
Me._cmd.ExecuteNonQuery()

fs = Nothing
img = Nothing
prm = Nothing



'TO RETRIEVE THE IMAGE, CODE FAILS ON
DIM BMP WITH OUT OF MEMORY

Dim img As Image
Dim sqlce As SqlCeDataReader
sqlce = globals.gDB.GetEmployeePic(1194)

Try
Dim byt As Byte()
While sqlce.Read
byt = sqlce.Item("EmployeePic")
Dim bmp As New Bitmap(New System.IO.MemoryStream(byt))
img = bmp
bmp = Nothing
End While

Me.PictureBox1.Image = img

Catch ex As OutOfMemoryException
MessageBox.Show(ex.Message)
End Try
 
A

Alex Feinman [MVP]

How many bytes are you getting back in your byte array when reading the
image data from SqlCeDataReader?
How does it compare with the actual size of data you previously stored
there?
If the array is non-empty, what are the first 4 bytes?
 
H

Heather B.

The byte count when I save the image is 32577 and that is the same count I
get reading it back out. The first 4 bytes are 0's, actually all the bytes
are 0's when I look at them through the watch window.
 
A

Alex Feinman [MVP]

There you go. I think the replication is not working properly. You need to
track down where exactly the data are lost
 
H

Heather B.

But at this point I'm not even using replication. The code I listed is on
one form under two seperate buttons, one takes the picture and stores it
into the database, the other reads it back out, I'm not doing any
sychronization in this test program. So I guess my write into the database
isn't working correctly, any ideas?
 
J

Joseph Byrns

Unless I am missing something you do not seem to be reading the file
contents into the byte array.

So after your Dim img as Byte()... line you should have:

fs.Read(img, 0, fs.Length)

Does that help?
 
A

Alex Feinman [MVP]

Excellent catch

Joseph Byrns said:
Unless I am missing something you do not seem to be reading the file
contents into the byte array.

So after your Dim img as Byte()... line you should have:

fs.Read(img, 0, fs.Length)

Does that help?
 
H

Heather B.

Indeed, excellent catch, that fixed the problem. I can now write an image
into the database, bring it out into a picture box or write it as a jpg to
the disk. Thank you both so much!!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top