使用存储在CustomXMLPart中的base64数据作为Office中的图像 [英] Using base64 data stored in CustomXMLPart as image in Office

查看:234
本文介绍了使用存储在CustomXMLPart中的base64数据作为Office中的图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

作为此问题关于使用功能区中的按钮上存储在Excel文件中的图像的后续操作:



是否可以将存储在base64编码字符串中的CustomXMLPart / CustomXMLNode中的图像作为Office文档中的图像,而无需先将其保存到磁盘并将其加载回来? / p>

我想使用图像的地方需要一个 IPictureDisp 对象作为参数(如 LoadPicture 函数返回,但只会从磁盘加载文件)。

解决方案

需要将base_64数据转换为字节数组:

 私有函数decodeBase64(ByVal strData As String)As Byte()
Dim objXML As MSXML2.DOMDocument
Dim objNode As MSXML2.IXMLDOMElement

设置objXML =新建MSXML2.DOMDocument
设置objNode = objXML.createElement(b64)
objNode.DataType =bin。 base64
objNode.Text = strData
decodeBase64 = objNode.nodeTypedValue

设置objNode = Nothing
设置objXML = Nothing
结束函数
http://thydzik.com/vb6vba-functions-to-convert-binary-string-to-base64-string/



然后,您可以将其加载到内存中,并使用有关此主题的信息创建IPictureDisp: http://www.xtremevbtalk.com/showthread.php?t=137857

 键入GUID 
Data1 As Long
Data2 As Integer
Data3 As Integer
Data4(7)As Byte
结束类型

私有声明函数CreateStreamOnHGlobal Libole32.dll(ByRef hGlobal As Any,ByVal fDeleteOnResume As Long,ByRef ppstr As Any)As Long
私有声明函数OleLoadPicture Lib pro32.dll(ByVal lpStream As IUnknown,ByVal lSize As Long,ByVal fRunMode As Long,ByRef riid As GUID,ByRef lplpObj As Any)As Long
私有声明函数CLSIDFromString Libole32.dll(ByVal lpsz As Long,ByRef pclsid as GUID)As Long

Private Const SIPICTURE As String ={7BF80980-BF32-101A-8BBB-00AA00300CAB}

公共功能PictureFromArray(ByRef b ()As Byte)As IPicture
On Error GoTo errorhandler

Dim istrm As IUnknown
Dim tGuid As GUID

如果不是CreateStreamOnHGlobal(b(LBound) (b)),False,istrm)然后
CLSIDFromString StrPtr(SIPICTURE),tGuid
OleLoadPicture istrm,UBound(b) - LBound(b)+ 1,False,tGuid,PictureFromArray
End如果

Set istrm = Nothing
退出函数
errorhandler:
Debug.Print无法转换为IPicture!
结束功能


As a followup to this question about using images stored in an Excel file on a button in the ribbon:

Is it possible to use an image stored in a CustomXMLPart/CustomXMLNode in base64-encoded string as an image in an Office document without first saving it to disk and loading it back?

The place where I want to use the image takes an IPictureDisp object as a parameter (like the LoadPicture function returns, but that will only load files from disk).

解决方案

First you need to convert your base_64 data to byte array:

Private Function decodeBase64(ByVal strData As String) As Byte()
    Dim objXML As MSXML2.DOMDocument
    Dim objNode As MSXML2.IXMLDOMElement

    Set objXML = New MSXML2.DOMDocument
    Set objNode = objXML.createElement("b64")
    objNode.DataType = "bin.base64"
    objNode.Text = strData
    decodeBase64 = objNode.nodeTypedValue

    Set objNode = Nothing
    Set objXML = Nothing
End Function

from: http://thydzik.com/vb6vba-functions-to-convert-binary-string-to-base64-string/

then you can load it in memory and create your IPictureDisp using information on this topic: http://www.xtremevbtalk.com/showthread.php?t=137857

Type GUID
  Data1 As Long
  Data2 As Integer
  Data3 As Integer
  Data4(7) As Byte
End Type

Private Declare Function CreateStreamOnHGlobal Lib "ole32.dll" (ByRef hGlobal As Any,     ByVal fDeleteOnResume As Long, ByRef ppstr As Any) As Long
Private Declare Function OleLoadPicture Lib "olepro32.dll" (ByVal lpStream As IUnknown, ByVal lSize As Long, ByVal fRunMode As Long, ByRef riid As GUID, ByRef lplpObj As Any) As Long
Private Declare Function CLSIDFromString Lib "ole32.dll" (ByVal lpsz As Long, ByRef pclsid As GUID) As Long

Private Const SIPICTURE As String = "{7BF80980-BF32-101A-8BBB-00AA00300CAB}"

Public Function PictureFromArray(ByRef b() As Byte) As IPicture
  On Error GoTo errorhandler

  Dim istrm As IUnknown
  Dim tGuid As GUID

  If Not CreateStreamOnHGlobal(b(LBound(b)), False, istrm) Then
    CLSIDFromString StrPtr(SIPICTURE), tGuid
    OleLoadPicture istrm, UBound(b) - LBound(b) + 1, False, tGuid, PictureFromArray
  End If

  Set istrm = Nothing
  Exit Function
errorhandler:
  Debug.Print "Could not convert to IPicture!"
End Function

这篇关于使用存储在CustomXMLPart中的base64数据作为Office中的图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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