How to convert String Picture back to Object, Decoding again

Dossfm0q

Banned User
Joined
Mar 9, 2009
Messages
570
Office Version
  1. 2019
Platform
  1. Windows
Here Encoder how to convert to Decoder

Code:
Sub EncdPic()


Call EncodeFilebase64("C:\Users\User\Desktop\Untitled.png") '"FullPath with Pic Extention"




End Sub
Public Function EncodeFilebase64(strPicPath As String) As String


    Dim PicExtn As String, FLPath As String
    Dim StrPath As Variant
    Dim BSC As Long
    Dim fso As Object
    
    PicExtn = Split(strPicPath, ".")(1)
   ' StrPath = Split(strPicPath, "\")
    'BSC = UBound(StrPath)
    'FLPath = Left(strPicPath, Len(strPicPath) - Len(StrPath(BSC)))
    
     FLPath = Replace(strPicPath, PicExtn, ".txt")
     
   'or remove ' apostrophe from below
   'FLPath = Replace(strPicPath, PicExtn, ".htm") ' swich to show it in HTML PIC
    
    
    '[SIZE=3][B][COLOR=#ff0000]  [/COLOR][COLOR=#b22222]Please Remove Space after < in "< img[/COLOR][COLOR=#ff0000][/COLOR][/B][/SIZE]
    ' EncodeFilebase64 = "< img src='data:image/" & PicExtn & ";base64," & EncodeFile(strPicPath) & "'/>" ' to be used for HTML
    'or use below
    EncodeFilebase64 = EncodeFile(strPicPath)
    
    'Close notepad File if it open
    
     Close_Notepad_ByName FLPath
     
     ' Kill File
     If Len(Dir(FLPath)) <> 0 Then Kill FLPath
    Set fso = CreateObject("Scripting.FileSystemObject")
 
    Dim Fileout As Object
    Set Fileout = fso.CreateTextFile(FLPath, True, True)
    Fileout.Write EncodeFilebase64
    Fileout.Close
    
    'Open File
    If InStr(1, FLPath, "txt", vbTextCompare) <> 0 Then
    Call Shell("Notepad" & " " & FLPath, vbNormalFocus)
    ElseIf InStr(1, FLPath, "htm", vbTextCompare) <> 0 Then
    
    Call Shell("explorer.exe" & " " & FLPath, vbNormalFocus)
    End If


Set Fileout = Nothing
Set fso = Nothing
End Function
Public Function EncodeFile(strPicPath As String) As String


    Const adTypeBinary = 1          ' Binary file is encoded


    ' Variables for encoding
    Dim objXML
    Dim objDocElem


    ' Variable for reading binary picture
    Dim objStream


    ' Open data stream from picture
    Set objStream = CreateObject("ADODB.Stream")
    objStream.Type = adTypeBinary
    objStream.Open
    objStream.LoadFromFile (strPicPath) ' if error check Path is correct or Exist


    ' Create XML Document object and root node
    ' that will contain the data
    Set objXML = CreateObject("MSXml2.DOMDocument")
    Set objDocElem = objXML.createElement("Base64Data")
    objDocElem.DataType = "bin.base64"


    ' Set binary value
    objDocElem.NodeTypedValue = objStream.Read()


    ' Get base64 value
    EncodeFile = objDocElem.Text


    ' Clean all
    Set objXML = Nothing
    Set objDocElem = Nothing
    Set objStream = Nothing


End Function
Public Sub Close_Notepad_ByName(NtpPath As String)


Dim oServ As Object
Dim cProc As Object
Dim oProc As Object
StrProcessName = "Notepad.exe"
Set oServ = GetObject("winmgmts:")
Set cProc = oServ.ExecQuery("select * from win32_process")
For Each oProc In cProc
    If InStr(1, oProc.Name, StrProcessName, vbTextCompare) <> 0 Then ' check if Notepad
        If InStr(1, oProc.CommandLine, NtpPath, vbTextCompare) <> 0 Then ' check Path
        oProc.Terminate
        End If
    End If
Next
Set oServ = Nothing
Set cProc = Nothing
End Sub
 
Last edited:

Excel Facts

Whats the difference between CONCAT and CONCATENATE?
The newer CONCAT function can reference a range of cells. =CONCATENATE(A1,A2,A3,A4,A5) becomes =CONCAT(A1:A5)

Forum statistics

Threads
1,223,886
Messages
6,175,190
Members
452,616
Latest member
intern444

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top