I am using MSSQL to store an image/tiff file, under the image datatype. I want to retrieve the image file from the database for viewing through VB. The codes below is to retrieve the image file, which is rather correct if I am not mistaken.

ChunkSize = objRS("Fax").ActualSize
ImageFile = (objRS("Fax").GetChunk(ChunkSize))

The problem is if the actual image size is 10000 bytes, all I get to retrieve is 5000 bytes, 50% of the actual file size. I tried it using different file formats but it is still the same. When I edited the retrieved image file's contents... I found garbled characters different from the actual image file's contents. Anybody know the answer to this and the way to solve it. Please any ideas... I tried MS Knowledgebase and Technet but I cant find the solution.
Who is Participating?
wqwConnect With a Mentor Commented:
sorry for the late call. check this code.

i created a table first in northwind db with this sql:

create table images(id int identity, blob image)

you need a form in VB with 2 command buttons, 2 picture boxes and a common dialog control (ctr+t - components dialog). leave default names. reference ado 2.1 too. paste this code in your form

Option Explicit

Private Const CONN_STR = "Provider=SQLOLEDB.1;Integrated Security=SSPI;Persist Security Info=False;Initial Catalog=Northwind"

Private Sub Command1_Click()
    Dim rs As New Recordset, oPic As StdPicture, oBag As New PropertyBag
    rs.CursorLocation = adUseClient
    rs.Open "images", CONN_STR, adOpenKeyset, adLockOptimistic
    CommonDialog1.Filter = "Graphics (*.bmp;*.gif;*.jpg)|*.bmp;*.gif;*.jpg|All files (*.*)|*.*"
    If CommonDialog1.FileName <> "" Then
        Set oPic = LoadPicture(CommonDialog1.FileName)
        oBag.WriteProperty "FileName", CommonDialog1.FileName
        oBag.WriteProperty "Image", oPic
        rs!Blob = oBag.Contents
        Set Picture1.Picture = oPic
    End If
End Sub

Private Sub Command2_Click()
    Dim rs As New Recordset, oBag As New PropertyBag
    rs.CursorLocation = adUseClient
    rs.Open "images", CONN_STR, adOpenKeyset, adLockOptimistic
    If Not rs.EOF Then
        oBag.Contents = rs!Blob.Value
        Set Picture2.Picture = oBag.ReadProperty("Image")
    End If
End Sub

basicly this code save images to image field. unfortunately uncompressed. may i suppose it's datatype problem that you have -- binary field instead of image. binary field can take up to 8060 bytes of data.

let me know if this is working for you.


the error is in

ImageFile = (objRS("Fax").GetChunk(ChunkSize))

should be

ImageFile = objRS("Fax").GetChunk(ChunkSize)

and ImageFile must be

Dim ImageFile() As Byte

The parentheses convert the byte array to string which is the intrinsic way in VB to convert from Unicode to Ascii. so you get 1 byte for every 2 bytes :-))

Let me know if this works for you.


LawrenceYAuthor Commented:
The codes cant work. Below is my actual program codes.

objRS.MoveFirst (my database)
    For i = 1 To numrecords (total records)
        ChunkSize = objRS("Image").ActualSize
        FaxMailData = objRS("Image").GetChunk(ChunkSize)
        Image2 = Image        ChunkSize2 = Len(Image2)
        MsgBox "ActualSize=" & ChunkSize & " | " & "ConvertedSize=" & ChunkSize2    'Debug
        MsgBox FaxMailData()    'Debug
    Next i

Find anything wrong with it? Please anybody tell me. Thanks.    
give me the Dim's. each variable declaration

You should use getchunk in a loop to retrieve the data in pieces rather than by trying to get the whole lot. There are restrictions on the number of bytes that can be extracted from a field in one go, this is defaulted to 5K unless you use settextsize etc which can prove a real pain!

This example gets the data in 1K blocks from the field.

   conChunkSize = 1000
   lngLogoSize = rstPubInfo!logo.ActualSize
   Do While lngOffset < lngLogoSize
      varChunk = rstPubInfo!logo.GetChunk(conChunkSize)
      varLogo = varLogo & varChunk
      lngOffset = lngOffset + conChunkSize
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.