转换图像(JPG)为base64在Excel VBA? [英] Convert image (jpg) to base64 in Excel VBA?

查看:3326
本文介绍了转换图像(JPG)为base64在Excel VBA?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要的将图像转换Excel内部(或通过VBA)为base64 (在最后,我将XML输出​​)。

I need to convert an image inside Excel (or through VBA) to base64 (in the end I will make XML output).

我怎样才能做到这一点?我是否需要对DOM参考?

How can I do this? Do I need to make a reference to DOM?

从来就一直在读<一个href=\"http://stackoverflow.com/questions/169907/how-do-i-base64-en$c$c-a-string-efficiently-using-excel-vba\">this问题但它仅适用于文本字符串不是图像...

I´ve been reading this question but it only works for text strings not images...

没有人有任何code,我可以看到?

Does anyone have any code that I can see?

推荐答案

继承人的功能。不记得从我得到了它。

Heres a function. Can't remember where I got it from.

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)

    ' 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

这篇关于转换图像(JPG)为base64在Excel VBA?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