ExcelHome技术论坛

 找回密码
 免费注册

QQ登录

只需一步,快速开始

快捷登录

搜索
EH技术汇-专业的职场技能充电站 妙哉!函数段子手趣味讲函数 Excel服务器-会Excel,做管理系统 效率神器,一键搞定繁琐工作
Python自动化办公应用大全 Excel 2021函数公式学习大典 Kutools for Office 套件发布 打造核心竞争力的职场宝典
让更多数据处理,一键完成 数据工作者的案头书 免费直播课集锦 ExcelHome出品 - VBA代码宝免费下载
用ChatGPT与VBA一键搞定Excel WPS表格从入门到精通 Excel VBA经典代码实践指南
查看: 957|回复: 6

GDI+分解gif动画

[复制链接]

TA的精华主题

TA的得分主题

发表于 2023-11-7 22:09 | 显示全部楼层 |阅读模式
[广告] VBA代码宝 - VBA编程加强工具 · VBA代码随查随用  · 内置多项VBA编程加强工具       ★ 免费下载 ★      ★使用手册
本帖最后由 lss001 于 2023-11-8 08:09 编辑

Private Declare Function GdipImageGetFrameCount Lib "gdiplus" (ByVal image As Long, _
    dimensionID As Guid, Count As Long) As GpStatus
Private Declare Function GdipImageSelectActiveFrame Lib "gdiplus" (ByVal image As Long, _
    dimensionID As Guid, ByVal frameIndex As Long) As GpStatus
Private Declare Function GdiplusStartup Lib "gdiplus" (token As Long, inputbuf _
    As GDIPlusStartupInput, Optional ByVal outputbuf As Long = 0) As GpStatus
Private Declare Function GdipSaveImageToFile Lib "gdiplus" (ByVal image As Long, _
    ByVal FileName As Long, guidEncoder As Guid, encoderParams As Any) As GpStatus
Private Declare Function GdipLoadImageFromFile Lib "gdiplus" (ByVal FileName As String, image As Long) As GpStatus
Private Declare Function GdiplusShutdown Lib "gdiplus" (ByVal token As Long) As GpStatus
Private Declare Function GdipDisposeImage Lib "gdiplus" (ByVal image As Long) As GpStatus
Private Declare Function GdipCreateFromHDC Lib "gdiplus" (ByVal hdc As Long, Graphics As Long) As GpStatus
Private Declare Function GdipDeleteGraphics Lib "gdiplus" (ByVal Graphics As Long) As GpStatus

Private Declare Function CLSIDFromString Lib "ole32" (ByVal Str As Long, id As Guid) As Long
Private Declare Function GetDC Lib "user32" (ByVal hwnd As Long) As Long
Private Declare Function ReleaseDC Lib "user32" (ByVal hwnd As Long, ByVal hdc As Long) As Long

Private Type Guid
    Data1   As Long
    Data2   As Integer
    Data3   As Integer
    Data4(7)   As Byte
End Type
Private Type GDIPlusStartupInput
    GdiPlusVersion As Long
    DebugEventCallback As Long
    SuppressBackgroundThread As Long
    SuppressExternalCodecs As Long
End Type
Private Enum GpStatus ' aka Status
   OK = 0
   GenericError = 1
   InvalidParameter = 2
   OutOfMemory = 3
   ObjectBusy = 4
   InsufficientBuffer = 5
   NotImplemented = 6
   Win32Error = 7
   WrongState = 8
   Aborted = 9
   FileNotFound = 10
   ValueOverflow = 11
   AccessDenied = 12
   UnknownImageFormat = 13
   FontFamilyNotFound = 14
   FontStyleNotFound = 15
   NotTruePrivateTypeFont = 16
   UnsupportedGdiplusVersion = 17
   GdiplusNotInitialized = 18
   PropertyNotFound = 19
   PropertyNotSupported = 20
End Enum

