Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

(75 pts) Getting StdPicture object from pointer to HBITMAP

Posted on 2000-05-06
7
Medium Priority
?
758 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
7 Comments
 
LVL 32

Accepted Solution

by:
Erick37 earned 300 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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
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

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

Question has a verified solution.

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

I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
This article describes how to use a set of graphical playing cards to create a Draw Poker game in Excel or VB6.
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
Suggested Courses

721 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