(Newbie question) Load image from memory to picturebox

02-17-2005, 03:19 AM
excuse me if i ask one more time about this argument
but i don'y know how to use this (last in the post) procedure posted days ago by OnError

How to i use this procedure to load image from memory to picture box or image control? (i not see any reference to a picture box)

Excuse for my "stupid" question but i'm at beginning with image and graphics at certain level.


Option Explicit

Data1 As Long
Data2 As Integer
Data3 As Integer
Data4(7) As Byte
End Type

Private Declare Function CreateStreamOnHGlobal Lib "ole32.dll" (ByRef hGlobal As Any, ByVal fDeleteOnResume As Long, ByRef ppstr As Any) As Long
Private Declare Function OleLoadPicture Lib "olepro32.dll" (ByVal lpStream As IUnknown, ByVal lSize As Long, ByVal fRunMode As Long, ByRef riid As GUID, ByRef lplpObj As Any) As Long
Private Declare Function CLSIDFromString Lib "ole32.dll" (ByVal lpsz As Long, ByRef pclsid As GUID) As Long

' HKEY_CLASSES_ROOT\Interface\{7BF80980-BF32-101A-8BBB-00AA00300CAB} = IPicture
Private Const SIPICTURE As String = "{7BF80980-BF32-101A-8BBB-00AA00300CAB}"

' convert a byte array to IPicture
Public Function PictureFromRes(ByRef b() As Byte) As IPicture
On Error Goto errorhandler

Dim istrm As IUnknown
Dim tGuid As GUID

If Not CreateStreamOnHGlobal(b(LBound(b)), False, istrm) Then
CLSIDFromString StrPtr(SIPICTURE), tGuid
OleLoadPicture istrm, UBound(b) - LBound(b) + 1, False, tGuid, PictureFromRes
End If

Set istrm = Nothing
Exit Function
Debug.Print "Could not convert to IPicture!"
End Function

02-17-2005, 08:52 AM
You said you have a picture in memory? Can you elaborate on that?

If I'm right, this looks like an excerpt from OnErr0r's function to load from a custom resource into a picture object.

Are you using a resource file in your project?

02-17-2005, 09:44 AM
you are right! i mistake to understand onerror routine. (i think that load from memory and not from resource file)
but anyway what is the correct procedure to assign a picture objects to a picture control?

Is there any other way to load a picture from memory and assign it to a picture control (or image control)?


02-17-2005, 09:55 AM
I guess where I was getting confused is how you were getting the picture into memory in the first place.

To load a picture from a file is easy.. use the LoadPicture() function.

Can you tell me more about what you're trying to do?

02-17-2005, 02:44 PM
This example from AllAPI (or the highly recommended API Guide) helped me out a lot. Its basically a couple of functions that load a Device Context (hDC) into the .Picture of a picture box. After that, I used the Intel .jpg example from VBAccelerator to save the .Picture to a .jpg file :D , but thats the beside the point.

Here is the link to the API example: http://www.mentalis.org/apilist/64251A6CC4AC18DEC4B9CA62DE2A43A5.html
(the CreatePicture example from BitBlt )

02-17-2005, 02:51 PM
My PictureFromRes function can actually load a GIF or JPG in a byte array (memory) to a picturebox. That works from a custom resource or byte array from whereever.


Set Picture1.Picture = PictureFromRes(byteArrayContainingImage)

Btw, you can also save to JPG with gdiplus.dll, a system dll starting with XP and redistributable from Win98 through Win2K.

02-18-2005, 03:14 AM
Thanks at all.
The procedure now is clear.


EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum