|
先将剪切板内的图片 导出为图片文件
再将文件读取到image
下面是将剪切板图片 保存为文件的函数
- '''Sub test()
- ''' Select Case ClipboardToJPG("c:\test.jpg")
- ''' Case 0:
- ''' MsgBox "剪贴板图片已保存"
- ''' Case 1:
- ''' MsgBox "剪贴板图片保存失败"
- ''' Case 2:
- ''' MsgBox "剪贴板中无图片"
- ''' Case 3:
- ''' MsgBox "剪贴板无法打开,可能被其他程序所占用"
- ''' Case 4:
- ''' MsgBox "GDI+错误"
- ''' End Select
- '''End Sub
- Option Explicit
- Private Type GUID
- Data1 As Long
- Data2 As Integer
- Data3 As Integer
- Data4(0 To 7) As Byte
- End Type
- #If VBA7 And Win64 Then
- Private Declare PtrSafe Function OpenClipboard Lib "user32" (ByVal hwnd As LongPtr) As Long
- Private Declare PtrSafe Function CloseClipboard Lib "user32" () As Long
- Private Declare PtrSafe Function GetClipboardData Lib "user32" (ByVal wFormat As Long) As Long
- Private Declare PtrSafe Function GdiplusStartup Lib "GDIPlus" (token As LongPtr, inputbuf As GdiplusStartupInput, ByVal outputbuf As LongPtr) As LongPtr
- Private Declare PtrSafe Function GdiplusShutdown Lib "GDIPlus" (ByVal token As LongPtr) As LongPtr
- Private Declare PtrSafe Function GdipDisposeImage Lib "GDIPlus" (ByVal Image As LongPtr) As LongPtr
- Private Declare PtrSafe Function GdipSaveImageToFile Lib "GDIPlus" (ByVal Image As LongPtr, ByVal filename As LongPtr, clsidEncoder As GUID, encoderParams As Any) As LongPtr
- Private Declare PtrSafe Function CLSIDFromString Lib "ole32" (ByVal str As LongPtr, id As GUID) As LongPtr
- Private Declare PtrSafe Function GdipCreateBitmapFromHBITMAP Lib "GDIPlus" (ByVal hbm As LongPtr, ByVal hPal As LongPtr, BITMAP As LongPtr) As LongPtr
- Private Type EncoderParameter
- GUID As GUID
- NumberOfValues As Long
- type As Long
- Value As LongPtr
- End Type
- Private Type GdiplusStartupInput
- GdiplusVersion As Long
- DebugEventCallback As LongPtr
- SuppressBackgroundThread As Long
- SuppressExternalCodecs As Long
- End Type
- #Else
- Private Declare Function OpenClipboard Lib "user32" (ByVal hwnd As Long) As Long
- Private Declare Function CloseClipboard Lib "user32" () As Long
- Private Declare Function GetClipboardData Lib "user32" (ByVal wFormat As Long) As Long
- Private Declare Function GdiplusStartup Lib "GDIPlus" (token As Long, inputbuf As GdiplusStartupInput, ByVal outputbuf As Long) As Long
- Private Declare Function GdiplusShutdown Lib "GDIPlus" (ByVal token As Long) As Long
- Private Declare Function GdipDisposeImage Lib "GDIPlus" (ByVal Image As Long) As Long
- Private Declare Function GdipSaveImageToFile Lib "GDIPlus" (ByVal Image As Long, ByVal filename As Long, clsidEncoder As GUID, encoderParams As Any) As Long
- Private Declare Function CLSIDFromString Lib "ole32" (ByVal str As Long, id As GUID) As Long
- Private Declare Function GdipCreateBitmapFromHBITMAP Lib "GDIPlus" (ByVal hbm As Long, ByVal hPal As Long, BITMAP As Long) As Long
- Private Type EncoderParameter
- GUID As GUID
- NumberOfValues As Long
- type As Long
- Value As Long
- End Type
- Private Type GdiplusStartupInput
- GdiplusVersion As Long
- DebugEventCallback As Long
- SuppressBackgroundThread As Long
- SuppressExternalCodecs As Long
- End Type
- #End If
- Private Const CF_BITMAP = 2
- Private Type EncoderParameters
- Count As Long
- Parameter As EncoderParameter
- End Type
- Public Function ClipboardToJPG(ByVal destfilename As String, Optional ByVal quality As Byte = 100) As Integer
- Rem 该函数用于取出剪贴板中图片转换为jpg文件另存到指定路径****
- Rem 参数说明:
- Rem destfilename:要保存的jpg文件的完整路径,必要参数;
- Rem quality: jpg文件的质量,0-100之间的数值,数值越大,图片质量越高
- Rem 返回值:
- Rem 0-保存成功;1-保存失败;2-剪贴板中无位图数据;3-无法打开剪贴板
- Dim tSI As GdiplusStartupInput
-
- #If VBA7 Then
- Dim lRes As LongPtr
- Dim lGDIP As LongPtr
- Dim lBitmap As LongPtr
- Dim hBmp As LongPtr
- #Else
- Dim lRes As Long
- Dim lGDIP As Long
- Dim lBitmap As Long
- Dim hBmp As Long
- #End If
-
- Rem 尝试打开剪贴板
- If OpenClipboard(0) Then
- Rem 尝试取出剪贴板中位图的句柄
- hBmp = GetClipboardData(CF_BITMAP)
- Rem 如果hBmp为0,说明剪贴板中没有存放图片
- If hBmp = 0 Then
- ClipboardToJPG = 2
- CloseClipboard
- Exit Function
- End If
- CloseClipboard
- Else '如果openclipboard返回0(False),说明剪贴板被其他程序所占用
- ClipboardToJPG = 3
- Exit Function
- End If
- Rem 初始化 GDI+
- tSI.GdiplusVersion = 1
- lRes = GdiplusStartup(lGDIP, tSI, 0)
- If lRes = 0 Then
- Rem 从句柄创建 GDI+ 图像
- lRes = GdipCreateBitmapFromHBITMAP(hBmp, 0, lBitmap)
- If lRes = 0 Then
- Dim tJpgEncoder As GUID
- Dim tParams As EncoderParameters
- Rem 初始化解码器的GUID标识
- CLSIDFromString StrPtr("{557CF401-1A04-11D3-9A73-0000F81EF32E}"), tJpgEncoder
- Rem 设置解码器参数
- tParams.Count = 1
- With tParams.Parameter ' Quality
- Rem 得到Quality参数的GUID标识
- CLSIDFromString StrPtr("{1D5BE4B5-FA4A-452D-9CDD-5DB35105E7EB}"), .GUID
- .NumberOfValues = 1
- .type = 4
- .Value = VarPtr(quality)
- End With
- Rem 保存图像
- lRes = GdipSaveImageToFile(lBitmap, StrPtr(destfilename), tJpgEncoder, tParams)
- If lRes = 0 Then
- ClipboardToJPG = 0 '转换成功
- Else
- ClipboardToJPG = 1 '转换失败
- End If
- Rem 销毁GDI+图像
- GdipDisposeImage lBitmap
- End If
- Rem 销毁 GDI+
- GdiplusShutdown lGDIP
- Else
- ClipboardToJPG = 4
- End If
- End Function
复制代码
|
|