Solved

(75 pts) Getting StdPicture object from pointer to HBITMAP

Posted on 2000-05-06
7
732 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 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
Instantly Create Instructional Tutorials

Contextual Guidance at the moment of need helps your employees adopt to new software or processes instantly. Boost knowledge retention and employee engagement step-by-step with one easy solution.

 
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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
pop out of webbrowser1 control vba6 5 51
Send outlook email from VBS Script 2 67
how to Classify of email (pdf attachments) 7 56
backup program with robocopy 6 91
Have you ever wanted to restrict the users input in a textbox to numbers, and while doing that make sure that they can't 'cheat' by pasting in non-numeric text? Of course you can do that with code you write yourself but it's tedious and error-prone …
Background What I'm presenting in this article is the result of 2 conditions in my work area: We have a SQL Server production environment but no development or test environment; andWe have an MS Access front end using tables in SQL Server but we a…
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…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

734 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