|
[广告] VBA代码宝 - VBA编程加强工具 · VBA代码随查随用 · 内置多项VBA编程加强工具 ★ 免费下载 ★ ★ 使用手册★
本帖最后由 wcymiss 于 2015-4-6 19:22 编辑
yf_home 发表于 2015-4-3 14:21
刚刚试了一下,Excel本身自带的粘贴为图片,不会有缺少边框的效果。
嗯,发现excel自带粘贴的是metafile格式的图片。不过这个效果会在右边和下边多出空白边。。
将3楼代码修改如下(红色部分是修改的地方):
'剪贴板函数
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 Format As Long) As Long
'OLE函数
Private Type Clsid
Data1 As Long
Data2 As Integer
Data3 As Integer
Data4(0 To 7) As Byte
End Type
Private Declare Function CLSIDFromString Lib "ole32" (ByVal lpsz As Long, pclsid As Clsid) As Long
'GDI函数
Private Type GdiplusStartupInput
GdiplusVersion As Long
DebugEventCallback As Long
SuppressBackgroundThread As Long
SuppressExternalCodecs As Long
End Type
Private Type EncoderParameter
Guid As Clsid
NumberOfValues As Long
type As Long
value As Long
End Type
Private Type EncoderParameters
count As Long
Parameter As EncoderParameter
End Type
Private Const CLSID_JPG As String = "{557CF401-1A04-11D3-9A73-0000F81EF32E}"
Private Const EncoderQuality As String = "{1D5BE4B5-FA4A-452D-9CDD-5DB35105E7EB}"
Private Declare Function GdiplusStartup Lib "gdiplus" (token As Long, inputbuf As GdiplusStartupInput, Optional ByVal outputbuf As Long = 0) As Long
Private Declare Function GdiplusShutdown Lib "gdiplus" (ByVal token As Long) As Long
Private Declare Function GdipCreateMetafileFromEmf Lib "gdiplus" (ByVal hEmf As Long, ByVal deleteEmf As Boolean, metafile 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 Clsid, encoderParams As Any) As Long
Sub ClipboardMetafileToJPGFile() '剪贴板中metafile格式图片保存为JPG文件
Dim hMem As Long
Dim metafile As Long
Dim GDI_Token As Long
Dim GpInput As GdiplusStartupInput
Dim ReturnValue As Long
Dim Params As EncoderParameters
Dim Quality As Long
'获取剪贴板BMP数据的Handle
OpenClipboard 0&
hMem = GetClipboardData(14) '获取hemf
CloseClipboard
If hMem = 0 Then MsgBox "未找到剪贴板中metafile格式数据": Exit Sub
'初始化GDI+
GpInput.GdiplusVersion = 1
ReturnValue = GdiplusStartup(GDI_Token, GpInput)
If ReturnValue <> 0 Then MsgBox "初始化GDI+失败!": Exit Sub
'创建GDI+的metafile对象
GdipCreateMetafileFromEmf hMem, False, metafile
'JPG压缩参数设置
Quality = 50
With Params
.count = 1
With .Parameter
.Guid = GetEncoderClsid(EncoderQuality)
.NumberOfValues = 1
.type = 4
.value = VarPtr(Quality)
End With
End With
GdipSaveImageToFile metafile, StrPtr("D:\Test\2.jpg"), GetEncoderClsid(CLSID_JPG), Params
GdipDisposeImage metafile
GdiplusShutdown GDI_Token
End Sub
Private Function GetEncoderClsid(CLSIDString As String) As Clsid
CLSIDFromString StrPtr(CLSIDString), GetEncoderClsid
End Function
|
评分
-
2
查看全部评分
-
|