Advertisement

07.16.2008 at 03:38PM PDT, ID: 23571580
[x]
Attachment Details
[x]
The Solution Rating System

With so many solutions, how can you tell which solutions are most likely to help you and which ones are not? To provide you with a tool to use, we rate our solutions based on various elements that most accurately determine if a solution is a quality solution. To explain what factors affect the solution rating, here are the elements we take into consideration when formulating our solution rating.

  • The Grade of the Solution
  • The Zone Rank of the Expert Providing the Solution
  • The Number of Author and Expert Comments
  • The Number of Experts Contributing
  • The Feedback of the Community

Your Input Matters
Because of the way the system is set up, the most important variable in this equation is you. As a member of Experts Exchange, you are able to cast your vote on the quality of the solutions in regard to how complete, accurate, helpful and easy to understand each solution is. When you provide your feedback, each rating is adjusted accordingly. So, if you see a solution that has a poor rating that you think is a good solution, let us know by rating it. As you do, the rating will be adjusted and will become more accurate for other members of our site.

If you have any suggestions that you would like to make for our rating system, please ask a question in the Suggestions Zone of Community Support.

Thank you!

8.2

Turn a Base64 encoded string into a .png image

Asked by eg252 in Extensible Markup Language (XML), Web Images

Tags:

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.Start Free Trial
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
59:
60:
61:
62:
63:
64:
65:
66:
67:
68:
69:
70:
71:
72:
73:
74:
75:
76:
77:
78:
79:
80:
81:
82:
83:
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
Attachments:
 
this is the string of the image
 
[+][-]07.17.2008 at 04:41AM PDT, ID: 22024296

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 30-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]07.17.2008 at 06:52AM PDT, ID: 22025425

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 30-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]07.17.2008 at 07:21AM PDT, ID: 22025731

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 30-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]07.17.2008 at 08:17AM PDT, ID: 22026391

View this solution now by starting your 30-day free trial. Setting up your free trial is quick, easy, and secure. We will return you to this solution, unlocked, when you're done.

 

About this solution

Zones: Extensible Markup Language (XML), Web Images
Tags: xml
Sign Up Now!
Solution Provided By: eg252
Participating Experts: 1
Solution Grade: A
 
 
 
Loading Advertisement...
20081112-EE-VQP-44 / EE_QW_2_20070628