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

Create a VB Picture Object from a GDI Bitmap Handle

Author:

Steve McMahon(steve@vbaccelerator.com)

Keywords:

API,GDI,Graphics

Updated:

13/10/98

Other Tips
All Tips
By Date
By Subject


API (33)
Bit
Manipulation (3)

Clipboard (3)
Combo
Box (5)

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

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

Windows (11)
Windows
Controls (10)



Submit


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
    cbSizeofStruct As Long
    picType As Long
    hImage As Long
    xExt As Long
    yExt As Long
End Type
Private Type Guid
    Data1 As Long
    Data2 As Integer
    Data3 As Integer
    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

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

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" _
    (ByVal hInst As Long, ByVal lpsz As String, _
    ByVal iType As Long, _
    ByVal cx As Long, ByVal cy As Long, _
    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 Const LR_LOADTRANSPARENT = &H20


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

 
 

About  Contribute  Send Feedback  Privacy

Copyright © 1998-1999, Steve McMahon ( steve@vbaccelerator.com). All Rights Reserved.
Last updated: 13/10/98

g="0" cellspacing="0" width="100%">  

About  Contribute  Send Feedback  Privacy

Copyright © 1998-1999, Steve McMahon ( steve@vbaccelerator.com). All Rights Reserved.
Last updated: 13/10/98