Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Convert BMP in Byte Array

Status
Not open for further replies.

victoryhighway2

Programmer
Jul 18, 2005
42
US
Hello,
I'm working on an application that needs to read a 256 color BMP file into a byte array and then download it into an embedded device.

The download portion is already working (it's been tested using an external application that converts the BMP into a text file (with one byte per line.)

What I would like to do is have the BMP to Byte conversion in my VB6 application.

I have found a class module that will give me the height, width and other information from a BMP file (which is helpful), but I also need the image data itself.

Does anyone have any suggestions on how to approach this?

Thanks in advance,
Geoffrey
 
If you load the bitmap into a picturebox, using loadpicture, you could then use point to get the colour values of each pixel. But using API functions is a lot faster.

The following is from the API Guide available from the DevX forum ( It gives a worked example of how to use the GetBitmapBits function.

Code:
'Create a new project, add a command button and a picture box to the project, load a picture into the picture box.
'Paste this code into Form1
Private Type BITMAP
    bmType As Long
    bmWidth As Long
    bmHeight As Long
    bmWidthBytes As Long
    bmPlanes As Integer
    bmBitsPixel As Integer
    bmBits As Long
End Type
Private Declare Function GetObject Lib "gdi32" Alias "GetObjectA" (ByVal hObject As Long, ByVal nCount As Long, lpObject As Any) As Long
Private Declare Function GetBitmapBits Lib "gdi32" (ByVal hBitmap As Long, ByVal dwCount As Long, lpBits As Any) As Long
Private Declare Function SetBitmapBits Lib "gdi32" (ByVal hBitmap As Long, ByVal dwCount As Long, lpBits As Any) As Long
Dim PicBits() As Byte, PicInfo As BITMAP
Dim Cnt As Long, BytesPerLine as Long
Private Sub Command1_Click()
    'KPD-Team 1999
    'URL: [URL unfurl="true"]http://www.allapi.net/[/URL]
    'E-Mail: KPDTeam@Allapi.net
    'Get information (such as height and width) about the picturebox
    GetObject Picture1.Image, Len(PicInfo), PicInfo
    'reallocate storage space
    BytesPerLine = (PicInfo.bmWidth * 3 + 3) And &HFFFFFFFC
    ReDim PicBits(1 To BytesPerLine * PicInfo.bmHeight * 3) As Byte
    'Copy the bitmapbits to the array
    GetBitmapBits Picture1.Image, UBound(PicBits), PicBits(1)
    'Invert the bits
    For Cnt = 1 To UBound(PicBits)
        PicBits(Cnt) = 255 - PicBits(Cnt)
    Next Cnt
    'Set the bits back to the picture
    SetBitmapBits Picture1.Image, UBound(PicBits), PicBits(1)
    'refresh
    Picture1.Refresh
End Sub
 
Hello N1GHTEYES,
Thank you for your post. I'll give it a try!

Regards,
Geoffrey
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top