VBAからGDI+を使う資料集
inet上の達人の方々から御教示いただいたコードを少しアレンジさせてもらっています(切り貼りとも言います)。
  1. ホーム
  2. EMF
  3. emfを読む1


emfを読む習作、recordのリスト取得

emfのrecordの内容のリストを出力する習作です。
従来漠然と、ベクトルグラフィックのファイルの一種と思っていましたが、デバイスコンテキストに対するグラフィック操作を記述したファイルだという事が分かりました。
デバイスコンテキストについての認識も若干進んだ気がします。


ワークシートに画像だけを貼り付けたものから取得したEMFの内容は下記の通りでした。
EMR_STRETCHDIBITSの中に、画像が入っているようです。

EMR_HEADER
EMR_SETTEXTCOLOR
EMR_SETBKCOLOR
EMR_SETROP
EMR_SETBKMODE
EMR_SAVEDC
EMR_SETWINDOWORGEX
EMR_SETWINDOWEXTEX
EMR_SETROP
EMR_SELECTPALETTE
EMR_GDICOMMENT
EMR_SETSTRETCHBLTMODE
EMR_STRETCHDIBITS
EMR_SETSTRETCHBLTMODE
EMR_GDICOMMENT
EMR_SETVIEWPORTORGEX
EMR_SETTEXTCOLOR
EMR_SETBKCOLOR
EMR_SELECTOBJECT
EMR_RESTOREDC
EMR_EOF


'EMFのレコードの列挙
Private Sub testEnumEmf()
    Dim hemf As Long '拡張メタファイルのハンドル
    Dim r As RECT '描画する領域
    Dim hdc As Long
    
    '拡張メタファイルのオープン
'    hEmf = GetEnhMetaFile("c:\pic\test.emf")
      Selection.Copy
    If OpenClipboard(0) Then
      hemf = GetClipboardData(CF_ENHMETAFILE)
      ' ハンドルを複製してから使用する
      hemf = CopyEnhMetaFile(hemf, vbNullString)
      CloseClipboard
    End If
    If hemf = 0 Then
      MsgBox "emf取得に失敗"
      Exit Sub  ' 失敗
    End If

    '描画領域の設定 'ダミー
    r.Left = 0
    r.Top = 0
    r.Right = 100
    r.Bottom = 100
    
    hdc = GetDC(0)
    'レコードの列挙
    EnumEnhMetaFile hdc, hemf, AddressOf EnumFunc, 0, r
    '拡張メタファイルのクローズ
    DeleteEnhMetaFile hemf
    Call ReleaseDC(0, hdc)
End Sub

' メタファイルレコード列挙コールバック
Public Function EnumFunc( _
  ByVal hdc As Long, _
  ByVal pHandles As Long, _
  ByVal pRecord As Long, _
  ByVal HandleNum As Long, _
  ByVal pData As Long) As Long

  Dim eh As ENHMETARECORD 'EMR 'レコードのヘッダ
  
  'レコードのヘッダをehに格納
  RtlMoveMemory VarPtr(eh), pRecord, Len(eh)
  Debug.Print getEMRtype(eh.iType)
  EnumFunc = 1
End Function

