|
楼主 |
发表于 2024-9-9 00:04
|
显示全部楼层
感谢大佬
Option Explicit
Private Declare PtrSafe Function OpenClipboard _
Lib "user32" (ByVal hwnd As Long) 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 Long, _
inputbuf As GdiplusStartupInput, _
ByVal outputbuf As Long) As Long
Private Declare PtrSafe Function GdiplusShutdown _
Lib "GDIPlus" (ByVal token As Long) As Long
Private Declare PtrSafe Function GdipDisposeImage _
Lib "GDIPlus" (ByVal Image As LongPtr) As Long
'第二参数:Longlong
Private Declare PtrSafe Function GdipSaveImageToFile _
Lib "GDIPlus" (ByVal Image As LongPtr, _
ByVal filename As LongLong, _
clsidEncoder As GUID, _
encoderParams As Any) As Long
Private Declare PtrSafe Function CLSIDFromString _
Lib "ole32" (ByVal str As LongPtr, _
id As GUID) As Long
Private Declare PtrSafe Function GdipCreateBitmapFromHBITMAP _
Lib "GDIPlus" (ByVal hbm As Long, _
ByVal hPal As Long, _
BITMAP As LongPtr) As Long
Private Const CF_BITMAP = 2 '对应(.bmp, .jpg, .gif)
Private Const CF_ENHMETAFILE = 14 '对应(.png, .wmf, .mix)
Private Type GUID
Data1 As Long
Data2 As Integer
Data3 As Integer
Data4(0 To 7) As Byte
End Type
'以下参数:LongPtr
Private Type GdiplusStartupInput
GdiplusVersion As LongPtr
DebugEventCallback As LongPtr
SuppressBackgroundThread As LongPtr
SuppressExternalCodecs As LongPtr
End Type
Private Type EncoderParameter
GUID As GUID
NumberOfValues As Long
type As Long
Value As Long
End Type
Private Type EncoderParameters
Count As Long
Parameter As EncoderParameter
End Type
Public Function ClipBitMaptoJPG(文件名$)
Dim destfilename As String
destfilename = Replace(文件名 & ".jpg", ".jpg.jpg", ".jpg") '指定保存文件
Const quality As Long = 80
Dim tSI As GdiplusStartupInput
Dim lRes As Long, tGDIP As Long
Dim lBitmap As LongPtr, hBmp As Long
'尝试打开剪贴板
If OpenClipboard(0) Then
hBmp = GetClipboardData(CF_BITMAP)
If hBmp = 0 Then
ClipBitMaptoJPG = "剪贴板无数据": GoTo t
End If
End If
tSI.GdiplusVersion = 1
lRes = GdiplusStartup(tGDIP, tSI, 0)
If lRes = 0 Then
lRes = GdipCreateBitmapFromHBITMAP(hBmp, 0, lBitmap)
If lRes = 0 Then
Dim tJpgEncoder As GUID
Dim tParams As EncoderParameters
CLSIDFromString StrPtr( _
"{557CF401-1A04-11D3-9A73-0000F81EF32E}"), tJpgEncoder
tParams.Count = 1
With tParams.Parameter
CLSIDFromString StrPtr( _
"{1D5BE4B5-FA4A-452D-9CDD-5DB35105E7EB}"), .GUID
.NumberOfValues = 1
.type = 4
.Value = quality
End With
'保存图像
lRes = GdipSaveImageToFile( _
lBitmap, StrPtr(destfilename), tJpgEncoder, tParams)
GdipDisposeImage lBitmap
ClipBitMaptoJPG = "图片保存" & destfilename
End If
'销毁 GDI+
GdiplusShutdown tGDIP
End If
t:
CloseClipboard
End Function
我改成函数了 |
|