Results 1 to 12 of 12

Thread: How to display a byte array bitmap on Picturebox?

  1. #1

    Thread Starter
    New Member
    Join Date
    Jan 2007
    Posts
    7

    How to display a byte array bitmap on Picturebox?

    There is a byte array(BMP1()), it include a full bitmap, how to display this bitmap on a picturebox? Of course i can save the byte array as a file(test.bmp) first, and use set picture1.picture=loadpicture("test.bmp"), but i dont want to use this method. How to display this bitmap on a picturebox without saving as a file?
    Thanks in advan.

  2. #2
    VB-aholic & Lovin' It LaVolpe's Avatar
    Join Date
    Oct 2007
    Location
    Beside Waldo
    Posts
    19,541

    Re: How to display a byte array bitmap on Picturebox?

    This is a common method for your request. If array is invalid or does not contain a valid picture, lock up can occur. The function returns a stdPicture object that you can apply where needed.
    Code:
    ' used to create a stdPicture from a byte array
    Private Declare Function CreateStreamOnHGlobal Lib "ole32" (ByVal hGlobal As Long, ByVal fDeleteOnRelease As Long, ppstm As Any) As Long
    Private Declare Function GlobalAlloc Lib "kernel32" (ByVal uFlags As Long, ByVal dwBytes As Long) As Long
    Private Declare Function GlobalLock Lib "kernel32" (ByVal hMem As Long) As Long
    Private Declare Function GlobalUnlock Lib "kernel32" (ByVal hMem As Long) As Long
    Private Declare Function OleLoadPicture Lib "olepro32" (pStream As Any, ByVal lSize As Long, ByVal fRunmode As Long, riid As Any, ppvObj As Any) As Long
    Private Declare Sub CopyMemory Lib "kernel32.dll" Alias "RtlMoveMemory" (ByRef Destination As Any, ByRef Source As Any, ByVal Length As Long)
    
    Public Function ArrayToPicture(inArray() As Byte, Offset As Long, Size As Long) As IPicture
        
        ' function creates a stdPicture from the passed array
        ' Offset is first item in array: 0 for 0 bound arrays    
        ' Size is how many bytes comprise the image
        Dim o_hMem  As Long
        Dim o_lpMem  As Long
        Dim aGUID(0 To 3) As Long
        Dim IIStream As IUnknown
        
        aGUID(0) = &H7BF80980    ' GUID for stdPicture
        aGUID(1) = &H101ABF32
        aGUID(2) = &HAA00BB8B
        aGUID(3) = &HAB0C3000
        
        o_hMem = GlobalAlloc(&H2&, Size)
        If Not o_hMem = 0& Then
            o_lpMem = GlobalLock(o_hMem)
            If Not o_lpMem = 0& Then
                CopyMemory ByVal o_lpMem, inArray(Offset), Size
                Call GlobalUnlock(o_hMem)
                If CreateStreamOnHGlobal(o_hMem, 1&, IIStream) = 0& Then
                      Call OleLoadPicture(ByVal ObjPtr(IIStream), 0&, 0&, aGUID(0), ArrayToPicture)
                End If
            End If
        End If
    End Function

  3. #3
    G&G Moderator chemicalNova's Avatar
    Join Date
    Jun 2002
    Location
    Victoria, Australia
    Posts
    4,246

    Re: How to display a byte array bitmap on Picturebox?

    The reason that locks up is because its recursive..

    If you want total control.. write your own Bitmap loader. Then pass the byte array into it.

    Use Get and SetDIBits to create another array of pixel values, fill them with the values from the file, and throw it back to the PictureBox.

    chem

    Visual Studio 6, Visual Studio.NET 2005, MASM

  4. #4
    VB-aholic & Lovin' It LaVolpe's Avatar
    Join Date
    Oct 2007
    Location
    Beside Waldo
    Posts
    19,541

    Re: How to display a byte array bitmap on Picturebox?

    What is recursive, the code I posted? It is not. It is a set of simple API calls. There is nothing recursive about it. Where you see ArrayToPicture in the API call, that is because it is being passed ByRef, nothing recursive about it. The reason it can lock up is because the OLELoadPicture API has a low tolerance for malformated images - corrupted GIFs being the worse. I have sample GIFs that will lock up VB's LoadPicture() function too, as expected, because that function uses the same API call.

    P.S. The poster wanted to avoid using a file as an intermediary step.

  5. #5

    Thread Starter
    New Member
    Join Date
    Jan 2007
    Posts
    7

    Re: How to display a byte array bitmap on Picturebox?

    To lavolpe
    Thank you, but it doesnt work. My byte array include a Full bitmap file(include BITMAPFILEHEADER and BITMAPINFOHEADER).

    To chemicalNova
    I think it should use SetDIBits, and i tried before i posted, but i dont know how to "fill them with the values from the file".

  6. #6
    VB-aholic & Lovin' It LaVolpe's Avatar
    Join Date
    Oct 2007
    Location
    Beside Waldo
    Posts
    19,541

    Re: How to display a byte array bitmap on Picturebox?

    Did you pass the correct parameter for Size? Maybe it should be UBound(array)+1? Did you use the keyword SET?

    You may not be using it correctly? That function expects the full header and everything, passing it just pixels won't work. I use this same routine in many applications. It works because it is basically the API version of VB's LoadPicture(). Post your bmp file and let me test it;

    Edited: Usage. imgData() declared As Byte, zero-bound
    If array is not zero bound or can be larger than the image data, change parameters as needed

    Set Picture1.Picture = ArrayToPicture(imgData(), 0, UBound(imgData)+1)
    Last edited by LaVolpe; Oct 19th, 2007 at 11:34 AM.

  7. #7
    PowerPoster
    Join Date
    Jan 2020
    Posts
    5,538

    Re: How to display a byte array bitmap on Picturebox?

    ShowPicFromStream by vb6,Show Picture from Byte array-VBForums
    https://www.vbforums.com/showthread....rom-Byte-array

    Code:
    Dim Gdi1 As New ShowPicStream
    
    Private Sub Command1_Click()
    Picture1.AutoRedraw = True
    
    Dim Buffer() As Byte
    Gdi1.CreateGraphics Picture1.hDC
    Gdi1.GetFileByte app.path & "\01_alpha-big.png", Buffer
    Gdi1.ShowPicFromStream Buffer
    Picture1.Refresh
    End Sub

  8. #8
    PowerPoster
    Join Date
    Jul 2010
    Location
    NYC
    Posts
    7,658

    Re: How to display a byte array bitmap on Picturebox?

    I'm sure bonoli is thrilled to learn of this alternative answer to his question 16 years after posting it, and will surely see it despite not having posted for 14 years.

  9. #9
    Hyperactive Member
    Join Date
    Aug 2013
    Posts
    266

    Re: How to display a byte array bitmap on Picturebox?

    Quote Originally Posted by fafalone View Post
    I'm sure bonoli is thrilled to learn of this alternative answer to his question 16 years after posting it, and will surely see it despite not having posted for 14 years.
    Not the same thing exactly but still always good to get reminded of this!

    https://xkcd.com/979/

  10. #10
    Angel of Code Niya's Avatar
    Join Date
    Nov 2011
    Posts
    9,017

    Re: How to display a byte array bitmap on Picturebox?

    Quote Originally Posted by fafalone View Post
    I'm sure bonoli is thrilled to learn of this alternative answer to his question 16 years after posting it, and will surely see it despite not having posted for 14 years.
    I thought of this when I read your post:-
    https://stackoverflow.com/questions/...ed-brushes-for

    I would be happy if someone could answer that for me one day, even if it's 16 years later lol.
    Treeview with NodeAdded/NodesRemoved events | BlinkLabel control | Calculate Permutations | Object Enums | ComboBox with centered items | .Net Internals article(not mine) | Wizard Control | Understanding Multi-Threading | Simple file compression | Demon Arena

    Copy/move files using Windows Shell | I'm not wanted

    C++ programmers will dismiss you as a cretinous simpleton for your inability to keep track of pointers chained 6 levels deep and Java programmers will pillory you for buying into the evils of Microsoft. Meanwhile C# programmers will get paid just a little bit more than you for writing exactly the same code and VB6 programmers will continue to whitter on about "footprints". - FunkyDexter

    There's just no reason to use garbage like InputBox. - jmcilhinney

    The threads I start are Niya and Olaf free zones. No arguing about the benefits of VB6 over .NET here please. Happiness must reign. - yereverluvinuncleber

  11. #11
    PowerPoster
    Join Date
    Jul 2010
    Location
    NYC
    Posts
    7,658

    Re: How to display a byte array bitmap on Picturebox?

    The vaunted .NET doesn't have something as simple as TypeHint?

    But yes answering old questions because you've run into the same issue and nobody ever did is doing the Lord's work, but if it's already been answered...

  12. #12
    Angel of Code Niya's Avatar
    Join Date
    Nov 2011
    Posts
    9,017

    Re: How to display a byte array bitmap on Picturebox?

    Quote Originally Posted by fafalone View Post
    The vaunted .NET doesn't have something as simple as TypeHint?
    It's a little more complicated than that. You see Microsoft is keeping all the best goodies for themselves. I'm just trying to get some of that for myself.

    I've tried a couple different things over the years but never found the answer.
    Treeview with NodeAdded/NodesRemoved events | BlinkLabel control | Calculate Permutations | Object Enums | ComboBox with centered items | .Net Internals article(not mine) | Wizard Control | Understanding Multi-Threading | Simple file compression | Demon Arena

    Copy/move files using Windows Shell | I'm not wanted

    C++ programmers will dismiss you as a cretinous simpleton for your inability to keep track of pointers chained 6 levels deep and Java programmers will pillory you for buying into the evils of Microsoft. Meanwhile C# programmers will get paid just a little bit more than you for writing exactly the same code and VB6 programmers will continue to whitter on about "footprints". - FunkyDexter

    There's just no reason to use garbage like InputBox. - jmcilhinney

    The threads I start are Niya and Olaf free zones. No arguing about the benefits of VB6 over .NET here please. Happiness must reign. - yereverluvinuncleber

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