Click here to Skip to main content
Click here to Skip to main content

Tagged as

Go to top

Reading BMP File Type with Visual Basic

, 14 Jun 2010
Rate this:
Please Sign up or sign in to vote.
This article just performs a way to read a file with Visual Basic 6.0 (VB)

Introduction

As we know, Microsoft Visual Basic 6.0 IDE supports the image file type *.bmp very well. So, writing your own code for processing *.bmp is not necessary. This article just performs a way to read a file with Visual Basic 6.0 (VB).

About Standard Bitmap File Type

*.bmp bitmap file type is the standard bitmap type in Windows OS from Windows 3.0. It is possible to use compression method to store data, but it seems not to be as effective as *.png modern type.

Here is the structure of *.bmp file type:

  • One 14-byte variant in BITMAPFILEHEADER structure: stored the common information of the file
  • One 40-byte variant in BITMAPINFOHEADER structure: stored the data information of the picture
  • One array called RGBQUAD: stored the colors appearing in the picture, 4 bytes per element of array
  • One array stored the color, which is in BGR (Blue – Green – Red) order: 3 bytes per color

RGBQUAD just stored the appeared colors, so we don’t care much about it.

To make it easier, declare the BITMAPFILE as follows:

Type BITMAPFILE
     bmfh As BITMAPFILEHEADER
     bmih As BITMAPINFOHEADER
     aBitmapBits() As BGR  ‘ contain 3-byte elements
End Type

BITMAPFILEHEADER Structure

We can declare in VB:

Type BITMAPFILEHEADER ‘14 bytes
     bfType As Integer
     bfSize As Long
     bfReserved1 As Integer
     bfReserved2 As Integer
     bfOffBits As Long
End Type

BITMAPINFOHEADER Structure

We declare:

Type BITMAPINFOHEADER ‘40 bytes
     biSize As Long
     biWidth As Long
     biHeight As Long
      iplanes As Integer
     biBitCount As Integer
     biCompression As Long
     biSizeImage As Long
     biXPelsPerMeter As Long
     biYPelsPerMeter As Long
     biClrUsed As Long
     biClrImportant As Long
End Type

Note: If you calculate according to biBitCount, then the formula is MaxNumberOfColor = 2^ biBitCount.

Windows allows these values: 1 (black/white); 4 (16 colors); 8 (256 colors); 24 (16.7 colors).

Blue – Green – Red (BGR) Type

The BGR color type contains 3 fields in order : blue element, green element, red element.

Type BGR ‘3 bytes / 1 variant
     rgbBlue As Byte
     rgbGreen As Byte
     rgbRed As Byte
End Type ‘note that the fields are always in order blue -> green -> red

Notes About Image Data

Please remember:

  1. Image data is stored in the last past of the *.bmp file.
  2. The number of elements stored in 1 line is always even. If size in byte of the width is odd, there will be a value 0 added at the end of line.
  3. Data is stored upside down: first line stores the last line of.

Implement In Visual Basic

Open Microsoft VB 6 IDE and put all the codes above in a module. Then add a picturebox into your main form. The properties of picturebox are in the following table:

Name picture1
Appearance 1 - 3D
AutoReDraw True
BorderStyle 0 - None

Than add a CommandButton named cmdWork.

We define a variant called strVarPath, of which value is the path to the picture in *.bmp type.

Use this code:

Dim bitmap1 As BITMAPFILE
Dim headers As Integer
Dim hei, wid, kx, ky As Long

Sub Init()
     Open strVarPath For Binary As #1
End Sub

Sub Read_header()
     Get #1, , bitmap1.bmfh
     Get #1, , bitmap1.bmih
     headers = LOF(1) - bitmap1.bmih.biSizeImage 	' size of the part from the
						' beginning to the data
End Sub

Sub Next_init()
Dim i, j As Long
Dim bitmp As String
     hei = bitmap1.bmih.biHeight
     wid = bitmap1.bmih.biWidth
     '--------------- get the height and width size
     kx = wid
     ky = hei
     '--------------- make the width even
     if (kx Mod 2 <> 0) then
          kx = kx + 1
     end if
     '--------------- declare an array, of which size = height x width
     ReDim bitmap1.aBitmapBits(ky, kx)
     Close #1
     Init
     bitmp = Space(headers)
     Get #1, , bitmp, '------ read the part before data
     For i = 1 To ky
          For j = 1 To kx
               Get #1, , bitmap1.aBitmapBits(i, j) ' here we read data line by line
          Next j
     Next i
End Sub

Sub process() '------ draw the data on picture1
Dim i, j As Long '------ image is drawn by drawing points, one by one on picture1
     Picture1.Height = hei
     Picture1.Width = wid
     For i = 1 To hei
          For j = 1 To wid
               Picture1.ForeColor = RGB(bitmap1.aBitmapBits(i, j).rgbRed, _
               bitmap1.aBitmapBits(i, j).rgbGreen, _
               bitmap1.aBitmapBits(i, j).rgbBlue)
               Picture1.PSet (j, hei - i)
          Next j
     Next i
End Sub

Sub Finish()
     Close #1
End Sub

Private Sub cmdWork_Click()
     Init
     Read_header
     Next_init
     process
     Finish
End Sub

Conclusion

Because of drawing points one by one, the method is ineffective about time (it costs nearly 1 second to process an 800x640 picture). This article is just to give you a look on how to read a file. To read and process a file more effectively, you should use another algorithm.

History

  • 14th June, 2010: Initial post

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

Duc Huy Nguyen

Vietnam Vietnam
Oops!

Comments and Discussions

 
GeneralOdd size bmp Pinmemberkevenm22-Jun-10 2:24 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Mobile
Web03 | 2.8.140905.1 | Last Updated 14 Jun 2010
Article Copyright 2010 by Duc Huy Nguyen
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid