Solved

(75 pts) Getting StdPicture object from pointer to HBITMAP

Posted on 2000-05-06
7
701 Views
Last Modified: 2008-02-20
I'm working with 2 DLL functions that do Bitmap picture conversions.  One takes a StdPicture OLE picture object and convert it to an array of RGB values, the other takes an array and converts it to a StdPicture bitmap.  

In the first conversion function, the C function is asking for an HBITMAP (which is the same as an StdPicture in VB, only it's a pointer to the bitmap instead of a bitmap object) and it returns the array of RGB values.  I pass it an StdPicture where it asks for the HBITMAP, and it returns the array fine... this works great.  I guess it takes the StdPicture and works with it internally in C.  

The second conversion function asks for the array of RBG values and converts them to an HBITMAP.  I pass it the array of RBG values and when I put the return value into a Long, it works fine.  However, when I try to put the return value into an StdPicture, it crashes VB.  The Long value returned is the pointer to the HBITMAP I assume.

I guess what I'm looking for is either a way to get an StdPicture OLE picture object from a Long pointer to a bitmap, or an explanation of how the HBITMAP thing works in C so I can get the StdPicture from it in VB.

NOTE - Long is the equivelant of HBITMAP in VB so when I try to do a straight conversion from the Long to StdPicture, I get a "Type Missmatch" error.

Please Help !

HATCHET
0
Comment
Question by:HATCHET
  • 4
  • 3
7 Comments
 
LVL 32

Accepted Solution

by:
Erick37 earned 75 total points
ID: 2784873
Use the OleCreatePictureIndirect API to get a Picture object from a bitmap handle:


Option Explicit

Private Type PicBmp
   Size As Long
   Type As Long
   hBmp As Long
   hPal As Long
   Reserved As Long
End Type
Private Type GUID
   Data1 As Long
   Data2 As Integer
   Data3 As Integer
   Data4(7) As Byte
End Type

Private Declare Function OleCreatePictureIndirect Lib "olepro32.dll" _
    (PicDesc As PicBmp, RefIID As GUID, _
    ByVal fPictureOwnsHandle As Long, IPic As IPicture) As Long

Public Function PicFromHandle(ByVal hBmp As Long, ByVal hPal As Long) As Picture
    Dim r As Long
    Dim Pic As PicBmp
    ' IPicture requires a reference to "Standard OLE Types."
    Dim IPic As IPicture
    Dim IID_IDispatch As GUID

    ' Fill in with IDispatch Interface ID.
    With IID_IDispatch
    .Data1 = &H20400
    .Data4(0) = &HC0
    .Data4(7) = &H46
    End With

    ' Fill Pic with necessary parts.
    With Pic
    .Size = Len(Pic)          ' Length of structure.
    .Type = vbPicTypeBitmap   ' Type of Picture (bitmap).
    .hBmp = hBmp              ' Handle to bitmap.
    .hPal = hPal              ' Handle to palette (may be null).
    End With

    ' Create Picture object.
    r = OleCreatePictureIndirect(Pic, IID_IDispatch, 1, IPic)

    ' Return the new Picture object.
    Set PicFromHandle = IPic
End Function


'Example of how to use
Private Sub Command1_Click()
    'Usage
    Set Me.Picture = PicFromHandle(Picture1.Picture.Handle, 0)
End Sub
0
 
LVL 3

Author Comment

by:HATCHET
ID: 2785101
Erick37,

YOU ROCK!!  You da man!   =D

Two quick questions :

1)  Where did you get this?  Did you write this?  If so, how did you know the values to go into the IID_IDispatch type?  What are those values for?

2)  What is the purpose of the palette?  I assume its the colors to use to create the picture.  How can I create a custom palette or where can I get a palette to use?  I know that 0 / NULL works, but I'm just curious.

HATCHET
0
 
LVL 32

Expert Comment

by:Erick37
ID: 2785115
Thanks!

1) This code has been floating around the internet for a while.  I forgot where I originally grabbed it, but I believe it came from M$:
http://support.microsoft.com/support/kb/articles/Q161/2/99.ASP

The IID_IDispatch is the GUID number for the IPicture object - defined by Windows.

2) The hPal is for the handle to a palette to be used with the bitmap.  This is only used in 8 bit video mode (256 color) which uses palettes.  Unless you are using a palette mode, this is ignored.
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 32

Expert Comment

by:Erick37
ID: 2785123
Here is another use for OleCreatePictureIndirect, this code extracts icons instead of bitmaps.  Note that the GUID is different:

"vbAccelerator Tips - Create a VB Picture from an API Icon Handle"

http://vbaccelerator.com/tips/vba0024.htm
0
 
LVL 3

Author Comment

by:HATCHET
ID: 2785165
Erick37,

YOU ROCK!!  You da man!   =D

Two quick questions :

1)  Where did you get this?  Did you write this?  If so, how did you know the values to go into the IID_IDispatch type?  What are those values for?

2)  What is the purpose of the palette?  I assume its the colors to use to create the picture.  How can I create a custom palette or where can I get a palette to use?  I know that 0 / NULL works, but I'm just curious.

HATCHET
0
 
LVL 3

Author Comment

by:HATCHET
ID: 2785169
Oops!  Hate when you hit REFRESH and it reposts the comment.

How would one create a palette for use with 8 Bit video mode like you mentioned?

Thanks again!    =D
0
 
LVL 32

Expert Comment

by:Erick37
ID: 2785200
The MS article I pointed to shows how to get the hPal for the system palette, if the system is in palette mode.  Also, the Picture.hPal property will be valid for any picture when in 8 bit mode.

You can create a palette of your own using CreatePalette, but you have to fill in the LOGPALETTE entries manually.

There is a good example function "SetupPalette" located here (near the end of the page):

http://www.ieighty.net/~davepamn/colorcube.html
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Article by: Martin
Here are a few simple, working, games that you can use as-is or as the basis for your own games. Tic-Tac-Toe This is one of the simplest of all games.   The game allows for a choice of who goes first and keeps track of the number of wins for…
You can of course define an array to hold data that is of a particular type like an array of Strings to hold customer names or an array of Doubles to hold customer sales, but what do you do if you want to coordinate that data? This article describes…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

864 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now