Solved

(75 pts) Getting StdPicture object from pointer to HBITMAP

Posted on 2000-05-06
7
693 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

 
LVL 32

Expert Comment

by:Erick37
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

There are many ways to remove duplicate entries in an SQL or Access database. Most make you temporarily insert an ID field, make a temp table and copy data back and forth, and/or are slow. Here is an easy way in VB6 using ADO to remove duplicate row…
This article describes some techniques which will make your VBA or Visual Basic Classic code easier to understand and maintain, whether by you, your replacement, or another Experts-Exchange expert.
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…
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…

771 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

13 Experts available now in Live!

Get 1:1 Help Now