|
楼主 |
发表于 2021-9-1 23:46
|
显示全部楼层
将一楼的附件中的basSaveRangeToPictrue导入到你自己的文件中,然后再把下面的代码粘贴的至basSaveRangeToPictrue文件尾:
- Public Property Get BASE64PictrueFromClipboard(Optional ByVal ByVal strFileExt As String=".bmp", _
- Optional ByVal AlphaColorOrJPEGQuality As Variant, _
- Optional ByVal AlphaDepthOrColorDepth As Long, _
- Optional ByVal ICO_SizeOrTIFF_COMPRESSION As TIFCOMPRESSION, _
- Optional ByVal NewWidth As Long, _
- Optional ByVal NewHeight As Long, _
- Optional ByVal PictureSizeMode As PictureSizeMode = Zoom) As String
-
- #If VBA7 Then
- Dim hBitmap As LongPtr
- Dim ptrVar As LongPtr
- #Else
- Dim hBitmap As Long
- Dim ptrVar As Long
- #End If
-
- Dim MetaFile As METAFILEPICT
-
- Dim lngRet As Long
- Dim bytFile() As Byte, hFile As Long
- Dim FileName As String
- Dim FileName2 As String
-
- On Error Resume Next
-
- lngRet = InStr(strFileExt, vbNullChar)
- If lngRet > 0 Then strFileExt = Left$(strFileExt, lngRet - 1)
- strFileExt = LCase$(Trim$(strFileExt))
- If Len(strFileExt) < 1 Then
- strFileExt = ".bmp"
- Else
- FileName = strFileExt
- strFileExt = GetFileExtension(FileName)
- If Len(strFileExt) < 1 Then
- strFileExt = GetFileExtension("." & FileName)
- If Len(strFileExt) < 1 Then strFileExt = ".bmp"
- End If
- End If
-
- FileName = String(512, vbNullChar): FileName2 = FileName
- If GetTempPath(512, StrPtr(FileName)) Then
- If GetTempFileName(StrPtr(FileName), StrPtr("Ron"), 0, StrPtr(FileName2)) Then
- KillFile FileName2
- If PathRenameExtensionW(StrPtr(FileName2), StrPtr(strFileExt)) Then
- lngRet = 0
- Do While PathFileExistsW(StrPtr(FileName2))
- lngRet = lngRet + 1
- If lngRet > 100 Then Exit Property
- If GetTempFileName(StrPtr(FileName), StrPtr("Ron"), 0, StrPtr(FileName2)) Then
- KillFile FileName2
- If PathRenameExtensionW(StrPtr(FileName2), StrPtr(strFileExt)) = 0 Then Exit Property
- End If
- Loop
- Else
- Exit Property
- End If
- End If
- End If
-
- lngRet = InStr(FileName2, vbNullChar)
- If lngRet > 0 Then
- FileName = Left$(FileName2, lngRet - 1)
- Else
- FileName = FileName2
- End If
-
- Select Case strFileExt
- Case ".emf"
- 'EMF格式:背景透明,矢量图。
- If Not (KillFile(FileName)) Then Exit Property
- If IsClipboardFormatAvailable(CF_EnhancedMetafile) Then
- If OpenClipboard(0) Then
- hBitmap = GetClipboardData(CF_EnhancedMetafile)
- lngRet = GetEnhMetaFileBits(hBitmap, 0, 0)
- If (lngRet > 0) Then
- ReDim bytFile(0 To lngRet - 1)
- GetEnhMetaFileBits hBitmap, lngRet, varPtr(bytFile(0))
- hFile = FreeFile
- Open FileName For Binary As hFile
- Put hFile, , bytFile
- Close hFile
- If PathFileExistsW(StrPtr(FileName)) Then
- BASE64PictrueFromClipboard = GetFileBase64(FileName)
- KillFile FileName
- End If
- End If
- CloseClipboard
- End If
- End If
- Case ".wmf"
- 'WMF格式:背景透明,矢量图
- If Not (KillFile(FileName)) Then Exit Property
- If IsClipboardFormatAvailable(CF_METAFILEPICT) Then
- If OpenClipboard(0) Then
- hBitmap = GetClipboardData(CF_METAFILEPICT)
- ptrVar = GlobalLock(hBitmap)
- CopyMemory varPtr(MetaFile), ptrVar, LenB(MetaFile)
- lngRet = GetMetaFileBitsEx(MetaFile.hMF, 0, 0)
- If (lngRet > 0) Then
- ReDim bytFile(0 To lngRet - 1)
- GetMetaFileBitsEx MetaFile.hMF, lngRet, varPtr(bytFile(0))
- hFile = FreeFile
- Open FileName For Binary As hFile
- Put hFile, , bytFile
- Close hFile
- If PathFileExistsW(StrPtr(FileName)) Then
- BASE64PictrueFromClipboard = GetFileBase64(FileName)
- KillFile FileName
- End If
- End If
- GlobalUnlock hBitmap
- CloseClipboard
- End If
- End If
- Case Else '由位图转换
- If OpenClipboard(0) Then
- hBitmap = GetClipboardData(CF_Bitmap)
- CloseClipboard
- If SaveHBitmapToFile(hBitmap, FileName, AlphaColorOrJPEGQuality, AlphaDepthOrColorDepth, _
- ICO_SizeOrTIFF_COMPRESSION, NewWidth, NewHeight, PictureSizeMode) Then
- BASE64PictrueFromClipboard = GetFileBase64(FileName)
- KillFile FileName
- End If
- End If
- End Select
- End Property
复制代码
然后,在你的代码中使用
Debug.Print BASE64PictrueFromClipboard
Debug.Print BASE64PictrueFromClipboard(".jpg")
Debug.Print BASE64PictrueFromClipboard("png")
或
Text=BASE64PictrueFromClipboard("gif")
|
|