0

I need to perform a data conversion from the old db ms sql 6.5 , now i have issue with the image store inside ms sql 6.5, this image stored are all ole data meaning the old application that interface this ms sql 6.5 actually store the image as ole in the image type. when i selectblob into a blob in powerbuilder i need to send this blob into ole_1.objectdata, then translate this ole_1.objectdata into the desired length in order to be output into a bitmap file on disk, i have extracted this translation code from expert-exchanged at "Printing Blob to bmp/jpg" (posted by Buasuwan, however cannot manage to get the dll as the post is an old post). It work fine for 60% of my blob converted to bitmap while the remaining produce empty view with relevant file size, just cannot view it only .here is the code, i wish some gurus there can help me with the ole translation to bitmap

    Blob lb_image

    SelectBLOB picture_image into :lb_image 
    from individual
    where individual_object_id='200506061121430020'
    using SQLCA;

    if SQLCA.sqlcode<>0 then
      messagebox("cannot connect","cannot connect")
    end if


    if(len(lb_image)>0) then


    ole_1.objectdata=lb_image


    gf_convertbmp(ole_1.objectdata,ls_path)


    end if

my gf_convertbmp as follows

    long          ll_index, ll_len, ll_length
    integer     li_FileNum

    // Find Keyword 'BM' for starting Bitmap File


     ll_len = Len(lb_ole_data)

     ll_index = 1 

     blob     lb_bm
     lb_bm = blob('BM')
     do while ll_index <= ll_len

       if BlobMid(lb_ole_data, ll_index, 2) = lb_bm then 

      exit
       end if   

        ll_index++;
     loop

     // Find Length of Image


      ll_length =  long(asc(char(BlobMid(lb_ole_data, ll_index - 4, 1)))) + &
                long(asc(char(BlobMid(lb_ole_data, ll_index - 3, 1)))) * 256 + &
                long(asc(char(BlobMid(lb_ole_data, ll_index - 2, 1)))) * 65536

       // Save Bitmap to File


      li_FileNum = FileOpen(filename, StreamMode!, Write!, LockWrite!, Replace!)


      // Write Bitmap Data


     do while ll_length > 0
          if ll_length > 32000 then
            FileWrite(li_FileNum, BlobMid(lb_ole_data, ll_index, 32000))
          else
            FileWrite(li_FileNum, BlobMid(lb_ole_data, ll_index, ll_length))
          exit
          end if
       ll_index += 32000
       ll_length -= 32000
      loop



      FileClose(li_FileNum)

1 Answer 1

0

If it really is an OLE wrapped bitmap, you're best approach (or at least the one I use) is to open the data as an olestream, and then look for the Ole10Native storage within the stream that contains the data.

// ls_storage is filename where data is written out
ll_rc = lole_storage.Open ( ls_storage )  

//Check to see that the Ole10Native storage exists.
ls_streamname = Char(1) + 'Ole10Native'
lole_storage.MemberExists ( ls_streamname, lb_objectexists )        
IF lb_objectexists THEN 
  //Start 4 bytes into the storage to get the file
  li_startat = 4            
ELSE
  Return -1
END IF  

ll_rc = lole_stream.Open( lole_storage, ls_streamname, stgRead!, stgExclusive! )

//Get the length of the OLE stream
ll_rc = lole_stream.Length ( ll_streamlen )


//Determine how many times to call Read
//read returns a maximum or 32765 characters at a time
//We are going to Seek to the first position, so don't include it in the
//calculations.  Also note that the Seek is zero-indexed, so we remove one
//from the startat for our calcs
ll_streamlen = ll_streamlen - ( li_startat - 1 )
IF ll_streamlen > ll_chunk THEN
    IF Mod( ll_streamlen, ll_chunk ) = 0 THEN
        ll_loops = ll_streamlen/ll_chunk
    ELSE
        ll_loops = ( ll_streamlen/ll_chunk ) + 1
    END IF
ELSE
    ll_loops = 1
END IF


//Read the OLE stream, starting at the requested position
ll_newpos = li_startat
FOR ll_i = 1 to ll_loops
    lole_stream.Seek ( ll_newpos )  
    ll_rc = lole_stream.Read( lblob_temp, ll_chunk )
    IF ll_i = 1 THEN
        ablb_dataout = lblob_temp
    ELSE
            ablb_dataout = ablb_dataout + lblob_temp
    END IF
    ll_newpos = ll_newpos + ll_chunk
NEXT

At this point you have a blob with the bitmap data

However, the data I've been working with was stored by MS Paint. Something we discovered recently is that newer versions of MS Paint store the data in WMF format rather than BMP. What we do in that case is look for the OlePres000 storage rather than the Ole10Native storage, and then start 40 characters into that storage rather than 4.

2
  • Amazing! Where do you get all these details? Did you follow any document on this or how? :)
    – somnath
    May 30, 2012 at 13:25
  • The bit about using OLEStreams and OLEStores in general was from a Sybase document. The rest was examining the compound files directly, figuring out their structure, and then what as needed to parse them. Feb 23, 2013 at 1:03

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.