The new vbAccelerator Site - more VB and .NET Code and Controls

Create a VB Picture Object from a GDI Bitmap Handle


Steve McMahon(





Other Tips
All Tips
By Date
By Subject

API (33)
Manipulation (3)

Clipboard (3)
Box (5)

Desktop (3)
GDI (13)
Graphics (13)
Internet (2)
Comms (3)

Keyboard (2)
Mouse (1)
Shell (1)
Sprites (1)
Subclassing (3)
Box (2)

Windows (11)
Controls (10)


This tip shows you how create a VB Picture object from a GDI bitmap handle. This is useful if you are dealing with real GDI bitmaps.

Start a new project and add a module. Then add the following code to the module:

Option Explicit

Private Type PictDesc
&nbsp &nbsp cbSizeofStruct As Long
&nbsp &nbsp picType As Long
&nbsp &nbsp hImage As Long
&nbsp &nbsp xExt As Long
&nbsp &nbsp yExt As Long
End Type
Private Type Guid
&nbsp &nbsp Data1 As Long
&nbsp &nbsp Data2 As Integer
&nbsp &nbsp Data3 As Integer
&nbsp &nbsp Data4(0 To 7) As Byte
End Type
Private Declare Function OleCreatePictureIndirect Lib "olepro32.dll" (lpPictDesc As PictDesc, riid As Guid, ByVal fPictureOwnsHandle As Long, ipic As IPicture) As Long

Public Function BitmapToPicture(ByVal hBmp As Long) As IPicture

&nbsp &nbsp If (hBmp = 0) Then Exit Function
&nbsp &nbsp
&nbsp &nbsp Dim oNewPic As Picture, tPicConv As PictDesc, IGuid As Guid
&nbsp &nbsp
&nbsp &nbsp
' Fill PictDesc structure with necessary parts:
&nbsp &nbsp With tPicConv
&nbsp &nbsp .cbSizeofStruct = Len(tPicConv)
&nbsp &nbsp .picType = vbPicTypeBitmap
&nbsp &nbsp .hImage = hBmp
&nbsp &nbsp End With
&nbsp &nbsp
&nbsp &nbsp
' Fill in IDispatch Interface ID
&nbsp &nbsp With IGuid
&nbsp &nbsp .Data1 = &H20400
&nbsp &nbsp .Data4(0) = &HC0
&nbsp &nbsp .Data4(7) = &H46
&nbsp &nbsp End With
&nbsp &nbsp
&nbsp &nbsp
' Create a picture object:
&nbsp &nbsp OleCreatePictureIndirect tPicConv, IGuid, True, oNewPic
&nbsp &nbsp
&nbsp &nbsp
' Return it:
&nbsp &nbsp Set BitmapToPicture = oNewPic
&nbsp &nbsp

End Function

To try out a the function, add a Command Button and a Picture Box to your project's form. Set the Picture Box's AutoSize property to True, then copy a bitmap to the project's directory, and rename it TEST.BMP.

Then add this code to the form:

Option Explicit

Private Declare Function LoadImage Lib "user32" Alias "LoadImageA" _
&nbsp &nbsp (ByVal hInst As Long, ByVal lpsz As String, _
&nbsp &nbsp ByVal iType As Long, _
&nbsp &nbsp ByVal cx As Long, ByVal cy As Long, _
&nbsp &nbsp ByVal fOptions As Long) As Long
' iType options:
Private Const IMAGE_BITMAP = 0
Private Const IMAGE_ICON = 1
Private Const IMAGE_CURSOR = 2
' fOptions flags:
Private Const LR_LOADMAP3DCOLORS = &H1000
Private Const LR_LOADFROMFILE = &H10

Private Sub Command1_Click()
Dim hBmp As Long
&nbsp &nbsp
' Load an icon called Test.Ico from the directory:
&nbsp &nbsp
&nbsp &nbsp
' If the icon contains more than one size of image,
&nbsp &nbsp ' set cx and cy to the width and height to load
&nbsp &nbsp ' the appropriate image in:
&nbsp &nbsp hBmp = LoadImage(App.hInstance, App.Path & "\TEST.BMP", IMAGE_BITMAP, 0, 0, LR_LOADFROMFILE Or LR_LOADMAP3DCOLORS)
&nbsp &nbsp
' Set the picture to this icon:
&nbsp &nbsp Set Picture1.Picture = BitmapToPicture(hBmp)
&nbsp &nbsp
End Sub

Start the project. When you click the button, the bitmap will be loaded as a GDI bitmap handle, converted to a picture and then shown in the PictureBox. Whilst this simple example could be achieved more quickly using the VB LoadPicture method, the method shown has several advantages:

  • You can specify that Gray (RGB 192,192,192) and Dark Gray (128,128,128) are mapped to the appropriate system colours (vbButtonFace and vbButtonShadow respectively). (LR_LOADMAP3DCOLORS flag)
  • You can specify that all occurrences of the colour in the top left pixel are mapped to the vbButtonFace colour (LR_LOADTRANSPARENT flag).


Related Tips and Articles:


AboutContributeSend FeedbackPrivacy

Copyright 1998-1999, Steve McMahon ( All Rights Reserved.
Last updated: 13/10/98