New topic: 

displaying blob.

<http://forums.realsoftware.com/viewtopic.php?t=38695>

         Page 1 of 1
   [ 5 posts ]                 Previous topic | Next topic          Author  
Message        whelpton          Post subject: displaying blob.Posted: Tue Apr 
19, 2011 4:12 pm                         
Joined: Tue Apr 12, 2011 10:27 am
Posts: 6                Okay guys, so Ive got an image stored as a blob in a 
mysql database. Im currently using the MySQLCommunityServer plugin & I was 
wondering if theres anyway that I can display the image in my program & how 
would that be possible?

Thanks a bunch.   
                             Top                 jlawrence          Post 
subject: Re: displaying blob.Posted: Tue Apr 19, 2011 4:57 pm                   
      
Joined: Thu Dec 16, 2010 1:22 pm
Posts: 81                absolutely!
you have (at least) two options.
store your blob in a memory block and then use the 
Picture.FromData method
http://docs.realsoftware.com/index.php/Picture.FromData

or (much simpler to me as I don't have a lot of experience with memory blocks)

write your blob out to a binary stream in a temporary file and then open that 
file as a picture.

If you need more assistance, I'll be happy to write some sample code for the 
2nd method... but it's quitting time so I'm going home now    
                             Top                 whelpton          Post 
subject: Re: displaying blob.Posted: Tue Apr 19, 2011 5:18 pm                   
      
Joined: Tue Apr 12, 2011 10:27 am
Posts: 6                I vaguely get the second idea, which would mean that 
the data would be saved as a variable and then outputted to the image? 

As for me needing more assistance, if you could that would be brilliant, I'm 
very new to programming & need all the assistance I can get  

Thanks!   
                             Top                 Jym          Post subject: Re: 
displaying blob.Posted: Tue Apr 19, 2011 7:02 pm                         
Joined: Sat Oct 01, 2005 5:19 pm
Posts: 2374                Probably the same for mysql but this is how I do it 
with REALSLQ

Make a Module and add to it the following 2 Functions (on a PC you can 
highlight the green text and copy and paste to the left pane of the Module)

Code:Function ConvertToHex(s As STring) As String
  
  Dim m As MemoryBlock
  Dim spos, mpos, bytes, b As Integer
  
  bytes = s.LenB
  if bytes < 1 then return ""
  
  Dim hexChar() As Integer = Array( _
  48,49,50,51,52,53,54,55,56,57,65,66,67,68,69,70 )
  
  m = New MemoryBlock( bytes*2 + bytes - 1 )
  m.LittleEndian = True
  for spos = 1 to bytes
  b = AscB( MidB( s, spos, 1 ) )
  if b < 16 then
  m.Byte(mpos) = hexChar(0)
  m.Byte(mpos+1) = hexChar(b)
  else
  m.Byte(mpos) = hexChar(b \ 16)
  m.Byte(mpos+1) = hexChar(b mod 16)
  end if
  if spos < bytes then m.Byte(mpos+2) = 32  // space
  mpos = mpos + 3
  next
  
  return DefineEncoding( m.StringValue(0, m.size), Encodings.ASCII )
End Function

and 
Code:Function hexToBinary(s as string) As string
  
  Dim i as integer
  Dim index as integer
  Dim count as integer
  Dim mb as MemoryBlock
  
  count = s.lenB
  
  mb = new MemoryBlock(count / 2)
  
 for i = 1 to count step 2
  
 mb.byte(index) = Val("&h" + midB(s, i, 2))
  
  index = index + 1
  
  next
  
  return mb.StringValue(0, count / 2)
  
End Function



To save to a database do something like (where p is your picture object)

Code:Dim s As String = ReplaceAll(ConvertToHex(p.GetData(Picture.FormatPNG, 
Picture.QualityMax)), " ", "")
yourdatabase.SQLExecute"INSERT INTO pics(pic) VALUES ('" + s + "')

to get it back to a picture

Code:Dim rs as RecordSet = yourdatabase.SQLSelect("SELECT pic FROM pics")
Dim p As Picture = Picture.FromData(hexToBinary(rs.IdxField(1).getString))

That's all there is too it   
                             Top                whelpton          Post subject: 
Re: displaying blob.Posted: Tue Apr 19, 2011 7:40 pm                         
Joined: Tue Apr 12, 2011 10:27 am
Posts: 6                Jym, thanks for the reply, Ive tried your code out, 
however when I run the program I get the following error.

  Dim p As Picture = Picture.FromData(hexToBinary(rs.IdxField(1).getString))
  

I have currently placed the statement beneath the following code, am I 
implimenting it correctly?

  myrec = db.sQLSelect(s)  //execute the query
  rec.title=myrec.Field("recname").stringValue
  name.text=myrec.Field("recname").stringValue
  instructions.text=myrec.Field("Instructions").stringValue
  instructions.text=myrec.Field("Instructions").stringValue
  METABOLIC=myrec.Field("metabolic").stringValue

  Dim rs as RecordSet =  db.SQLSelect("SELECT file_data FROM ecook WHERE 
image_id = '" + str(dashboard.recid) + "'""")
  Dim p As Picture = Picture.FromData(hexToBinary(rs.IdxField(1).getString))

Canvas1.Backdrop=p
  
Thank you   
                             Top            Display posts from previous: All 
posts1 day7 days2 weeks1 month3 months6 months1 year Sort by AuthorPost 
timeSubject AscendingDescending          Page 1 of 1
   [ 5 posts ]      
-- 
Over 1500 classes with 29000 functions in one REALbasic plug-in collection. 
The Monkeybread Software Realbasic Plugin v9.3. 
http://www.monkeybreadsoftware.de/realbasic/plugins.shtml

[email protected]

Reply via email to