|
下面这段代码不太精确:- Sub Test()
- With ActiveSheet.ChartObjects.Add(0, 0, 800, 500)
- .Chart.Paste
- .Chart.Export filename:="D:\2.jpg", FilterName:="JPG"
- .Delete
- End With
- End Sub
复制代码 如果要精确的图片,需要用到API:
- '剪贴板函数
- 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 GdipCreateBitmapFromHBITMAP Lib "gdiplus" (ByVal hbm As Long, ByVal hpal As Long, bitmap 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 ClipboardBmpToJPGFile() '剪贴板图片保存JPG文件
- Dim hMem As Long
- Dim bitmap 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(2)
- CloseClipboard
- If hMem = 0 Then MsgBox "未找到截屏数据": Exit Sub
-
- '初始化GDI+
- GpInput.GdiplusVersion = 1
- ReturnValue = GdiplusStartup(GDI_Token, GpInput)
- If ReturnValue <> 0 Then MsgBox "初始化GDI+失败!": Exit Sub
-
- '创建GDI+的bitmap对象
- GdipCreateBitmapFromHBITMAP hMem, 0, bitmap
-
- 'JPG压缩参数设置
- Quality = 50
- With Params
- .count = 1
- With .Parameter
- .Guid = GetEncoderClsid(EncoderQuality)
- .NumberOfValues = 1
- .type = 4
- .value = VarPtr(Quality)
- End With
- End With
- GdipSaveImageToFile bitmap, StrPtr("D:\Test\2.jpg"), GetEncoderClsid(CLSID_JPG), Params
-
- GdipDisposeImage bitmap
- GdiplusShutdown GDI_Token
- End Sub
- Private Function GetEncoderClsid(CLSIDString As String) As Clsid
- CLSIDFromString StrPtr(CLSIDString), GetEncoderClsid
- End Function
复制代码 |
评分
-
5
查看全部评分
-
|