1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152
|
Option Compare Database
Option Explicit
'***************************************************************************************
'* API *
'***************************************************************************************
Private Declare Function SetStretchBltMode Lib "GDI32" (ByVal hDC As Long, ByVal nStretchMode As Long) As Long
Private Declare Function StretchBlt Lib "GDI32" _
(ByVal hDC As Long, ByVal X As Long, ByVal Y As Long, ByVal nWidth As Long, _
ByVal nHeight As Long, ByVal hSrcDC As Long, ByVal xSrc As Long, _
ByVal ySrc As Long, ByVal nSrcWidth As Long, _
ByVal nSrcHeight As Long, ByVal dwRop As Long) As Long
Private Declare Function BitBlt Lib "GDI32" _
(ByVal destdc As Long, ByVal X As Long, ByVal Y As Long, ByVal nWidth As Long, _
ByVal nHeight As Long, ByVal srcdc As Long, ByVal xSrc As Long, ByVal ySrc As Long, _
ByVal dwRop As Long) As Long
Private Declare Function GetMapMode Lib "GDI32" (ByVal hDC As Long) As Long
Private Declare Function SetMapMode Lib "GDI32" (ByVal hDC As Long, ByVal nMapMode As Long) As Long
Private Declare Function GetDeviceCaps Lib "GDI32" (ByVal hDC As Long, ByVal nIndex As Long) As Long
Private Declare Function apiGetObject Lib "GDI32" Alias "GetObjectA" (ByVal hObject As Long, ByVal nCount As Long, lpObject As Any) As Long
Private Declare Function SelectObject Lib "GDI32" (ByVal hDC As Long, ByVal hObject As Long) As Long
Private Declare Function DeleteObject Lib "GDI32" (ByVal hObject As Long) As Long
Private Declare Function GetDC Lib "user32" (ByVal hwnd As Long) As Long
Private Declare Function CreateCompatibleDC Lib "GDI32" (ByVal hDC As Long) As Long
Private Declare Function ReleaseDC Lib "user32" (ByVal hwnd As Long, ByVal hDC As Long) As Long
Private Declare Function DeleteDC Lib "GDI32" (ByVal hDC As Long) As Long
Private Declare Function DPtoLP Lib "GDI32" (ByVal hDC As Long, lpPoint As PointAPI, ByVal nCount As Long) As Long
Private Declare Function LPtoDP Lib "GDI32" (ByVal hDC As Long, lpPoint As PointAPI, ByVal nCount As Long) As Long
Private Declare Function GetObjectBmp Lib "GDI32" Alias "GetObjectA" _
(ByVal hObject As Long, ByVal nCount As Long, lpObject As bitmap) As Long
Private Declare Function CreateEnhMetaFile Lib "GDI32" Alias "CreateEnhMetaFileA" _
(ByVal hdcRef As Long, ByVal lpFileName As String, ByRef lpRect As Any, ByVal lpDescription As String) As Long
Private Declare Function CloseEnhMetaFile Lib "GDI32" (ByVal hDC As Long) As Long
Private Declare Function DeleteEnhMetaFile Lib "GDI32" (ByVal hemf As Long) As Long
Private Declare Function GetEnhMetaFileBits Lib "GDI32" (ByVal hemf As Long, ByVal cbBuffer As Long, lpbBuffer As Byte) As Long
'***************************************************************************************
'* Constantes *
'***************************************************************************************
'Format d'images
Private Const CF_ENHMETAFILE = 14
' Types pour BitBlt/StretchBlt
Private Const SRCCOPY = &HCC0020
' GetDeviceCaps
Private Const LOGPIXELSY = 90
Private Const LOGPIXELSX = 88
' Diverses Constantes
Private Const HIMETRIC_INCH = 2540 ' Pour conversion Pouce<->Himetric
Private Const COLORONCOLOR = 3 ' Mode pour StretchBlt
Private Const HALFTONE = 4 ' Mode pour StretchBlt avec antialiasing
Private Const MM_HIMETRIC = 3
Private Const MM_TEXT = 1
'***************************************************************************************
'* Types *
'***************************************************************************************
' Type Point pour API
Private Type PointAPI
X As Long
Y As Long
End Type
Private Type bitmap
bmType As Long
bmWidth As Long
bmHeight As Long
bmWidthBytes As Long
bmPlanes As Integer
bmBitsPixel As Integer
bmBits As Long
End Type
' Rectangle pour API
Private Type Rect
Left As Long
Top As Long
Right As Long
Bottom As Long
End Type
'***************************************************************************************
'* Fonction pour convertir l'image pour Access *
'***************************************************************************************
' pPicture est un identifiant d'image (ex : Img.FileData.Picture)
' la fonction renvoie un tableau formaté pour la propriété PictureData d'un contrôle Access
'***************************************************************************************
Public Function WIAtoAccess(pPicture As Long) As Variant
On Error GoTo Gestion_Erreurs
Dim lPictureData() As Byte
Dim lBmp As bitmap
Dim lhDC As Long
Dim lOldBmp As Long
Dim lhMeta As Long
Dim lhMetaFile As Long
Dim lhdcref As Long
Dim lrect As Rect
Dim lngret As Long
Dim pt As PointAPI
' Lecture de la taille de l'image
Call GetObjectBmp(pPicture, Len(lBmp), lBmp)
' Récupère la taille en données de type OLE_Himetric pour la création de l'EMF
lhdcref = GetDC(0) ' Device contexte temporaire
lngret = SetMapMode(lhdcref, MM_HIMETRIC)
' Rectangle pour création de l'EMF
lrect.Right = lBmp.bmWidth * (HIMETRIC_INCH / GetDeviceCaps(lhdcref, LOGPIXELSX))
lrect.Bottom = lBmp.bmHeight * (HIMETRIC_INCH / GetDeviceCaps(lhdcref, LOGPIXELSY))
' Conversion de la taille en pixels
pt.X = lrect.Right
pt.Y = lrect.Bottom
LPtoDP lhdcref, pt, 1
pt.Y = Abs(pt.Y)
' Mode d'origine
SetMapMode lhdcref, lngret
' Création d'un contexte d'affichage EMF
lhMeta = CreateEnhMetaFile(lhdcref, vbNullString, lrect, vbNullString)
' Coordonnées en pixels
lngret = SetMapMode(lhMeta, MM_TEXT)
lngret = SetStretchBltMode(lhMeta, HALFTONE)
' Crée un contexte d'affichage temporaire
lhDC = CreateCompatibleDC(0)
' Affecte le bitmap au DC temporaire
lOldBmp = SelectObject(lhDC, pPicture)
' Mapping mode.
SetMapMode lhMeta, GetMapMode(lhDC)
' Copie directe de l'image dans le MetaFile
StretchBlt lhMeta, 0, 0, pt.X, pt.Y, lhDC, 0, 0, lBmp.bmWidth, lBmp.bmHeight, SRCCOPY
' Ferme le contexte d'affichage et récupère le MetaFile
lhMetaFile = CloseEnhMetaFile(lhMeta)
If Abs(pt.X) > 0 And Abs(pt.Y) > 0 Then
lngret = GetEnhMetaFileBits(lhMetaFile, 0, ByVal 0&)
If lngret > 1 Then
' Redimensionne le tableau de données
ReDim lPictureData((lngret - 1) + 8)
' Récupère les données dans le tableau
lngret = GetEnhMetaFileBits(lhMetaFile, lngret, lPictureData(8))
' Supprime le MétaFile
lngret = DeleteEnhMetaFile(lhMetaFile)
' Type de l'image dans le tableau de données
lPictureData(0) = CF_ENHMETAFILE
End If
Else
' Image invalide
lPictureData = Null
End If
Gestion_Erreurs:
' Libère le device contexte de travail
ReleaseDC 0&, lhdcref
' Sélectionne l'ancien bitmap pour libérer pPicture
SelectObject lhDC, lOldBmp
' Supprime le contexte d'affichage temporaire
DeleteDC (lhDC)
If Err.Number <> 0 Then WIAtoAccess = Null Else WIAtoAccess = lPictureData
End Function |
Partager