Link to home
Start Free TrialLog in
Avatar of kperelman
kperelman

asked on

how to convert xml base64 string gif/jpg/png image file using vba

I am not sure why this is not working?

I am using vba:

If have a base64 gif string (here's the excerpt):

R0lGODlhsAQIB/cAAAAAAI  ... <more here> ... u+I1r3rdK1/76te/Ajawgh1jQAAAOw==

I attempted to create a binary file by loading the xml above into a string: base64_string

   dim btArr() As Byte
   dim base64_string as string

   base64_string=<xml from above>
   btArr = base64_string

   Open 'image.gif" For Binary As #1
   Put #1, 1, Base64decode(btArr)
   Close #1

But when I try to see the image by opening the file, it says the image format is invalid.  

What else do I need to do to make the file a  gif file?
Avatar of als315
als315
Flag of Russian Federation image

Code from this page:
http://www.nonhostile.com/howto-encode-decode-base64-vb6.asp
works for me.
Sub save_gif()
Dim A As String
A = "R0lGODlhEAAOALMAAOazToeHh0tLS/7LZv/0jvb29t/f3//Ub//ge8WSLf/rhf/3kdbW1mxsbP//mf///yH5BAAAAAAALAAAAAAQAA4AAARe8L1Ekyky67QZ1hLnjM5UUde0ECwLJoExKcppV0aCcGCmTIHEIUEqjgaORCMxIC6e0CcguWw6aFjsVMkkIr7g77ZKPJjPZqIyd7sJAgVGoEGv2xsBxqNgYPj/gAwXEQA7"
Open "c:\tmp\image.gif" For Binary As #1
   Put #1, 1, DecodeBase64(A)
Close #1
End Sub


 
Private Function DecodeBase64(ByVal strData As String) As Byte()
 
    Dim objXML As MSXML2.DOMDocument
    Dim objNode As MSXML2.IXMLDOMElement
    
    ' help from MSXML
    Set objXML = New MSXML2.DOMDocument
    Set objNode = objXML.createElement("b64")
    objNode.DataType = "bin.base64"
    objNode.Text = strData
    DecodeBase64 = objNode.nodeTypedValue
    
    ' thanks, bye
    Set objNode = Nothing
    Set objXML = Nothing
 
End Function

Open in new window

You need reference to Microsoft XML library (I've used 6.0)
Avatar of kperelman
kperelman

ASKER

Thanks.

The function works fine.  

Is there an algorythm written in a vba function that does the same thing?
You like to have structures from XML library in vba?
For the project the algorythm is what I was looking for to endcode and decode.
ASKER CERTIFIED SOLUTION
Avatar of als315
als315
Flag of Russian Federation image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial