I made a mistake in previous post. The code should have been
' Loads the image to the picture box control from SQL Server
Dim ImageByteArr(0) As Byte ' An array to hold image (bytes)
Dim patientIC As String ' Patient's IC for Image
' Get ID from a text box control and remove spaces
patientIC = txtId.Text.Trim
' Load an array of bytes from SQL Server's field of type Image
LoadByteArray(ImageByteArr, patientIC)
If ImageByteArr.Length <= 1 Then
MsgBox("Empty byte array! patientIC='" & patientIC & "' might not exist?")
End If
' Convert an array of bytes to image
Dim TempImage As Image = Nothing
Byte2Image(TempImage, ImageByteArr)
If TempImage IsNot Nothing Then
pbBody.Image = TempImage
Else
MsgBox("Image is nothing!")
End If
Code line 9 changes a bit. Do you get now "Empty byte array..." message?
Two possibilities left. The ID patientIC is still not found. Or the saving of the image fails in the first place.
If you view the DB data in SQL Server Management Studio, it shows <Binary data> in the Image type field, but it may not be valid data. You could try to execute following SQL statement in Management Studio
SELECT [ImageID], [BlobField]
FROM [ImageTable]
WHERE [ImageID]='patient101'
(just replace correct field and table names and ID value). You should now see if there's actually data in Image field:
ImageID BlobField
patient101 0xFFD8FFE000104A46494600010101006...
Image data is dumped in hex format but you'll see that there's actually some data in the field.
Another (and possible a better) way is to execute query
SELECT DATALENGTH([BlobField]) AS DataLen
FROM [ImageTable]
WHERE [ImageID]='patient101'
I got the result
DataLen
2319
for an image. This is a value you can compare to the length of data you're originally saving. You had (my variable names, sorry) Image2Byte(Form2.BitmapCanvas, ImageByteArr)
in your code. Write after that line MsgBox(ImageByteArr.Length)
and compare those two lengths, they should be the same.
I hope this didn't get too complicated :) There's some small bug left in the code, but it's hard to catch.