Results 1 to 9 of 9

Thread: load a picture box control from memory

  1. #1

    Thread Starter
    New Member
    Join Date
    Apr 2004
    Posts
    5

    load a picture box control from memory

    How can I load a picture box control with a bit map that is in a byte array? I really don't want to write this to a file first. Thanks in advance.

  2. #2
    Hyperactive Member
    Join Date
    Nov 2002
    Location
    Someplace 'ore the rainbow
    Posts
    392
    If the array is set up properly it would just be
    PictureBox.Picture = Array

    cjqp
    When your answer is the Arc Sin of 1.015, you should check your Pythagorean triple.

  3. #3

    Thread Starter
    New Member
    Join Date
    Apr 2004
    Posts
    5

    load picture box from memory

    If I use that syntax I get a type mismatch error. If I use the picturebox.picture = loadpicture(array) I get an error because it isn't a file. What am I missing?

  4. #4
    Addicted Member
    Join Date
    Aug 2002
    Posts
    187
    Have you tried the PictureFromBits function. I load an image into
    an image control using:

    VB Code:
    1. imgPerson.Picture = PictureFromBits(ImageStream)

    Jack

  5. #5

    Thread Starter
    New Member
    Join Date
    Apr 2004
    Posts
    5

    load picture box control from memory

    I am using VB6, so I don't have the imagestream object (wish I did!).

  6. #6
    Addicted Member
    Join Date
    Aug 2002
    Posts
    187
    I too am using VB6, my fault though I should of commented my code...ImageStream is a class property I use for my image byte stream.

    To clarify:

    VB Code:
    1. imgPerson.Picture = PictureFromBits(myByteVariable)
    2. [COLOR=green]where myByteStream is your variable containing the byte stream[/COLOR]

    Jack

  7. #7

    Thread Starter
    New Member
    Join Date
    Apr 2004
    Posts
    5

    Load picture box from memory

    I have seen PictureFromBits() referenced, but do you have the code for that function?

  8. #8
    Addicted Member
    Join Date
    Aug 2002
    Posts
    187
    Getting late where I am...not thinking straight. Here is the full code I use for PictureFromBits function. I got this off the net a while ago and cannot remember who the author was. My apologies to him/her for not being able to give them credit.

    I have this in a BAS module
    VB Code:
    1. Option Explicit
    2. Public Enum CBoolean   ' enum members are Long data types
    3.   CFalse = 0
    4.   CTrue = 1
    5. End Enum
    6.  
    7. Public Const S_OK = 0    ' indicates successful HRESULT
    8.  
    9. 'WINOLEAPI CreateStreamOnHGlobal(
    10. '    HGLOBAL hGlobal,            // Memory handle for the stream object
    11. '    BOOL fDeleteOnRelease,  // Whether to free memory when the object is released
    12. '    LPSTREAM * ppstm           // Indirect pointer to the new stream object
    13. ');
    14. Declare Function CreateStreamOnHGlobal Lib "ole32" _
    15.                               (ByVal hGlobal As Long, _
    16.                               ByVal fDeleteOnRelease As CBoolean, _
    17.                               ppstm As Any) As Long
    18.  
    19. 'STDAPI OleLoadPicture(
    20. '    IStream * pStream,  // Pointer to the stream that contains picture's data
    21. '    LONG lSize,            // Number of bytes read from the stream
    22. '    BOOL fRunmode,   // The opposite of the initial value of the picture's property
    23. '    REFIID riid,             // Reference to the identifier of the interface describing the type
    24. '                                   // of interface pointer to return
    25. '    VOID ppvObj          // Indirect pointer to the object, not AddRef'd!!
    26. ');
    27. Declare Function OleLoadPicture Lib "olepro32" _
    28.                               (pStream As Any, _
    29.                               ByVal lSize As Long, _
    30.                               ByVal fRunmode As CBoolean, _
    31.                               riid As GUID, _
    32.                               ppvObj As Any) As Long
    33.  
    34. Public Type GUID    ' 16 bytes (128 bits)
    35.   dwData1 As Long      ' 4 bytes
    36.   wData2 As Integer     ' 2 bytes
    37.   wData3 As Integer     ' 2 bytes
    38.   abData4(7) As Byte   ' 8 bytes, zero based
    39. End Type
    40.  
    41. Declare Function CLSIDFromString Lib "ole32" (ByVal lpsz As Any, pclsid As GUID) As Long
    42.  
    43. Public Const sIID_IPicture = "{7BF80980-BF32-101A-8BBB-00AA00300CAB}"
    44.  
    45. Public Const GMEM_MOVEABLE = &H2
    46. Declare Function GlobalAlloc Lib "kernel32" (ByVal uFlags As Long, ByVal dwBytes As Long) As Long
    47. Declare Function GlobalLock Lib "kernel32" (ByVal hMem As Long) As Long
    48. Declare Function GlobalUnlock Lib "kernel32" (ByVal hMem As Long) As Long
    49. Declare Function GlobalFree Lib "kernel32" (ByVal hMem As Long) As Long
    50.  
    51. Declare Sub MoveMemory Lib "kernel32" Alias "RtlMoveMemory" (pDest As Any, pSource As Any, ByVal dwLength As Long)
    52.  
    53. ' ====================================================================
    54.  
    55.  
    56. Public Type OPENFILENAME  '  ofn
    57.   lStructSize As Long
    58.   hWndOwner As Long
    59.   hInstance As Long
    60.   lpstrFilter As String
    61.   lpstrCustomFilter As String
    62.   nMaxCustFilter As Long
    63.   nFilterIndex As Long
    64.   lpstrFile As String
    65.   nMaxFile As Long
    66.   lpstrFileTitle As String
    67.   nMaxFileTitle As Long
    68.   lpstrInitialDir As String
    69.   lpstrTitle As String
    70.   Flags As Long
    71.   nFileOffset As Integer
    72.   nFileExtension As Integer
    73.   lpstrDefExt As String
    74.   lCustData As Long
    75.   lpfnHook As Long
    76.   lpTemplateName As String
    77. End Type
    78.  
    79. Public Function PictureFromBits(abPic() As Byte) As IPicture  ' not a StdPicture!!
    80.         Dim nLow As Long
    81.         Dim cbMem  As Long
    82.         Dim hMem  As Long
    83.         Dim lpMem  As Long
    84.         Dim IID_IPicture As GUID
    85.         Dim istm As stdole.IUnknown '  IStream
    86.         Dim ipic As IPicture
    87.  
    88.         ' Get the size of the picture's bits
    89.  
    90. 100     nLow = LBound(abPic)
    91.        
    92. 110     cbMem = (UBound(abPic) - nLow) + 1
    93.  
    94.         ' Allocate a global memory object
    95. 120     hMem = GlobalAlloc(GMEM_MOVEABLE, cbMem)
    96.  
    97. 130     If hMem Then
    98.    
    99.             ' Lock the memory object and get a pointer to it.
    100. 140         lpMem = GlobalLock(hMem)
    101.  
    102. 150         If lpMem Then
    103.      
    104.                 ' Copy the picture bits to the memory pointer and unlock the handle.
    105. 160             MoveMemory ByVal lpMem, abPic(nLow), cbMem
    106. 170             Call GlobalUnlock(hMem)
    107.      
    108.                 ' Create an ISteam from the pictures bits (we can explicitly free hMem
    109.                 ' below, but we'll have the call do it...)
    110. 180             If (CreateStreamOnHGlobal(hMem, CTrue, istm) = S_OK) Then
    111. 190                 If (CLSIDFromString(StrPtr(sIID_IPicture), IID_IPicture) = S_OK) Then
    112.          
    113.                         ' Create an IPicture from the IStream (the docs say the call does not
    114.                         ' AddRef its last param, but it looks like the reference counts are correct..)
    115. 200                     Call OleLoadPicture(ByVal ObjPtr(istm), cbMem, CFalse, _
    116.                                 IID_IPicture, PictureFromBits)
    117.          
    118.                     End If   ' CLSIDFromString
    119.                 End If   ' CreateStreamOnHGlobal
    120.             End If   ' lpMem
    121.    
    122.             '    Call GlobalFree(hMem)
    123.         End If   ' hMem
    124.      
    125.     End Function

    Jack

  9. #9

    Thread Starter
    New Member
    Join Date
    Apr 2004
    Posts
    5

    load picture box from memory

    Jack,
    That worked perfectly! Thanks for all your help. I can now push this project ahead. Thanks again.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width