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 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183
|
Option Compare Database
Option Explicit
'***************************************************************************************
'* API *
'***************************************************************************************
' GDI
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 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 CreateCompatibleBitmap Lib "gdi32" _
(ByVal hdc As Long, ByVal nWidth As Long, ByVal nHeight As Long) As Long
' Image EMF
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 STRETCH_HALFTONE = 4 ' Mode pour StretchBlt
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
'---------------------------------------------------------------------------------------
' Redimensionne une image avant de l'injecter dans un contrôle
'---------------------------------------------------------------------------------------
' pFichier : Chemin du fichier à charger
' pControl : Contrôle image dans lequel on affiche l'image
'---------------------------------------------------------------------------------------
Public Sub ChargeImageReduite(pFichier As String, pControl As Access.Image)
Dim lhDC As Long, lhdcref As Long
Dim lHdcResize As Long
Dim lOldBitmap As Long, lBitmap As Long
Dim lImg As Object, lOldImg As Long
Dim lngret As Long
Dim lBmpInfo As bitmap
Dim lrect As Rect
Dim pt As PointAPI
Dim lhMeta As Long
Dim lhMetaFile As Long
Dim lpicdata() As Byte
Dim lWidth As Long, lHeight As Long
' Chargement de l'image
lhDC = CreateCompatibleDC(0)
Set lImg = LoadPicture(pFichier)
apiGetObject lImg.handle, Len(lBmpInfo), lBmpInfo
' Sélectionne le bitmap image dans le contexte d'affichage
lOldImg = SelectObject(lhDC, lImg)
' Taille de l'image
lWidth = TwipsToPixelX(pControl.Width)
lHeight = TwipsToPixelX(pControl.Width) / lImg.Width * lImg.Height
' Conversion en coordonnées HiMetric pour EMF
pt.x = lWidth
pt.y = lHeight
lhdcref = GetDC(0) ' Device contexte temporaire
lngret = SetMapMode(lhdcref, MM_HIMETRIC)
DPtoLP lhdcref, pt, 1
pt.y = Abs(pt.y)
lrect.Right = pt.x
lrect.Bottom = pt.y
SetMapMode lhdcref, lngret
' Redimensionne l'image
lHdcResize = CreateCompatibleDC(0)
lBitmap = CreateCompatibleBitmap(lhDC, lWidth, lHeight)
lOldBitmap = SelectObject(lHdcResize, lBitmap)
Call SetStretchBltMode(lHdcResize, STRETCH_HALFTONE)
StretchBlt lHdcResize, 0, 0, lWidth, lHeight, lhDC, 0, 0, lBmpInfo.bmWidth, lBmpInfo.bmHeight, SRCCOPY
' Supprime l'image originale de la mémoire
DeleteObject (SelectObject(lhDC, lOldImg))
Set lImg = Nothing
DeleteDC lhDC
' Création d'un contexte d'affichage EMF
lhMeta = CreateEnhMetaFile(lhdcref, vbNullString, lrect, vbNullString)
lngret = SetMapMode(lhMeta, MM_TEXT)
lngret = SetStretchBltMode(lhMeta, STRETCH_HALFTONE)
' Copie directe de l'image réduite dans le MetaFile
BitBlt lhMeta, 0, 0, lWidth, lHeight, lHdcResize, 0, 0, SRCCOPY
' Supprime l'image redimensionnée de la mémoire
DeleteObject SelectObject(lHdcResize, lOldBitmap)
DeleteDC lHdcResize
' Ferme le contexte d'affichage et récupère le MetaFile
lhMetaFile = CloseEnhMetaFile(lhMeta)
' Libère le device contexte temporaire
ReleaseDC 0, lhdcref
' Récupère la taille des données Méta
lngret = GetEnhMetaFileBits(lhMetaFile, 0, ByVal 0&)
' Redimensionne le tableau de données
ReDim lpicdata((lngret - 1) + 8)
' Récupère les données dans le tableau
Call GetEnhMetaFileBits(lhMetaFile, lngret, lpicdata(8))
' Supprime le MétaFile
lngret = DeleteEnhMetaFile(lhMetaFile)
' Type de l'image dans le tableau de données
lpicdata(0) = CF_ENHMETAFILE
' Affecte les données de l'image au contrôle
pControl.PictureData = lpicdata
End Sub
'---------------------------------------------------------------------------------------
' Converti les Twips en Pixels sur l'axe horizontal
'---------------------------------------------------------------------------------------
' pTwipsX : Valeur à convertir en Twips
' Renvoie la valeur convertie en Pixels
'---------------------------------------------------------------------------------------
Public Function TwipsToPixelX(pTwipsX As Long) As Long
Static Mult As Long
Dim hdc As Long
If Mult = 0 Then
hdc = GetDC(0)
Mult = 1440 / GetDeviceCaps(hdc, LOGPIXELSX)
ReleaseDC 0, hdc
End If
TwipsToPixelX = CLng(pTwipsX / Mult)
End Function
'---------------------------------------------------------------------------------------
' Converti les Twips en Pixels sur l'axe vertical
'---------------------------------------------------------------------------------------
' pTwipsY : Valeur à convertir en Twips
' Renvoie la valeur convertie en Pixels
'---------------------------------------------------------------------------------------
Public Function TwipsToPixelY(pTwipsY As Long) As Long
Static Mult As Long
Dim hdc As Long
If Mult = 0 Then
hdc = GetDC(0)
Mult = 1440 / GetDeviceCaps(hdc, LOGPIXELSY)
ReleaseDC 0, hdc
End If
TwipsToPixelY = CLng(pTwipsY / Mult)
End Function |
Partager