Private Sub GDI分解gif图像()
    Dim hImage&, fCount&, hwnd&, appHDC&, i&, gGuid As Guid, bGuid As Guid
    Dim uInput As GDIPlusStartupInput, Graphics&, token&
    Const PropertyTagFrameDelay = &H5100

    uInput.GdiPlusVersion = 1
    GdiplusStartup token, uInput '开始/初始化
    hwnd = Application.hwnd
    appHDC = GetDC(hwnd) '获取DC//句柄

    GdipCreateFromHDC appHDC, Graphics
    '根据实际修改以下文件路径/文件名
    GdipLoadImageFromFile VBA.StrConv(ThisWorkbook.Path & "\xxx.gif", 64), hImage
    CLSIDFromString StrPtr("{6AEDBD6D-3FB5-418A-83A6-7F45229DC872}"), gGuid 'gif
    CLSIDFromString StrPtr("{557CF400-1A04-11D3-9A73-0000F81EF32E}"), bGuid 'bmp
    GdipImageGetFrameCount hImage, gGuid, fCount '帧数
    If fCount > 0 Then
        For i = 1 To fCount
            GdipImageSelectActiveFrame hImage, gGuid, i '选择当前帧
            GdipSaveImageToFile hImage, StrPtr("d:\" & i & ".bmp"), bGuid, ByVal 0& '保存
        Next
    End If

    GdipDisposeImage hImage
    GdipDeleteGraphics Graphics
    GdiplusShutdown token '结束/释放gdi+
    ReleaseDC hwnd, appHDC '释放appHdc
End Sub

评分

1

查看全部评分

TA的精华主题

TA的得分主题

发表于 2023-11-14 19:57 | 显示全部楼层
求64位可用版本,谢谢大神

TA的精华主题

TA的得分主题

发表于 2023-11-14 21:38 | 显示全部楼层

TA的精华主题

TA的得分主题

发表于 2023-11-15 09:03 | 显示全部楼层
morpheus126 发表于 2023-11-14 19:57
求64位可用版本,谢谢大神

改一下函数和变量声明就行了,直接改成64位的了,没有兼容32位
  1. Private Declare PtrSafe Function GdipImageGetFrameCount Lib "gdiplus" (ByVal image As LongPtr, _
  2.     dimensionID As Guid, Count As Long) As GpStatus
  3. Private Declare PtrSafe Function GdipImageSelectActiveFrame Lib "gdiplus" (ByVal image As LongPtr, _
  4.     dimensionID As Guid, ByVal frameIndex As Long) As GpStatus
  5. Private Declare PtrSafe Function GdiplusStartup Lib "gdiplus" (token As LongPtr, inputbuf _
  6.     As GDIPlusStartupInput, Optional ByVal outputbuf As LongPtr = 0) As GpStatus
  7. Private Declare PtrSafe Function GdipSaveImageToFile Lib "gdiplus" (ByVal image As LongPtr, _
  8.     ByVal FileName As LongPtr, guidEncoder As Guid, encoderParams As Any) As GpStatus
  9. Private Declare PtrSafe Function GdipLoadImageFromFile Lib "gdiplus" (ByVal FileName As String, image As LongPtr) As GpStatus
  10. Private Declare PtrSafe Function GdiplusShutdown Lib "gdiplus" (ByVal token As LongPtr) As GpStatus
  11. Private Declare PtrSafe Function GdipDisposeImage Lib "gdiplus" (ByVal image As LongPtr) As GpStatus
  12. Private Declare PtrSafe Function GdipCreateFromHDC Lib "gdiplus" (ByVal hdc As LongPtr, Graphics As LongPtr) As GpStatus
  13. Private Declare PtrSafe Function GdipDeleteGraphics Lib "gdiplus" (ByVal Graphics As LongPtr) As GpStatus

  14. Private Declare PtrSafe Function CLSIDFromString Lib "ole32" (ByVal Str As LongPtr, id As Guid) As Long
  15. Private Declare PtrSafe Function GetDC Lib "user32" (ByVal hwnd As LongPtr) As Long
  16. Private Declare PtrSafe Function ReleaseDC Lib "user32" (ByVal hwnd As LongPtr, ByVal hdc As LongPtr) As Long

  17. Private Type Guid
  18.     Data1   As Long
  19.     Data2   As Integer
  20.     Data3   As Integer
  21.     Data4(7)   As Byte
  22. End Type
  23. Private Type GDIPlusStartupInput
  24.     GdiPlusVersion As Long
  25.     DebugEventCallback As LongPtr
  26.     SuppressBackgroundThread As Long
  27.     SuppressExternalCodecs As Long
  28. End Type
  29. Private Enum GpStatus ' aka Status
  30.    OK = 0
  31.    GenericError = 1
  32.    InvalidParameter = 2
  33.    OutOfMemory = 3
  34.    ObjectBusy = 4
  35.    InsufficientBuffer = 5
  36.    NotImplemented = 6
  37.    Win32Error = 7
  38.    WrongState = 8
  39.    Aborted = 9
  40.    FileNotFound = 10
  41.    ValueOverflow = 11
  42.    AccessDenied = 12
  43.    UnknownImageFormat = 13
  44.    FontFamilyNotFound = 14
  45.    FontStyleNotFound = 15
  46.    NotTruePrivateTypeFont = 16
  47.    UnsupportedGdiplusVersion = 17
  48.    GdiplusNotInitialized = 18
  49.    PropertyNotFound = 19
  50.    PropertyNotSupported = 20
  51. End Enum

  52. Private Sub GDI分解gif图像()
  53.     Dim hImage As LongPtr, fCount&, hwnd As LongPtr, appHDC As LongPtr, i&, gGuid As Guid, bGuid As Guid
  54.     Dim uInput As GDIPlusStartupInput, Graphics As LongPtr, token As LongPtr
  55.     Const PropertyTagFrameDelay = &H5100

  56.     uInput.GdiPlusVersion = 1
  57.     GdiplusStartup token, uInput  '开始/初始化
  58.     hwnd = Application.hwnd
  59.     appHDC = GetDC(hwnd) '获取DC//句柄

  60.     GdipCreateFromHDC appHDC, Graphics
  61.     '根据实际修改以下文件路径/文件名
  62.     GdipLoadImageFromFile VBA.StrConv("d:\edge.gif", 64), hImage
  63.     CLSIDFromString StrPtr("{6AEDBD6D-3FB5-418A-83A6-7F45229DC872}"), gGuid 'gif
  64.     CLSIDFromString StrPtr("{557CF400-1A04-11D3-9A73-0000F81EF32E}"), bGuid 'bmp
  65.     GdipImageGetFrameCount hImage, gGuid, fCount '帧数
  66.     If fCount > 0 Then
  67.         For i = 1 To fCount
  68.             GdipImageSelectActiveFrame hImage, gGuid, i '选择当前帧
  69.             GdipSaveImageToFile hImage, StrPtr("d:" & i & ".bmp"), bGuid, ByVal 0&  '保存
  70.         Next
  71.     End If

  72.     GdipDisposeImage hImage
  73.     GdipDeleteGraphics Graphics
  74.     GdiplusShutdown token '结束/释放gdi+
  75.     ReleaseDC hwnd, appHDC '释放appHdc
  76. End Sub


复制代码

评分

1

查看全部评分

TA的精华主题

TA的得分主题

发表于 2023-11-16 15:54 | 显示全部楼层
小fisher 发表于 2023-11-15 09:03
改一下函数和变量声明就行了,直接改成64位的了,没有兼容32位

谢谢大佬。。。我试过自己改,还是没改彻底,主要是数据类型部分失踪不得敲门

TA的精华主题

TA的得分主题

发表于 2023-11-16 15:55 | 显示全部楼层
perfect131 发表于 2023-11-14 21:38
https://club.excelhome.net/thread-1676897-1-1.html

谢谢大佬,您的我也抄了

TA的精华主题

TA的得分主题

发表于 2023-12-23 12:06 | 显示全部楼层
两年前,我也曾尝试过,合成,分解,播放gif,感谢分享。在此能遇到小fisher,能学习到很多。
您需要登录后才可以回帖 登录 | 免费注册

本版积分规则

手机版|关于我们|联系我们|ExcelHome

GMT+8, 2025-12-8 08:43 , Processed in 0.037098 second(s), 13 queries , Gzip On, MemCache On.

Powered by Discuz! X3.4

© 1999-2023 Wooffice Inc.

沪公网安备 31011702000001号 沪ICP备11019229号-2

本论坛言论纯属发表者个人意见,任何违反国家相关法律的言论,本站将协助国家相关部门追究发言者责任!     本站特聘法律顾问:李志群律师

快速回复 返回顶部 返回列表