Option Explicit
#If VBA7 Then
Private Declare PtrSafe Function GetDC Lib "user32" (ByVal hwnd As LongPtr) As LongPtr
Private Declare PtrSafe Function ReleaseDC Lib "user32" (ByVal hwnd As LongPtr, ByVal hDC As LongPtr) As Long
Private Declare PtrSafe Function GetDeviceCaps Lib "gdi32" (ByVal hDC As LongPtr, ByVal nIndex As Long) As Long
#Else
Private Enum LongPtr
[_]
End Enum
Private Declare Function GetDC Lib "user32" (ByVal hwnd As LongPtr) As LongPtr
Private Declare Function ReleaseDC Lib "user32" (ByVal hwnd As LongPtr, ByVal hDC As LongPtr) As Long
Private Declare Function GetDeviceCaps Lib "gdi32" (ByVal hDC As LongPtr, ByVal nIndex As Long) As Long
#End If
Sub AdjustImageSize(ByVal Pic As Shape, ByVal FilePathName As String)
Dim img As Object, w As Double, h As Double
On Error GoTo errHandler
Set img = CreateObject("WIA.ImageFile")
img.LoadFile FilePathName
w = img.FileData.ImageFile.Width
h = img.FileData.ImageFile.Height
With Pic
.Height = PXtoPT(h, True)
.Width = PXtoPT(w, False)
End With
Set img = Nothing
Exit Sub
errHandler:
MsgBox Err.Description & vbLf & "Error# : " & Err.Number
End Sub
Function ScreenDPI(ByVal bVert As Boolean) As Long
#If Win64 Then
Const NULL_PTR = 0^
#Else
Const NULL_PTR = 0&
#End If
Const LOGPIXELSX As Long = 88&, LOGPIXELSY As Long = 90&
Static lDPI(1) As Long, hDC As LongPtr
If lDPI(0&) = 0& Then
hDC = GetDC(NULL_PTR)
lDPI(0&) = GetDeviceCaps(hDC, LOGPIXELSX)
lDPI(1&) = GetDeviceCaps(hDC, LOGPIXELSY)
hDC = ReleaseDC(NULL_PTR, hDC)
End If
ScreenDPI = lDPI(Abs(bVert))
End Function
Function PXtoPT(ByVal Pixels As Long, ByVal bVert As Boolean) As Single
Const POINTSPERINCH As Long = 72&
PXtoPT = (Pixels * POINTSPERINCH) / ScreenDPI(bVert)
End Function