Excel Vba。从Image控件获取ADODB流(不是使用LoadFromFile)

时间:2016-07-13 07:01:59

标签: excel vba adodb

使用以下功能,我可以从文件加载图片并将其编码为base64:

Public Function EncodeFileBase64() (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
    ' EncodeFileBase64 = objDocElem.Text
    EncodeFileBase64 = Replace(objDocElem.Text, vbLf, "")


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

End Function

由于我想编码之前调整图像大小,我想'逻辑'应该是:

- 1 - 通过使用对话框选择图片来加载图片,调整图片大小并将其显示在image.picture控件中:

Private Sub CommandButtonImage_Click()
    With Application.FileDialog(msoFileDialogFilePicker)
        .AllowMultiSelect = False
        .ButtonName = "Submit"
        .Title = "Selezionare un'immagine"
        .Filters.Add "Image", "*.gif; *.jpg; *.jpeg; *.png", 1
        If .Show = -1 Then
            ' file has been selected

            ' Resize somehow the image

            ' display preview image in an image control
            Me.Image1.PictureSizeMode = fmPictureSizeModeZoom
            Me.Image1.Picture = LoadPicture(.SelectedItems(1))
        Else
            ' something
        End If
    End With
End Sub

- 2 - 通过从图像控件Me.Image1.Picture

加载图像数据来获取图像数据

- 3 - 将加载的数据传递到encode_to_base64 function

问题在于我不知道如何更改上面的ADODB.stream代码,以便它不会从文件中加载图片,但会从Me.Image1.Picture加载图片,然后将其编码为BASE64。应该可以找到类似的here

0 个答案:

没有答案