0

With the following function (source) can I load and then encode to base64 an image. Now I would like to resize the image (keeping width/height ratio) before encoding it. I'm not used to program in Vba and I did not find any example or snippet that may help me.

Public Function EncodeFileBase64(strPicPath As String) As String
    Const adTypeBinary = 1

    Dim objXML
    Dim objDocElem

    Dim objStream

    Set objStream = CreateObject("ADODB.Stream")
    objStream.Type = adTypeBinary
    objStream.Open
    objStream.LoadFromFile (strPicPath)

    Set objXML = CreateObject("MSXml2.DOMDocument")
    Set objDocElem = objXML.CreateElement("Base64Data")
    objDocElem.DataType = "bin.base64"

    objDocElem.nodeTypedValue = objStream.Read()

    ' EncodeFileBase64 = objDocElem.Text
    EncodeFileBase64 = Replace(objDocElem.Text, vbLf, "")

    Set objXML = Nothing
    Set objDocElem = Nothing
    Set objStream = Nothing
End Function
Community
  • 1
  • 1
Nicero
  • 4,181
  • 6
  • 30
  • 52

1 Answers1

0

After lots of temptatives the most pratical solution is:

  1. resize the image
  2. save the resized image to a file
  3. load the saved file and apply base64 encoding to this

VBA does not have instruments to manipulate images so you must use some external software. I used ImageMagick: take a look at my solution to an other post of mine.

Community
  • 1
  • 1
Nicero
  • 4,181
  • 6
  • 30
  • 52