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.
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
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
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.
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".
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)
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
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. :p
Re: How to display a byte array bitmap on Picturebox?
Quote:
Originally Posted by
fafalone
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. :p
Not the same thing exactly but still always good to get reminded of this!
https://xkcd.com/979/
Re: How to display a byte array bitmap on Picturebox?
Quote:
Originally Posted by
fafalone
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. :p
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.
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...
Re: How to display a byte array bitmap on Picturebox?
Quote:
Originally Posted by
fafalone
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. :D
I've tried a couple different things over the years but never found the answer.