Avatar of eg252
eg252

asked on 

Turn a Base64 encoded string into a .png image

I'm getting a xml file with a .png picture embedded in base 64. How can I decode and save as picture file? I'm using VBA. I'm attaching the code i'm using which obviously doesn't work. I attached the string of the image to the question as test.txt.
I need that file a png.
this is the part of the xml that has the image.
<Image ImageFormat="PNG" dt:dt="bin.base64" xmlns:dt="urn:schemas-microsoft-com:datatypes">iVBORw0KGgoAAAANSUhEUgAAAp0AAAIvAQMAAAAMGfIDA....AAAAAElFTkSuQmCC</Image>
 
I'm using the following VBA:
Const sBASE_64_CHARACTERS  "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"
 
Function fGetWebPost(strSourceURL As String, strRequest As String)
Dim xmlServerHttp As Object, co As Long, s As String
Set xmlServerHttp = CreateObject("MSXML2.ServerXMLHTTP.4.0")
With xmlServerHttp
    .Open "POST", strSourceURL, False
    '.setRequestHeader "Content-Type", "application/x-www-form-urlencoded"
    .send (strRequest)
    s = .responseText
End With
fGetWebPost = s
 
 
Open "c:\test.png" For Output As #1
Print #1, Base64decode(Mid(s, 1312, 11798))
Close #1
 
Set xmlServerHttp = Nothing
End Function
'**************************************
    ' Name: Base 64 Encode / Decode
    ' Description:Base 64 encodeing is used
    '     to convert binary files to a "safe" form
    '     at for transporting files through smtp (
    '     email) and other protocols. It is also u
    '     sed for basic authentication. With this
    '     code, you can decode the current UserNam
    '     e/Password who is visiting a protected p
    '     age on your site by requesting one of th
    '     e serverVariables.
    ' By: Lewis E. Moten III
    '
    ' Inputs:These two algorithims are expec
    '     ting a string that is to be encoded or d
    '     ecoded.
    '
    ' Returns:Returns a string that was enco
    '     ded or decoded.
    '
    ' Side Effects:The string returned from
    '     encoding with Base64 is not delimited by
    '     cariadge returns. Some formats that I ha
    '     ve seen in the past, such as email - may
    '     continue to the next line after the 80th
    '     character.
    'Also, these functions work With Strings. (Not binary data). When working with binary data, some changes may need To come into place.
    '
    'This code is copyrighted and has    ' limited warranties.Please see http://w
    '     ww.Planet-Source-Code.com/vb/scripts/Sho
    '     wCode.asp?txtCodeId=6268&lngWId=4    'for details.    '**************************************
    
    ' --------------------------------------
    '     ---------------------------------------
    Function Base64decode(ByVal asContents)
        Dim lsResult
        Dim lnPosition
        Dim lsGroup64, lsGroupBinary
        Dim Char1, Char2, Char3, Char4
        Dim Byte1, Byte2, Byte3
        If Len(asContents) Mod 4 > 0 Then asContents = asContents & String(4 - (Len(asContents) Mod 4), " ")
        lsResult = ""
        
        For lnPosition = 1 To Len(asContents) Step 4
            lsGroupBinary = ""
            lsGroup64 = Mid(asContents, lnPosition, 4)
            Char1 = InStr(sBASE_64_CHARACTERS, Mid(lsGroup64, 1, 1)) - 1
            Char2 = InStr(sBASE_64_CHARACTERS, Mid(lsGroup64, 2, 1)) - 1
            Char3 = InStr(sBASE_64_CHARACTERS, Mid(lsGroup64, 3, 1)) - 1
            Char4 = InStr(sBASE_64_CHARACTERS, Mid(lsGroup64, 4, 1)) - 1
            Byte1 = Chr(((Char2 And 48) \ 16) Or (Char1 * 4) And &HFF)
            Byte2 = lsGroupBinary & Chr(((Char3 And 60) \ 4) Or (Char2 * 16) And &HFF)
            Byte3 = Chr((((Char3 And 3) * 64) And &HFF) Or (Char4 And 63))
            lsGroupBinary = Byte1 & Byte2 & Byte3
            
            lsResult = lsResult + lsGroupBinary
        Next
        Base64decode = lsResult
    End Function

Open in new window

test.txt
Web DevelopmentXML

Avatar of undefined
Last Comment
eg252
Avatar of Ray Paseur
Ray Paseur
Flag of United States of America image

@EQ252: This sort of thing is often used in CAPTCHA tests.  Would a PHP script be of any use to you? ~Ray
Avatar of eg252
eg252

ASKER

no i use vba.. it has to be vba - sorry
Avatar of Ray Paseur
Ray Paseur
Flag of United States of America image

Understood.  Good luck, ~Ray
ASKER CERTIFIED SOLUTION
Avatar of eg252
eg252

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
Web Development
Web Development

Web development includes all aspects of presenting content on intranets and the Internet, including delivery development, protocols, languages and standards, server software, browser clients, databases and multimedia generation.

78K
Questions
--
Followers
--
Top Experts
Get a personalized solution from industry experts
Ask the experts
Read over 600 more reviews

TRUSTED BY

IBM logoIntel logoMicrosoft logoUbisoft logoSAP logo
Qualcomm logoCitrix Systems logoWorkday logoErnst & Young logo
High performer badgeUsers love us badge
LinkedIn logoFacebook logoX logoInstagram logoTikTok logoYouTube logo