将base64转换为图像

时间:2016-10-16 15:43:52

标签: vba

我有以下2个函数,它们为我提供了图像的base64

Option Compare Database
Option Explicit

Function readBytes(strFile As String) As Variant
  Const TypeBinary = 1
  Dim inStream As Object
  ' ADODB stream object used
  Set inStream = CreateObject("ADODB.Stream")
  ' open with no arguments makes the stream an empty container
  inStream.Open
  inStream.Type = TypeBinary
  inStream.LoadFromFile strFile
  readBytes = inStream.Read()
End Function

Function encodeBase64(arrBytes As Variant) As String
  Dim DM As Object, EL As Object
  Set DM = CreateObject("Microsoft.XMLDOM")
  ' Create temporary node with Base64 data type
  Set EL = DM.createElement("tmp")
  EL.dataType = "bin.base64"
  ' Set bytes, get encoded String
  EL.nodeTypedValue = arrBytes
  encodeBase64 = EL.Text
End Function

Sub test()

TestIt CurrentProject.Path & "\pic.jpg"

End Sub

Function TestIt(strFile As String) As String

  Dim arrBytes As Variant, strRet As String

  arrBytes = readBytes(strFile)
  strRet = encodeBase64(arrBytes)

Dim s As String
Open CurrentProject.Path & "\pic_base64.txt" For Binary As #1
Put #1, 1, strRet
Close #1


End Function

我需要将以下两个函数转换为与上述函数相同的VBA格式,并添加一个额外的测试函数,它将从上面的函数中获取base64转换后的图像字符串并将其写回图像pic.jpg

private function decodeBase64(base64)
  dim DM, EL
  Set DM = CreateObject("Microsoft.XMLDOM")
  ' Create temporary node with Base64 data type
  Set EL = DM.createElement("tmp")
  EL.DataType = "bin.base64"
  ' Set encoded String, get bytes
  EL.Text = base64
  decodeBase64 = EL.NodeTypedValue
end function

private Sub writeBytes(file, bytes)
  Dim binaryStream
  Set binaryStream = CreateObject("ADODB.Stream")
  binaryStream.Type = TypeBinary
  'Open the stream and write binary data
  binaryStream.Open
  binaryStream.Write bytes
  'Save binary data to disk
  binaryStream.SaveToFile file, ForWriting
End Sub

0 个答案:

没有答案
相关问题