Private Function getEMRtype(iType As Long) As String
  Select Case iType
    Case 1
      getEMRtype = "EMR_HEADER"
    Case 2
      getEMRtype = "EMR_POLYBEZIER"
    Case 3
      getEMRtype = "EMR_POLYGON"
    Case 4
      getEMRtype = "EMR_POLYLINE"
    Case 5
      getEMRtype = "EMR_POLYBEZIERTO"
    Case 6
      getEMRtype = "EMR_POLYLINETO"
    Case 7
      getEMRtype = "EMR_POLYPOLYLINE"
    Case 8
      getEMRtype = "EMR_POLYPOLYGON"
    Case 9
      getEMRtype = "EMR_SETWINDOWEXTEX"
    Case 10
      getEMRtype = "EMR_SETWINDOWORGEX"
    Case 11
      getEMRtype = "EMR_SETVIEWPORTEXTEX"
    Case 12
      getEMRtype = "EMR_SETVIEWPORTORGEX"
    Case 13
      getEMRtype = "EMR_SETBRUSHORGEX"
    Case 14
      getEMRtype = "EMR_EOF"
    Case 15
      getEMRtype = "EMR_SETPIXELV"
    Case 16
      getEMRtype = "EMR_SETMAPPERFLAGS"
    Case 17
      getEMRtype = "EMR_SETMAPMODE"
    Case 18
      getEMRtype = "EMR_SETBKMODE"
    Case 19
      getEMRtype = "EMR_SETPOLYFILLMODE"
    Case 20
      getEMRtype = "EMR_SETROP"
    Case 21
      getEMRtype = "EMR_SETSTRETCHBLTMODE"
    Case 22
      getEMRtype = "EMR_SETTEXTALIGN"
    Case 23
      getEMRtype = "EMR_SETCOLORADJUSTMENT"
    Case 24
      getEMRtype = "EMR_SETTEXTCOLOR"
    Case 25
      getEMRtype = "EMR_SETBKCOLOR"
    Case 26
      getEMRtype = "EMR_OFFSETCLIPRGN"
    Case 27
      getEMRtype = "EMR_MOVETOEX"
    Case 28
      getEMRtype = "EMR_SETMETARGN"
    Case 29
      getEMRtype = "EMR_EXCLUDECLIPRECT"
    Case 30
      getEMRtype = "EMR_INTERSECTCLIPRECT"
    Case 31
      getEMRtype = "EMR_SCALEVIEWPORTEXTEX"
    Case 32
      getEMRtype = "EMR_SCALEWINDOWEXTEX"
    Case 33
      getEMRtype = "EMR_SAVEDC"
    Case 34
      getEMRtype = "EMR_RESTOREDC"
    Case 35
      getEMRtype = "EMR_SETWORLDTRANSFORM"
    Case 36
      getEMRtype = "EMR_MODIFYWORLDTRANSFORM"
    Case 37
      getEMRtype = "EMR_SELECTOBJECT"
    Case 38
      getEMRtype = "EMR_CREATEPEN"
    Case 39
      getEMRtype = "EMR_CREATEBRUSHINDIRECT"
    Case 40
      getEMRtype = "EMR_DELETEOBJECT"
    Case 41
      getEMRtype = "EMR_ANGLEARC"
    Case 42
      getEMRtype = "EMR_ELLIPSE"
    Case 43
      getEMRtype = "EMR_RECTANGLE"
    Case 44
      getEMRtype = "EMR_ROUNDRECT"
    Case 45
      getEMRtype = "EMR_ARC"
    Case 46
      getEMRtype = "EMR_CHORD"
    Case 47
      getEMRtype = "EMR_PIE"
    Case 48
      getEMRtype = "EMR_SELECTPALETTE"
    Case 49
      getEMRtype = "EMR_CREATEPALETTE"
    Case 50
      getEMRtype = "EMR_SETPALETTEENTRIES"
    Case 51
      getEMRtype = "EMR_RESIZEPALETTE"
    Case 52
      getEMRtype = "EMR_REALIZEPALETTE"
    Case 53
      getEMRtype = "EMR_EXTFLOODFILL"
    Case 54
      getEMRtype = "EMR_LINETO"
    Case 55
      getEMRtype = "EMR_ARCTO"
    Case 56
      getEMRtype = "EMR_POLYDRAW"
    Case 57
      getEMRtype = "EMR_SETARCDIRECTION"
    Case 58
      getEMRtype = "EMR_SETMITERLIMIT"
    Case 59
      getEMRtype = "EMR_BEGINPATH"
    Case 60
      getEMRtype = "EMR_ENDPATH"
    Case 61
      getEMRtype = "EMR_CLOSEFIGURE"
    Case 62
      getEMRtype = "EMR_FILLPATH"
    Case 63
      getEMRtype = "EMR_STROKEANDFILLPATH"
    Case 64
      getEMRtype = "EMR_STROKEPATH"
    Case 65
      getEMRtype = "EMR_FLATTENPATH"
    Case 66
      getEMRtype = "EMR_WIDENPATH"
    Case 67
      getEMRtype = "EMR_SELECTCLIPPATH"
    Case 68
      getEMRtype = "EMR_ABORTPATH = 68"
    Case 70
      getEMRtype = "EMR_GDICOMMENT"
    Case 71
      getEMRtype = "EMR_FILLRGN"
    Case 72
      getEMRtype = "EMR_FRAMERGN"
    Case 73
      getEMRtype = "EMR_INVERTRGN"
    Case 74
      getEMRtype = "EMR_PAINTRGN"
    Case 75
      getEMRtype = "EMR_EXTSELECTCLIPRGN"
    Case 76
      getEMRtype = "EMR_BITBLT"
    Case 77
      getEMRtype = "EMR_STRETCHBLT"
    Case 78
      getEMRtype = "EMR_MASKBLT"
    Case 79
      getEMRtype = "EMR_PLGBLT"
    Case 80
      getEMRtype = "EMR_SETDIBITSTODEVICE"
    Case 81
      getEMRtype = "EMR_STRETCHDIBITS"
    Case 82
      getEMRtype = "EMR_EXTCREATEFONTINDIRECTW"
    Case 83
      getEMRtype = "EMR_EXTTEXTOUTA"
    Case 84
      getEMRtype = "EMR_EXTTEXTOUTW"
    Case 85
      getEMRtype = "EMR_POLYBEZIER16"
    Case 86
      getEMRtype = "EMR_POLYGON16"
    Case 87
      getEMRtype = "EMR_POLYLINE16"
    Case 88
      getEMRtype = "EMR_POLYBEZIERTO16"
    Case 89
      getEMRtype = "EMR_POLYLINETO16"
    Case 90
      getEMRtype = "EMR_POLYPOLYLINE16"
    Case 91
      getEMRtype = "EMR_POLYPOLYGON16"
    Case 92
      getEMRtype = "EMR_POLYDRAW16"
    Case 93
      getEMRtype = "EMR_CREATEMONOBRUSH"
    Case 94
      getEMRtype = "EMR_CREATEDIBPATTERNBRUSHPT"
    Case 95
      getEMRtype = "EMR_EXTCREATEPEN"
    Case 96
      getEMRtype = "EMR_POLYTEXTOUTA"
    Case 97
      getEMRtype = "EMR_POLYTEXTOUTW"
    Case Else
      getEMRtype = "unKnown"
  End Select
End Function