Access数据库培训
网站公告
·Access快速平台QQ群号:277422564    ·Access快速开发平台下载地址及教程    ·欢迎添加微信交流账号:Accessoft7    ·如何快速搜索本站文章|示例|资料!    
您的位置: 首页 > 技术文章 > 源码示例

截屏并压缩保存为jpg图片

时 间:2019-01-03 11:18:35
作 者:易勋   ID:35404  城市:上海
摘 要:通过模拟按键截屏,然后压缩保存为jpg图片
正 文:

函数:

Option Compare Database
Option Explicit

Public Declare Sub Sleep Lib "Kernel32" (ByVal dwmilliseconds As Long)

Private Declare Sub keybd_event Lib "user32" (ByVal bVk As Byte, ByVal bScan As Byte, ByVal dwFlags As Long, ByVal dwExtraInfo As Long)
 
Private Const KEYEVENTF_KEYUP = &H2
Private Const VK_SNAPSHOT = &H2C
Private Const VK_MENU = &H12

'剪贴板函数
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

Public Function ScreenSaveAs(FilePath As String) As Boolean '剪贴板图片保存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
     
    ScreenSaveAs = False
    GetScreen
    DoEvents
    Sleep 100
    '获取剪贴板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 Function
    
    '创建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(CurrentProject.Path & "\001.jpg"), GetEncoderClsid(CLSID_JPG), Params
    
    GdipDisposeImage bitmap
    GdiplusShutdown GDI_Token
    ScreenSaveAs = True
End Function

Private Function GetScreen()
    keybd_event VK_MENU, 0, 0, 0
    keybd_event VK_SNAPSHOT, 0, 0, 0
    keybd_event VK_SNAPSHOT, 0, KEYEVENTF_KEYUP, 0
    keybd_event VK_MENU, 0, KEYEVENTF_KEYUP, 0
End Function

Private Function GetEncoderClsid(CLSIDString As String) As Clsid
    CLSIDFromString StrPtr(CLSIDString), GetEncoderClsid
End Function

调用方法:

ScreenSaveAs "路径并/文件名.jpg"



Access软件网官方交流QQ群 (群号:115120150)       access源码网店

最新评论 查看更多评论(33)

2019/1/27 23:53:24崔宇
保存的文件是0字节JPG,是个什么情况

发表评论您的评论将提升作者分享的动力!快来评论一下吧!

用户名:
密 码:
内 容:
 

常见问答

技术分类

相关资源

最新帖子

关于我们 | 服务条款 | 在线投稿 | 友情链接 | 网站统计 | 网站帮助