[blue]Option Explicit
Private Type PICTDESC
lSize As Long
lType As Long
hImage As Long
hPal As Long
lReserved As Long
End Type
Private Type IID
Data1 As Long
Data2 As Integer
Data3 As Integer
Data4(7) As Byte
End Type
Private Const S_OK = &H0
Private Const SRCCOPY = &HCC0020
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 CreateCompatibleBitmap Lib "gdi32" (ByVal hdc As Long, ByVal nWidth As Long, ByVal nHeight As Long) As Long
Private Declare Function CreateCompatibleDC Lib "gdi32" (ByVal hdc As Long) As Long
Private Declare Function OleCreatePictureIndirect Lib "olepro32" (lpPictDesc As PICTDESC, RefIid As IID, ByVal fOwn As Boolean, IPict As IPicture) 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 SetStretchBltMode Lib "gdi32" (ByVal hdc As Long, ByVal nStretchMode As Long) As Long
Private Enum StretchMode
BLACKONWHITE = 1
WHITEONBLACK = 2
COLORONCOLOR = 3
HALFTONE = 4
End Enum
Private Sub Command1_Click()
Dim myPic As IPicture
Set myPic = LoadPicture("c:\Sunset.bmp") ' my example picture; you probably want to change this ...
Set Picture1 = Thumbnail(myPic, 33) ' display the returned thumbnail in a picture
End Sub
Public Function Thumbnail(myPic As IPicture, Optional ScalePercent As Single = 100) As IPicture
Dim srcDC As Long
Dim dstDC As Long
Dim oldsrcBmp As Long
Dim dstBmp As Long
Dim olddstBmp As Long
Dim dstPic As IPicture
Dim srcWidth As Single
Dim srcHeight As Single
srcDC = CreateCompatibleDC(Form1.hdc)
dstDC = CreateCompatibleDC(Form1.hdc)
srcWidth = Form1.ScaleX(myPic.Width, vbHimetric, vbPixels) ' IPicture measurements are HiMetric, but we need pixels
srcHeight = Form1.ScaleX(myPic.Height, vbHimetric, vbPixels)
myPic.SelectPicture srcDC, 0&, 0& ' essentially an OLE version of SelectObject API
dstBmp = CreateCompatibleBitmap(Form1.hdc, srcWidth * (ScalePercent / 100), srcHeight * (ScalePercent / 100)) ' create a blank, appropriately sized destination bitmap
olddstBmp = SelectObject(dstDC, dstBmp)
SetStretchBltMode dstDC, COLORONCOLOR
StretchBlt dstDC, 0, 0, srcWidth * (ScalePercent / 100), srcHeight * (ScalePercent / 100), srcDC, 0, 0, srcWidth, srcHeight, SRCCOPY
dstBmp = SelectObject(dstDC, olddstBmp) ' recover and free handle to our thumbnailed bitmap from DC
Set Thumbnail = PictureFromhBmp(dstBmp) ' and return an IPicture
End Function
Public Function PictureFromhBmp(ByVal hBmp As Long) As IPicture
Dim IPict As IPicture
Dim lpPictDesc As PICTDESC
Dim iidIPict As IID
If hBmp Then
' Minimally initialise PICTDESC structure
With lpPictDesc
.lSize = Len(lpPictDesc)
.lType = vbPicTypeBitmap
.hImage = hBmp
End With
'GUID for IUnknown Interface
'{00000000-0000-0000-C000-000000000046}
iidIPict.Data4(0) = &HC0
iidIPict.Data4(7) = &H46
If OleCreatePictureIndirect(lpPictDesc, iidIPict, True, IPict) = S_OK Then
Set PictureFromhBmp = IPict
End If
End If
End Function[/blue]